Skip to content

Commit 3140823

Browse files
author
m.mezhensky
committed
#257: create new id_v2 option
1 parent 9fa16c7 commit 3140823

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
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\CatalogGraphQl\Model\Resolver\Product;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
13+
use Magento\Framework\GraphQl\Query\Resolver\Value;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
17+
/**
18+
* @inheritdoc
19+
*
20+
* Format new option id_v2 in base64 encode for custom options
21+
*/
22+
class CustomizableOptionValueIdV2 implements ResolverInterface
23+
{
24+
private const OPTION_TYPE = 'custom-option';
25+
26+
/**
27+
* @inheritdoc
28+
*
29+
* Create new option id_v2 that encodes details for each option and in most cases can be presented
30+
* as base64("<option-type>/<option-id>/<option-value-id>")
31+
*
32+
* @param Field $field
33+
* @param ContextInterface $context
34+
* @param ResolveInfo $info
35+
* @param array|null $value
36+
* @param array|null $args
37+
* @return Value|mixed|void
38+
*/
39+
public function resolve(
40+
Field $field,
41+
$context,
42+
ResolveInfo $info,
43+
array $value = null,
44+
array $args = null
45+
) {
46+
$optionDetails = [
47+
self::OPTION_TYPE,
48+
$value['option_id'],
49+
$value['option_type_id']
50+
];
51+
52+
if (!isset($value['option_id']) || empty($value['option_id'])) {
53+
throw new LocalizedException(__('Wrong format option data: option_id should not be empty.'));
54+
}
55+
56+
if (!isset($value['option_type_id']) || empty($value['option_type_id'])) {
57+
throw new LocalizedException(__('Wrong format option data: option_type_id should not be empty.'));
58+
}
59+
60+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
61+
$content = \implode('/', $optionDetails);
62+
63+
return base64_encode($content);
64+
}
65+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type CustomizableAreaValue @doc(description: "CustomizableAreaValue defines the
132132
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC.")
133133
sku: String @doc(description: "The Stock Keeping Unit for this option.")
134134
max_characters: Int @doc(description: "The maximum number of characters that can be entered for this customizable option.")
135+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
135136
}
136137

137138
type CategoryTree implements CategoryInterface @doc(description: "Category Tree implementation.") {
@@ -153,6 +154,7 @@ type CustomizableDateValue @doc(description: "CustomizableDateValue defines the
153154
price: Float @doc(description: "The price assigned to this option.")
154155
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC.")
155156
sku: String @doc(description: "The Stock Keeping Unit for this option.")
157+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
156158
}
157159

158160
type CustomizableDropDownOption implements CustomizableOptionInterface @doc(description: "CustomizableDropDownOption contains information about a drop down menu that is defined as part of a customizable option.") {
@@ -166,6 +168,7 @@ type CustomizableDropDownValue @doc(description: "CustomizableDropDownValue defi
166168
sku: String @doc(description: "The Stock Keeping Unit for this option.")
167169
title: String @doc(description: "The display name for this option.")
168170
sort_order: Int @doc(description: "The order in which the option is displayed.")
171+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
169172
}
170173

171174
type CustomizableMultipleOption implements CustomizableOptionInterface @doc(description: "CustomizableMultipleOption contains information about a multiselect that is defined as part of a customizable option.") {
@@ -179,6 +182,7 @@ type CustomizableMultipleValue @doc(description: "CustomizableMultipleValue defi
179182
sku: String @doc(description: "The Stock Keeping Unit for this option.")
180183
title: String @doc(description: "The display name for this option.")
181184
sort_order: Int @doc(description: "The order in which the option is displayed.")
185+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
182186
}
183187

184188
type CustomizableFieldOption implements CustomizableOptionInterface @doc(description: "CustomizableFieldOption contains information about a text field that is defined as part of a customizable option.") {
@@ -191,6 +195,7 @@ type CustomizableFieldValue @doc(description: "CustomizableFieldValue defines th
191195
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC.")
192196
sku: String @doc(description: "The Stock Keeping Unit for this option.")
193197
max_characters: Int @doc(description: "The maximum number of characters that can be entered for this customizable option.")
198+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
194199
}
195200

196201
type CustomizableFileOption implements CustomizableOptionInterface @doc(description: "CustomizableFileOption contains information about a file picker that is defined as part of a customizable option.") {
@@ -205,6 +210,7 @@ type CustomizableFileValue @doc(description: "CustomizableFileValue defines the
205210
file_extension: String @doc(description: "The file extension to accept.")
206211
image_size_x: Int @doc(description: "The maximum width of an image.")
207212
image_size_y: Int @doc(description: "The maximum height of an image.")
213+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
208214
}
209215

210216
interface MediaGalleryInterface @doc(description: "Contains basic information about a product image or video.") @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\MediaGalleryTypeResolver") {
@@ -274,6 +280,7 @@ type CustomizableRadioValue @doc(description: "CustomizableRadioValue defines th
274280
sku: String @doc(description: "The Stock Keeping Unit for this option.")
275281
title: String @doc(description: "The display name for this option.")
276282
sort_order: Int @doc(description: "The order in which the radio button is displayed.")
283+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
277284
}
278285

279286
type CustomizableCheckboxOption implements CustomizableOptionInterface @doc(description: "CustomizableCheckbbixOption contains information about a set of checkbox values that are defined as part of a customizable option.") {
@@ -287,6 +294,7 @@ type CustomizableCheckboxValue @doc(description: "CustomizableCheckboxValue defi
287294
sku: String @doc(description: "The Stock Keeping Unit for this option.")
288295
title: String @doc(description: "The display name for this option.")
289296
sort_order: Int @doc(description: "The order in which the checkbox value is displayed.")
297+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CustomizableOptionValueIdV2")
290298
}
291299

292300
type VirtualProduct implements ProductInterface, CustomizableProductInterface @doc(description: "A virtual product is non-tangible product that does not require shipping and is not kept in inventory.") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Resolver\Variant\Attributes;
9+
10+
use Magento\Eav\Model\ResourceModel\Entity\Attribute;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
18+
/**
19+
* @inheritdoc
20+
*
21+
* Format new option id_v2 in base64 encode for super attribute options
22+
*/
23+
class ConfigurableAttributeIdV2 implements ResolverInterface
24+
{
25+
private const OPTION_TYPE = 'configurable';
26+
27+
/**
28+
* @var Attribute
29+
*/
30+
private $eavAttribute;
31+
32+
/**
33+
* ConfigurableAttributeIdV2 constructor.
34+
*
35+
* @param Attribute $eavAttribute
36+
*/
37+
public function __construct(Attribute $eavAttribute)
38+
{
39+
$this->eavAttribute = $eavAttribute;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*
45+
* Create new option id_v2 that encodes details for each option and in most cases can be presented
46+
* as base64("<option-type>/<attribute-id>/<value-index>")
47+
*
48+
* @param Field $field
49+
* @param ContextInterface $context
50+
* @param ResolveInfo $info
51+
* @param array|null $value
52+
* @param array|null $args
53+
* @return Value|mixed|string
54+
* @throws LocalizedException
55+
*/
56+
public function resolve(
57+
Field $field,
58+
$context,
59+
ResolveInfo $info,
60+
array $value = null,
61+
array $args = null
62+
) {
63+
$attribute_id = $this->eavAttribute->getIdByCode('catalog_product', $value['code']);
64+
$optionDetails = [
65+
self::OPTION_TYPE,
66+
$attribute_id,
67+
$value['value_index']
68+
];
69+
70+
if (empty($attribute_id)) {
71+
throw new LocalizedException(__('Wrong format option data: attribute_id should not be empty.'));
72+
}
73+
74+
if (!isset($value['value_index']) || empty($value['value_index'])) {
75+
throw new LocalizedException(__('Wrong format option data: value_index should not be empty.'));
76+
}
77+
78+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
79+
$content = \implode('/', $optionDetails);
80+
81+
return base64_encode($content);
82+
}
83+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type ConfigurableAttributeOption @doc(description: "ConfigurableAttributeOption
1818
label: String @doc(description: "A string that describes the configurable attribute option")
1919
code: String @doc(description: "The ID assigned to the attribute")
2020
value_index: Int @doc(description: "A unique index number assigned to the configurable product option")
21+
id_v2: String @doc(description: "Base64 string, that encodes details for each option.") @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\Variant\\Attributes\\ConfigurableAttributeIdV2")
2122
}
2223

2324
type ConfigurableProductOptions @doc(description: "ConfigurableProductOptions defines configurable attributes for the specified product") {

0 commit comments

Comments
 (0)