Skip to content

Commit ff550ea

Browse files
committed
Merge branch 'ACP2E-267' of https://github.com/magento-l3/magento2ce into PR-2022-02-15-CE2
2 parents 65714d4 + 9945e09 commit ff550ea

File tree

14 files changed

+809
-18
lines changed

14 files changed

+809
-18
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Wishlist/ConfigurableOptions.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,54 @@
77

88
namespace Magento\ConfigurableProductGraphQl\Model\Wishlist;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Helper\Product\Configuration;
1112
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
13+
use Magento\Framework\EntityManager\MetadataPool;
1214
use Magento\Framework\Exception\LocalizedException;
1315
use Magento\Framework\GraphQl\Config\Element\Field;
1416
use Magento\Framework\GraphQl\Query\ResolverInterface;
17+
use Magento\Framework\GraphQl\Query\Uid;
1518
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1619

1720
/**
1821
* Fetches the selected configurable options
1922
*/
2023
class ConfigurableOptions implements ResolverInterface
2124
{
25+
/**
26+
* Option type name
27+
*/
28+
private const OPTION_TYPE = 'configurable';
29+
2230
/**
2331
* @var Configuration
2432
*/
2533
private $configurationHelper;
2634

35+
/**
36+
* @var MetadataPool
37+
*/
38+
private $metadataPool;
39+
40+
/**
41+
* @var Uid
42+
*/
43+
private $uidEncoder;
44+
2745
/**
2846
* @param Configuration $configurationHelper
47+
* @param MetadataPool $metadataPool
48+
* @param Uid $uidEncoder
2949
*/
3050
public function __construct(
31-
Configuration $configurationHelper
51+
Configuration $configurationHelper,
52+
MetadataPool $metadataPool,
53+
Uid $uidEncoder
3254
) {
3355
$this->configurationHelper = $configurationHelper;
56+
$this->metadataPool = $metadataPool;
57+
$this->uidEncoder = $uidEncoder;
3458
}
3559

3660
/**
@@ -52,12 +76,24 @@ public function resolve(
5276
/** @var ItemInterface $item */
5377
$item = $value['itemModel'];
5478
$result = [];
79+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
80+
$productLinkId = $item->getProduct()->getData($linkField);
5581

5682
foreach ($this->configurationHelper->getOptions($item) as $option) {
83+
if (isset($option['option_type'])) {
84+
//Don't return customizable options in this resolver
85+
continue;
86+
}
5787
$result[] = [
5888
'id' => $option['option_id'],
89+
'configurable_product_option_uid' => $this->uidEncoder->encode(
90+
self::OPTION_TYPE . '/' . $productLinkId . '/' . $option['option_id']
91+
),
5992
'option_label' => $option['label'],
6093
'value_id' => $option['option_value'],
94+
'configurable_product_option_value_uid' => $this->uidEncoder->encode(
95+
self::OPTION_TYPE . '/' . $option['option_id'] . '/' . $option['option_value']
96+
),
6197
'value_label' => $option['value'],
6298
];
6399
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ConfigurableProductGraphQl\Model\Wishlist;
9+
10+
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
11+
use Magento\CatalogGraphQl\Model\ProductDataProvider;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
17+
/**
18+
* Fetches the data of selected variant of configurable product
19+
*/
20+
class ConfiguredVariant implements ResolverInterface
21+
{
22+
/**
23+
* @var ProductDataProvider
24+
*/
25+
private $productDataProvider;
26+
27+
/**
28+
* @param ProductDataProvider $productDataProvider
29+
*/
30+
public function __construct(ProductDataProvider $productDataProvider)
31+
{
32+
$this->productDataProvider = $productDataProvider;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(
39+
Field $field,
40+
$context,
41+
ResolveInfo $info,
42+
array $value = null,
43+
array $args = null
44+
) {
45+
if (!$value['itemModel'] instanceof ItemInterface) {
46+
throw new LocalizedException(__('"itemModel" should be a "%instance" instance', [
47+
'instance' => ItemInterface::class
48+
]));
49+
}
50+
51+
$item = $value['itemModel'];
52+
$product = $item->getProduct();
53+
$option = $product->getCustomOption('simple_product');
54+
55+
return $option && $option->getProduct()
56+
? $this->productDataProvider->getProductDataById((int) $option->getProduct()->getId())
57+
: null;
58+
}
59+
}

app/code/Magento/ConfigurableProductGraphQl/etc/schema.graphqls

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ type SelectedConfigurableOption @doc(description: "Contains details about a sele
7777
}
7878

7979
type ConfigurableWishlistItem implements WishlistItemInterface @doc(description: "A configurable product wish list item."){
80-
child_sku: String! @doc(description: "The SKU of the simple product corresponding to a set of selected configurable options.") @resolver(class: "\\Magento\\ConfigurableProductGraphQl\\Model\\Wishlist\\ChildSku")
80+
child_sku: String! @deprecated(reason: "Use `ConfigurableWishlistItem.configured_variant.sku` instead.") @doc(description: "The SKU of the simple product corresponding to a set of selected configurable options.") @resolver(class: "\\Magento\\ConfigurableProductGraphQl\\Model\\Wishlist\\ChildSku")
8181
configurable_options: [SelectedConfigurableOption!] @resolver(class: "\\Magento\\ConfigurableProductGraphQl\\Model\\Wishlist\\ConfigurableOptions") @doc(description: "An array of selected configurable options.")
82+
configured_variant: ProductInterface @doc(description: "Product details of the selected variant. The value is null if some options are not configured.") @resolver(class: "\\Magento\\ConfigurableProductGraphQl\\Model\\Wishlist\\ConfiguredVariant")
8283
}
8384

8485
type ConfigurableProductOptionsSelection @doc(description: "Contains metadata corresponding to the selected configurable options.") {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WishlistGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Wishlist\Model\Item as WishlistItem;
15+
use Magento\WishlistGraphQl\Model\WishlistItem\DataProvider\CustomizableOption;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
class CustomizableOptions implements ResolverInterface
21+
{
22+
/**
23+
* @var CustomizableOption
24+
*/
25+
private $customizableOption;
26+
27+
/**
28+
* @param CustomizableOption $customizableOption
29+
*/
30+
public function __construct(
31+
CustomizableOption $customizableOption
32+
) {
33+
$this->customizableOption = $customizableOption;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
40+
{
41+
if (!isset($value['itemModel'])) {
42+
throw new LocalizedException(__('"itemModel" value should be specified'));
43+
}
44+
45+
/** @var WishlistItem $wishlistItem */
46+
$wishlistItem = $value['itemModel'];
47+
$wishlistItemOption = $wishlistItem->getOptionByCode('option_ids');
48+
49+
if (null === $wishlistItemOption) {
50+
return [];
51+
}
52+
53+
$customizableOptionsData = [];
54+
$customizableOptionIds = explode(',', $wishlistItemOption->getValue());
55+
56+
foreach ($customizableOptionIds as $customizableOptionId) {
57+
$customizableOption = $this->customizableOption->getData(
58+
$wishlistItem,
59+
(int)$customizableOptionId
60+
);
61+
$customizableOptionsData[] = $customizableOption;
62+
}
63+
return $customizableOptionsData;
64+
}
65+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WishlistGraphQl\Model\WishlistItem\DataProvider;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Query\Uid;
12+
use Magento\Wishlist\Model\Item as WishlistItem;
13+
14+
/**
15+
* Custom Option Data provider
16+
*/
17+
class CustomizableOption
18+
{
19+
/**
20+
* Option type name
21+
*/
22+
private const OPTION_TYPE = 'custom-option';
23+
24+
/**
25+
* @var CustomizableOptionValueInterface
26+
*/
27+
private $customizableOptionValue;
28+
29+
/**
30+
* @var Uid
31+
*/
32+
private $uidEncoder;
33+
34+
/**
35+
* @param CustomizableOptionValueInterface $customOptionValueDataProvider
36+
* @param Uid $uidEncoder
37+
*/
38+
public function __construct(
39+
CustomizableOptionValueInterface $customOptionValueDataProvider,
40+
Uid $uidEncoder
41+
) {
42+
$this->customizableOptionValue = $customOptionValueDataProvider;
43+
$this->uidEncoder = $uidEncoder;
44+
}
45+
46+
/**
47+
* Retrieve custom option data
48+
*
49+
* @param WishlistItem $wishlistItem
50+
* @param int $optionId
51+
* @return array
52+
* @throws LocalizedException
53+
*/
54+
public function getData(WishlistItem $wishlistItem, int $optionId): array
55+
{
56+
$product = $wishlistItem->getProduct();
57+
$option = $product->getOptionById($optionId);
58+
59+
if (!$option) {
60+
return [];
61+
}
62+
63+
$selectedOption = $wishlistItem->getOptionByCode('option_' . $option->getId());
64+
65+
$selectedOptionValueData = $this->customizableOptionValue->getData(
66+
$wishlistItem,
67+
$option,
68+
$selectedOption
69+
);
70+
71+
return [
72+
'id' => $option->getId(),
73+
'customizable_option_uid' => $this->uidEncoder->encode((string) self::OPTION_TYPE . '/' . $option->getId()),
74+
'label' => $option->getTitle(),
75+
'type' => $option->getType(),
76+
'values' => $selectedOptionValueData,
77+
'sort_order' => $option->getSortOrder(),
78+
'is_required' => $option->getIsRequire(),
79+
];
80+
}
81+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WishlistGraphQl\Model\WishlistItem\DataProvider\CustomizableOptionValue;
9+
10+
use Magento\Catalog\Model\Product\Option;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Wishlist\Model\Item as WishlistItem;
15+
use Magento\Wishlist\Model\Item\Option as SelectedOption;
16+
use Magento\WishlistGraphQl\Model\WishlistItem\DataProvider\CustomizableOptionValueInterface;
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
class Composite implements CustomizableOptionValueInterface
22+
{
23+
/**
24+
* @var ObjectManagerInterface
25+
*/
26+
private $objectManager;
27+
28+
/**
29+
* @var CustomizableOptionValueInterface[]
30+
*/
31+
private $customizableOptionValues;
32+
33+
/**
34+
* @param ObjectManagerInterface $objectManager
35+
* @param CustomizableOptionValueInterface[] $customizableOptionValues
36+
*/
37+
public function __construct(
38+
ObjectManagerInterface $objectManager,
39+
array $customizableOptionValues = []
40+
) {
41+
$this->objectManager = $objectManager;
42+
$this->customizableOptionValues = $customizableOptionValues;
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function getData(
49+
WishlistItem $wishlistItem,
50+
Option $option,
51+
SelectedOption $selectedOption
52+
): array {
53+
$optionType = $option->getType();
54+
55+
if (!array_key_exists($optionType, $this->customizableOptionValues)) {
56+
throw new GraphQlInputException(__('Option type "%1" is not supported', $optionType));
57+
}
58+
$customizableOptionValueClassName = $this->customizableOptionValues[$optionType];
59+
60+
$customizableOptionValue = $this->objectManager->get($customizableOptionValueClassName);
61+
if (!$customizableOptionValue instanceof CustomizableOptionValueInterface) {
62+
throw new LocalizedException(
63+
__('%1 doesn\'t implement CustomizableOptionValueInterface', $customizableOptionValueClassName)
64+
);
65+
}
66+
return $customizableOptionValue->getData($wishlistItem, $option, $selectedOption);
67+
}
68+
}

0 commit comments

Comments
 (0)