Skip to content

Commit 0ace0c3

Browse files
27845 fix when qty or id not specified in grouped_options
1 parent 768b305 commit 0ace0c3

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ interface GroupedOptionsInterface extends ExtensibleDataInterface
1717
/**
1818
* Get associated product id
1919
*
20-
* @return int
20+
* @return int|null
2121
*/
22-
public function getId(): int;
22+
public function getId(): ?int;
2323

2424
/**
2525
* Get associated product qty
2626
*
27-
* @return int
27+
* @return int|null
2828
*/
29-
public function getQty(): int;
29+
public function getQty(): ?int;
3030

3131
/**
3232
* Set extension attributes

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

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

1010
use Magento\Framework\DataObject;
1111
use Magento\Framework\DataObject\Factory as ObjectFactory;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\GroupedProduct\Api\Data\GroupedOptionsInterface;
1214
use Magento\GroupedProduct\Model\Product\Type\Grouped;
1315
use Magento\Quote\Api\Data as QuoteApi;
1416
use Magento\Quote\Api\Data\CartItemInterface;
@@ -71,17 +73,33 @@ public function convertToBuyRequest(CartItemInterface $cartItem): ?DataObject
7173
$groupedOptions = $cartItem->getProductOption()->getExtensionAttributes()->getGroupedOptions();
7274
$this->groupedOptions = $groupedOptions;
7375

74-
$requestData = [];
75-
foreach ($groupedOptions as $item) {
76-
$requestData[self::SUPER_GROUP_CODE][$item->getId()] = $item->getQty();
77-
}
78-
79-
return $this->objectFactory->create($requestData);
76+
return $this->objectFactory->create($this->getConvertedData($groupedOptions));
8077
}
8178

8279
return null;
8380
}
8481

82+
/**
83+
* Returns grouped_options converted to super_group data
84+
*
85+
* @param GroupedOptionsInterface[] $groupedOptions
86+
* @return array
87+
* @throws LocalizedException
88+
*/
89+
private function getConvertedData(array $groupedOptions): array
90+
{
91+
$requestData = [];
92+
foreach ($groupedOptions as $item) {
93+
/** @var GroupedOptionsInterface $item */
94+
if ($item->getQty() === null || $item->getId() === null) {
95+
throw new LocalizedException(__('Please specify id and qty for grouped options.'));
96+
}
97+
$requestData[self::SUPER_GROUP_CODE][$item->getId()] = $item->getQty();
98+
}
99+
100+
return $requestData;
101+
}
102+
85103
/**
86104
* Option processor
87105
*

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

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

2323
/**
24-
* @var int
24+
* @var int|null
2525
*/
2626
private $id;
2727

@@ -31,12 +31,15 @@ class GroupedOptions implements GroupedOptionsInterface
3131
private $extensionAttributes;
3232

3333
/**
34-
* @param int $id
35-
* @param int $qty
34+
* @param int|null $id
35+
* @param int|null $qty
3636
* @param GroupedOptionsExtensionInterface|null $extensionAttributes
3737
*/
38-
public function __construct(int $id, int $qty, $extensionAttributes = null)
39-
{
38+
public function __construct(
39+
?int $id = null,
40+
?int $qty = null,
41+
?GroupedOptionsExtensionInterface $extensionAttributes = null
42+
) {
4043
$this->id = $id;
4144
$this->qty = $qty;
4245
$this->extensionAttributes = $extensionAttributes;
@@ -45,15 +48,15 @@ public function __construct(int $id, int $qty, $extensionAttributes = null)
4548
/**
4649
* @inheritDoc
4750
*/
48-
public function getId(): int
51+
public function getId(): ?int
4952
{
5053
return $this->id;
5154
}
5255

5356
/**
5457
* @inheritDoc
5558
*/
56-
public function getQty(): int
59+
public function getQty(): ?int
5760
{
5861
return $this->qty;
5962
}

0 commit comments

Comments
 (0)