diff --git a/vendor/magento/module-quote-graph-ql/Model/Cart/CreateBuyRequest.php b/vendor/magento/module-quote-graph-ql/Model/Cart/CreateBuyRequest.php index e15b7324ce2..7fdce5245b4 100644 --- a/vendor/magento/module-quote-graph-ql/Model/Cart/CreateBuyRequest.php +++ b/vendor/magento/module-quote-graph-ql/Model/Cart/CreateBuyRequest.php @@ -20,13 +20,21 @@ class CreateBuyRequest */ private $dataObjectFactory; + /** + * @var CreateBuyRequestDataProviderInterface[] + */ + private $providers; + /** * @param DataObjectFactory $dataObjectFactory + * @param array $providers */ public function __construct( - DataObjectFactory $dataObjectFactory + DataObjectFactory $dataObjectFactory, + array $providers = [] ) { $this->dataObjectFactory = $dataObjectFactory; + $this->providers = $providers; } /** @@ -39,21 +47,30 @@ class CreateBuyRequest public function execute(float $qty, array $customizableOptionsData): DataObject { $customizableOptions = []; + $enteredOptions = []; foreach ($customizableOptionsData as $customizableOption) { if (isset($customizableOption['value_string'])) { - $customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue( - $customizableOption['value_string'] - ); + if (!is_numeric($customizableOption['id'])) { + $enteredOptions[$customizableOption['id']] = $customizableOption['value_string']; + } else { + $customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue( + $customizableOption['value_string'] + ); + } } } - $dataArray = [ - 'data' => [ + $requestData = [ + [ 'qty' => $qty, - 'options' => $customizableOptions, - ], + 'options' => $customizableOptions + ] ]; - return $this->dataObjectFactory->create($dataArray); + foreach ($this->providers as $provider) { + $requestData[] = $provider->execute($enteredOptions); + } + + return $this->dataObjectFactory->create(['data' => array_merge([], ...$requestData)]); } /** diff --git a/vendor/magento/module-quote-graph-ql/Model/Cart/CreateBuyRequestDataProviderInterface.php b/vendor/magento/module-quote-graph-ql/Model/Cart/CreateBuyRequestDataProviderInterface.php new file mode 100644 index 00000000000..af52c2869e9 --- /dev/null +++ b/vendor/magento/module-quote-graph-ql/Model/Cart/CreateBuyRequestDataProviderInterface.php @@ -0,0 +1,19 @@ +uidEncoder = $uidEncoder; + $this->optionUidArgsProcessor = + $optionUidArgsProcessor ?: ObjectManager::getInstance()->get(CustomizableOptionUidArgsProcessor::class); } /** * Process the updateCartItems arguments for cart uids * - * @param string $fieldName, + * @param string $fieldName * @param array $args * @return array * @throws GraphQlInputException @@ -58,6 +67,10 @@ class CartItemsUidArgsProcessor implements ArgumentsProcessorInterface $args[$filterKey]['cart_items'][$key][self::ID] = $this->uidEncoder->decode((string)$uidFilter); unset($args[$filterKey]['cart_items'][$key][self::UID]); } + if (!empty($cartItem['customizable_options'])) { + $args[$filterKey]['cart_items'][$key]['customizable_options'] = + $this->optionUidArgsProcessor->process($fieldName, $cartItem['customizable_options']); + } } } return $args; diff --git a/vendor/magento/module-quote-graph-ql/Model/CartItem/CustomizableOptionUidArgsProcessor.php b/vendor/magento/module-quote-graph-ql/Model/CartItem/CustomizableOptionUidArgsProcessor.php new file mode 100644 index 00000000000..278239bba54 --- /dev/null +++ b/vendor/magento/module-quote-graph-ql/Model/CartItem/CustomizableOptionUidArgsProcessor.php @@ -0,0 +1,64 @@ +uidEncoder = $uidEncoder; + } + + /** + * Process the customizable options for updateCartItems arguments for uids + * + * @param string $fieldName + * @param array $customizableOptions + * @return array + * @throws GraphQlInputException + */ + public function process(string $fieldName, array $customizableOptions): array + { + foreach ($customizableOptions as $key => $option) { + $idFilter = $option[self::ID] ?? []; + $uidFilter = $option[self::UID] ?? []; + + if (!empty($idFilter) + && !empty($uidFilter) + && $fieldName === 'updateCartItems') { + throw new GraphQlInputException( + __( + '`%1` and `%2` can\'t be used for CustomizableOptionInput object at the same time.', + [self::ID, self::UID] + ) + ); + } elseif (!empty($uidFilter)) { + $customizableOptions[$key][self::ID] = $this->uidEncoder->decode((string)$uidFilter); + unset($customizableOptions[$key][self::UID]); + } + } + return $customizableOptions; + } +} diff --git a/vendor/magento/module-quote-graph-ql/etc/schema.graphqls b/vendor/magento/module-quote-graph-ql/etc/schema.graphqls index 1dc66531fba..feb531f107b 100644 --- a/vendor/magento/module-quote-graph-ql/etc/schema.graphqls +++ b/vendor/magento/module-quote-graph-ql/etc/schema.graphqls @@ -62,7 +62,8 @@ input CartItemInput @doc(description: "Defines an item to be added to the cart." } input CustomizableOptionInput @doc(description: "Defines a customizable option.") { - id: Int @doc(description: "The customizable option ID of the product.") + uid: ID @doc(description: "The unique ID for a `CartItemInterface` object.") + id: Int @deprecated(reason: "Use `uid` instead.") @doc(description: "The customizable option ID of the product.") value_string: String! @doc(description: "The string value of the option.") }