Skip to content

Commit 768b305

Browse files
improve fix Allow customer to specify associated product qtys when adding grouped product to cart via RESTful API
1 parent 4240a6b commit 768b305

File tree

3 files changed

+18
-39
lines changed

3 files changed

+18
-39
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function getId(): int;
2424
/**
2525
* Get associated product qty
2626
*
27-
* @return float
27+
* @return int
2828
*/
29-
public function getQty(): float;
29+
public function getQty(): int;
3030

3131
/**
3232
* Set extension attributes

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

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
use Magento\Framework\DataObject;
1111
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;
1512
use Magento\GroupedProduct\Model\Product\Type\Grouped;
1613
use Magento\Quote\Api\Data as QuoteApi;
1714
use Magento\Quote\Api\Data\CartItemInterface;
@@ -29,16 +26,6 @@ class CartItemProcessor implements CartItemProcessorInterface
2926
*/
3027
private $objectFactory;
3128

32-
/**
33-
* @var GroupedOptionsInterface
34-
*/
35-
private $groupedOptionFactory;
36-
37-
/**
38-
* @var Json
39-
*/
40-
private $jsonSerializer;
41-
4229
/**
4330
* @var QuoteApi\ProductOptionExtensionFactory
4431
*/
@@ -56,21 +43,15 @@ class CartItemProcessor implements CartItemProcessorInterface
5643

5744
/**
5845
* @param ObjectFactory $objectFactory
59-
* @param GroupedOptionsInterfaceFactory $groupedOptionFactory
60-
* @param Json $jsonSerializer
6146
* @param QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory
6247
* @param QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
6348
*/
6449
public function __construct(
6550
ObjectFactory $objectFactory,
66-
GroupedOptionsInterfaceFactory $groupedOptionFactory,
67-
Json $jsonSerializer,
6851
QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory,
6952
QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
7053
) {
7154
$this->objectFactory = $objectFactory;
72-
$this->groupedOptionFactory = $groupedOptionFactory;
73-
$this->jsonSerializer = $jsonSerializer;
7455
$this->productOptionExtensionFactory = $productOptionExtensionFactory;
7556
$this->productOptionFactory = $productOptionFactory;
7657
}
@@ -83,21 +64,19 @@ public function __construct(
8364
*/
8465
public function convertToBuyRequest(CartItemInterface $cartItem): ?DataObject
8566
{
86-
$extensionAttributes = $cartItem->getProductOption()
87-
? $cartItem->getProductOption()->getExtensionAttributes()
88-
: null;
89-
if ($extensionAttributes) {
90-
$groupedOptions = $extensionAttributes->getGroupedOptions();
91-
if ($groupedOptions) {
92-
$this->groupedOptions = $groupedOptions;
93-
$requestData = [];
94-
95-
foreach ($groupedOptions as $item) {
96-
$requestData[self::SUPER_GROUP_CODE][$item->getId()] = $item->getQty();
97-
}
98-
99-
return $this->objectFactory->create($requestData);
67+
if ($cartItem->getProductOption()
68+
&& $cartItem->getProductOption()->getExtensionAttributes()
69+
&& $cartItem->getProductOption()->getExtensionAttributes()->getGroupedOptions()
70+
) {
71+
$groupedOptions = $cartItem->getProductOption()->getExtensionAttributes()->getGroupedOptions();
72+
$this->groupedOptions = $groupedOptions;
73+
74+
$requestData = [];
75+
foreach ($groupedOptions as $item) {
76+
$requestData[self::SUPER_GROUP_CODE][$item->getId()] = $item->getQty();
10077
}
78+
79+
return $this->objectFactory->create($requestData);
10180
}
10281

10382
return null;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class GroupedOptions implements GroupedOptionsInterface
1717
{
1818
/**
19-
* @var float
19+
* @var int
2020
*/
2121
private $qty;
2222

@@ -32,10 +32,10 @@ class GroupedOptions implements GroupedOptionsInterface
3232

3333
/**
3434
* @param int $id
35-
* @param float $qty
35+
* @param int $qty
3636
* @param GroupedOptionsExtensionInterface|null $extensionAttributes
3737
*/
38-
public function __construct(int $id, float $qty, $extensionAttributes = null)
38+
public function __construct(int $id, int $qty, $extensionAttributes = null)
3939
{
4040
$this->id = $id;
4141
$this->qty = $qty;
@@ -53,7 +53,7 @@ public function getId(): int
5353
/**
5454
* @inheritDoc
5555
*/
56-
public function getQty(): float
56+
public function getQty(): int
5757
{
5858
return $this->qty;
5959
}

0 commit comments

Comments
 (0)