Skip to content

Commit cc0c942

Browse files
Allow customer to specify associated product qtys when adding grouped via REST
1 parent 3626d9c commit cc0c942

File tree

8 files changed

+208
-187
lines changed

8 files changed

+208
-187
lines changed

app/code/Magento/GroupedProduct/Api/Data/GroupedItemQtyInterface.php

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\GroupedProduct\Api\Data;
9+
10+
use Magento\Framework\Api\ExtensibleDataInterface;
11+
12+
/**
13+
* Represents `product item id with qty` of a grouped product.
14+
*/
15+
interface GroupedOptionsInterface extends ExtensibleDataInterface
16+
{
17+
/**
18+
* Get associated product id
19+
*
20+
* @return int
21+
*/
22+
public function getId(): int;
23+
24+
/**
25+
* Get associated product qty
26+
*
27+
* @return float
28+
*/
29+
public function getQty(): float;
30+
31+
/**
32+
* Set extension attributes
33+
*
34+
* @param \Magento\GroupedProduct\Api\Data\GroupedOptionsExtensionInterface $extensionAttributes
35+
* @return void
36+
*/
37+
public function setExtensionAttributes(GroupedOptionsExtensionInterface $extensionAttributes): void;
38+
39+
/**
40+
* Get extension attributes
41+
*
42+
* @return \Magento\GroupedProduct\Api\Data\GroupedOptionsExtensionInterface|null
43+
*/
44+
public function getExtensionAttributes(): ?GroupedOptionsExtensionInterface;
45+
}

