Skip to content

Commit 026e68f

Browse files
committed
Extended product customizable options coverage
1 parent 908ed15 commit 026e68f

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@
3838
</item>
3939
<item name="customizable_options" xsi:type="array">
4040
<item name="field" xsi:type="string">CustomizableFieldOption</item>
41+
<item name="date" xsi:type="string">CustomizableDateOption</item>
4142
<item name="date_time" xsi:type="string">CustomizableDateOption</item>
43+
<item name="time" xsi:type="string">CustomizableDateOption</item>
4244
<item name="file" xsi:type="string">CustomizableFileOption</item>
4345
<item name="area" xsi:type="string">CustomizableAreaOption</item>
4446
<item name="drop_down" xsi:type="string">CustomizableDropDownOption</item>
47+
<item name="multiple" xsi:type="string">CustomizableMultipleOption</item>
4548
<item name="radio" xsi:type="string">CustomizableRadioOption</item>
49+
<item name="checkbox" xsi:type="string">CustomizableCheckboxOption</item>
4650
</item>
4751
</argument>
4852
</arguments>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,19 @@ type CustomizableDropDownValue @doc(description: "CustomizableDropDownValue defi
323323
sort_order: Int @doc(description: "The order in which the option is displayed")
324324
}
325325

326+
type CustomizableMultipleOption implements CustomizableOptionInterface @doc(description: "CustomizableMultipoleOption contains information about a multiselect that is defined as part of a customizable option") {
327+
value: [CustomizableMultipleValue] @doc(description: "An array that defines the set of options for a multiselect")
328+
}
329+
330+
type CustomizableMultipleValue @doc(description: "CustomizableMultipleValue defines the price and sku of a product whose page contains a customized multiselect") {
331+
option_type_id: Int @doc(description: "The ID assigned to the value")
332+
price: Float @doc(description: "The price assigned to this option")
333+
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC")
334+
sku: String @doc(description: "The Stock Keeping Unit for this option")
335+
title: String @doc(description: "The display name for this option")
336+
sort_order: Int @doc(description: "The order in which the option is displayed")
337+
}
338+
326339
type CustomizableFieldOption implements CustomizableOptionInterface @doc(description: "CustomizableFieldOption contains information about a text field that is defined as part of a customizable option") {
327340
value: CustomizableFieldValue @doc(description: "An object that defines a text field")
328341
product_sku: String @doc(description: "The Stock Keeping Unit of the base product")
@@ -407,6 +420,19 @@ type CustomizableRadioValue @doc(description: "CustomizableRadioValue defines t
407420
sort_order: Int @doc(description: "The order in which the radio button is displayed")
408421
}
409422

423+
type CustomizableCheckboxOption implements CustomizableOptionInterface @doc(description: "CustomizableCheckbbixOption contains information about a set of checkbox values that are defined as part of a customizable option") {
424+
value: [CustomizableCheckboxValue] @doc(description: "An array that defines a set of checkbox values")
425+
}
426+
427+
type CustomizableCheckboxValue @doc(description: "CustomizableCheckboxValue defines the price and sku of a product whose page contains a customized set of checkbox values") {
428+
option_type_id: Int @doc(description: "The ID assigned to the value")
429+
price: Float @doc(description: "The price assigned to this option")
430+
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC")
431+
sku: String @doc(description: "The Stock Keeping Unit for this option")
432+
title: String @doc(description: "The display name for this option")
433+
sort_order: Int @doc(description: "The order in which the checkbox value is displayed")
434+
}
435+
410436
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") {
411437
}
412438

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10+
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
11+
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
1012
use Magento\Catalog\Api\ProductRepositoryInterface;
1113
use Magento\Framework\DataObject;
1214
use Magento\Framework\DataObjectFactory;
@@ -23,6 +25,11 @@
2325
*/
2426
class AddSimpleProductToCart
2527
{
28+
/**
29+
* @var ProductCustomOptionRepositoryInterface
30+
*/
31+
private $customOptionRepository;
32+
2633
/**
2734
* @var ArrayManager
2835
*/
@@ -42,15 +49,18 @@ class AddSimpleProductToCart
4249
* @param ArrayManager $arrayManager
4350
* @param DataObjectFactory $dataObjectFactory
4451
* @param ProductRepositoryInterface $productRepository
52+
* @param ProductCustomOptionRepositoryInterface $customOptionRepository
4553
*/
4654
public function __construct(
4755
ArrayManager $arrayManager,
4856
DataObjectFactory $dataObjectFactory,
49-
ProductRepositoryInterface $productRepository
57+
ProductRepositoryInterface $productRepository,
58+
ProductCustomOptionRepositoryInterface $customOptionRepository
5059
) {
5160
$this->arrayManager = $arrayManager;
5261
$this->dataObjectFactory = $dataObjectFactory;
5362
$this->productRepository = $productRepository;
63+
$this->customOptionRepository = $customOptionRepository;
5464
}
5565

5666
/**
@@ -67,7 +77,7 @@ public function execute(Quote $cart, array $cartItemData): void
6777
{
6878
$sku = $this->extractSku($cartItemData);
6979
$qty = $this->extractQty($cartItemData);
70-
$customizableOptions = $this->extractCustomizableOptions($cartItemData);
80+
$customizableOptions = $this->extractCustomizableOptions($cartItemData, $sku);
7181

7282
try {
7383
$product = $this->productRepository->get($sku);
@@ -127,15 +137,23 @@ private function extractQty(array $cartItemData): float
127137
* Extract Customizable Options from cart item data
128138
*
129139
* @param array $cartItemData
140+
* @param string $sku
130141
* @return array
131142
*/
132-
private function extractCustomizableOptions(array $cartItemData): array
143+
private function extractCustomizableOptions(array $cartItemData, string $sku): array
133144
{
134145
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []);
135-
146+
$productCustomOptions = $this->customOptionRepository->getList($sku);
147+
$productCustomOptionsMap = $this->getProductCustomOptionsMap($productCustomOptions);
136148
$customizableOptionsData = [];
149+
137150
foreach ($customizableOptions as $customizableOption) {
138-
$customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
151+
$multipleOptionTypesList = ['multiple', 'checkbox']; // TODO: use constants
152+
if (in_array($productCustomOptionsMap[$customizableOption['id']]->getType(), $multipleOptionTypesList)) {
153+
$customizableOptionsData[$customizableOption['id']] = explode(',', $customizableOption['value']);
154+
} else {
155+
$customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
156+
}
139157
}
140158
return $customizableOptionsData;
141159
}
@@ -156,4 +174,21 @@ private function createBuyRequest(float $qty, array $customOptions): DataObject
156174
],
157175
]);
158176
}
177+
178+
/**
179+
* Creates an array with a key equals option ID
180+
*
181+
* @param array $productCustomOptions
182+
* @return array
183+
*/
184+
private function getProductCustomOptionsMap(array $productCustomOptions): array
185+
{
186+
$customOptionsData = [];
187+
/** @var ProductCustomOptionInterface $productCustomOption */
188+
foreach ($productCustomOptions as $productCustomOption) {
189+
$customOptionsData[$productCustomOption->getOptionId()] = $productCustomOption;
190+
}
191+
192+
return $customOptionsData;
193+
}
159194
}

app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/CustomizableOptionValue/Text.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function getData(
4242
): array {
4343
/** @var TextOptionType $optionTypeRenderer */
4444
$optionTypeRenderer = $option->groupFactory($option->getType());
45+
$optionTypeRenderer->setOption($option);
4546
$priceValueUnits = $this->priceUnitLabel->getData($option->getPriceType());
4647

4748
$selectedOptionValueData = [

0 commit comments

Comments
 (0)