app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ protected function _prepareProduct(\Magento\Framework\DataObject $buyRequest, $p
389389

390390
if (is_string($_result)) {
391391
return $_result;
392-
} elseif (!isset($_result[0])) {
392+
}
393+
394+
if (!isset($_result[0])) {
393395
return __('Cannot process the item.')->render();
394396
}
395397

@@ -407,6 +409,11 @@ protected function _prepareProduct(\Magento\Framework\DataObject $buyRequest, $p
407409
]
408410
)
409411
);
412+
if ($buyRequest->getSuperGroup()) {
413+
$serializedValue = $this->serializer->serialize($buyRequest->getSuperGroup());
414+
$_result[0]->addCustomOption('super_group', $serializedValue);
415+
}
416+
410417
$products[] = $_result[0];
411418
} else {
412419
$associatedProductsInfo[] = [$subProduct->getId() => $qty];

app/code/Magento/GroupedProduct/Model/Quote/Item/CartItemProcessor.php

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,94 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\GroupedProduct\Model\Quote\Item;
89

10+
use Magento\Framework\DataObject;
911
use Magento\Framework\DataObject\Factory as ObjectFactory;
12+
use Magento\Framework\Serialize\Serializer\Json;
13+
use Magento\GroupedProduct\Api\Data\GroupedOptionsInterface;
14+
use Magento\GroupedProduct\Api\Data\GroupedOptionsInterfaceFactory;
15+
use Magento\GroupedProduct\Model\Product\Type\Grouped;
16+
use Magento\Quote\Api\Data as QuoteApi;
1017
use Magento\Quote\Api\Data\CartItemInterface;
11-
use Magento\Quote\Api\Data\ProductOptionInterface;
1218
use Magento\Quote\Model\Quote\Item\CartItemProcessorInterface;
1319

1420
/**
15-
* A class that converts the Grouped Product super group, as received over RESTful API,
16-
* into the format needed within the buy request
17-
*
18-
* Class \Magento\GroupedProduct\Model\Quote\Item\CartItemProcessor
21+
* Converts grouped_options to super_group for the grouped product.
1922
*/
2023
class CartItemProcessor implements CartItemProcessorInterface
2124
{
25+
private const SUPER_GROUP_CODE = 'super_group';
26+
2227
/**
2328
* @var ObjectFactory
2429
*/
2530
private $objectFactory;
2631

2732
/**
28-
* CartItemProcessor constructor.
29-
*
33+
* @var GroupedOptionsInterface
34+
*/
35+
private $groupedOptionFactory;
36+
37+
/**
38+
* @var Json
39+
*/
40+
private $jsonSerializer;
41+
42+
/**
43+
* @var QuoteApi\ProductOptionExtensionFactory
44+
*/
45+
private $productOptionExtensionFactory;
46+
47+
/**
48+
* @var QuoteApi\ProductOptionInterfaceFactory
49+
*/
50+
private $productOptionFactory;
51+
52+
/**
3053
* @param ObjectFactory $objectFactory
54+
* @param GroupedOptionsInterfaceFactory $groupedOptionFactory
55+
* @param Json $jsonSerializer
56+
* @param QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory
57+
* @param QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
3158
*/
32-
public function __construct(ObjectFactory $objectFactory)
33-
{
59+
public function __construct(
60+
ObjectFactory $objectFactory,
61+
GroupedOptionsInterfaceFactory $groupedOptionFactory,
62+
Json $jsonSerializer,
63+
QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory,
64+
QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
65+
) {
3466
$this->objectFactory = $objectFactory;
67+
$this->groupedOptionFactory = $groupedOptionFactory;
68+
$this->jsonSerializer = $jsonSerializer;
69+
$this->productOptionExtensionFactory = $productOptionExtensionFactory;
70+
$this->productOptionFactory = $productOptionFactory;
3571
}
3672

3773
/**
38-
* Converts the super_group request data into the same format as native frontend add-to-cart
74+
* Converts the grouped_options request data into the same format as native frontend add-to-cart
3975
*
4076
* @param CartItemInterface $cartItem
41-
*
42-
* @return \Magento\Framework\DataObject|null
77+
* @return DataObject|null
4378
*/
44-
public function convertToBuyRequest(CartItemInterface $cartItem)
79+
public function convertToBuyRequest(CartItemInterface $cartItem): ?DataObject
4580
{
46-
$productOption = $cartItem->getProductOption();
47-
if ($productOption instanceof ProductOptionInterface && $productOption->getExtensionAttributes()) {
48-
$superGroup = $productOption->getExtensionAttributes()->getSuperGroup();
49-
if (is_array($superGroup)) {
81+
$extensionAttributes = $cartItem->getProductOption()
82+
? $cartItem->getProductOption()->getExtensionAttributes()
83+
: null;
84+
if ($extensionAttributes) {
85+
$groupedOptions = $extensionAttributes->getGroupedOptions();
86+
if ($groupedOptions) {
5087
$requestData = [];
5188

52-
/** @var GroupedItemQty $item */
53-
foreach ($superGroup as $item) {
54-
if (!isset($requestData['super_group'])) {
55-
$requestData['super_group'] = [];
56-
}
57-
58-
$requestData['super_group'][$item->getProductId()] = $item->getQty();
89+
foreach ($groupedOptions as $item) {
90+
$requestData[self::SUPER_GROUP_CODE][$item->getId()] = $item->getQty();
5991
}
6092

61-
if (!empty($requestData)) {
62-
return $this->objectFactory->create($requestData);
63-
}
93+
return $this->objectFactory->create($requestData);
6494
}
6595
}
6696

@@ -71,11 +101,29 @@ public function convertToBuyRequest(CartItemInterface $cartItem)
71101
* Option processor
72102
*
73103
* @param CartItemInterface $cartItem
74-
*
75104
* @return CartItemInterface
76105
*/
77-
public function processOptions(CartItemInterface $cartItem)
106+
public function processOptions(CartItemInterface $cartItem): CartItemInterface
78107
{
108+
if ($cartItem->getProductType() !== Grouped::TYPE_CODE) {
109+
return $cartItem;
110+
}
111+
112+
$superGroup = $cartItem->getOptionByCode(self::SUPER_GROUP_CODE);
113+
$superGroupValues = $superGroup ? $this->jsonSerializer->unserialize($superGroup->getValue()) : null;
114+
if ($superGroupValues) {
115+
$productOptions = [];
116+
foreach ($superGroupValues as $id => $qty) {
117+
$productOptions[] = $this->groupedOptionFactory->create(['id' => $id, 'qty' => $qty]);
118+
}
119+
120+
$extension = $this->productOptionExtensionFactory->create()->setGroupedOptions($productOptions);
121+
if (!$cartItem->getProductOption()) {
122+
$cartItem->setProductOption($this->productOptionFactory->create());
123+
}
124+
$cartItem->getProductOption()->setExtensionAttributes($extension);
125+
}
126+
79127
return $cartItem;
80128
}
81129
}

app/code/Magento/GroupedProduct/Model/Quote/Item/GroupedItemQty.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)