Skip to content

Commit dcc6381

Browse files
author
Shawn Abramson
committed
Added super_group extension attribute to Magento\Quote\Api\Data\ProductOptionInterface to allow user to send associated product quantities when adding to cart via REST API
Added a 'grouped' cartItemProcessors to the Magento\Quote\Model\Quote\Item\Repository to reformat the super_group extension attribute into a format that is compatible with buy request
1 parent 86515dd commit dcc6381

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Magento\GroupedProduct\Api\Data;
4+
5+
interface GroupedItemQtyInterface extends \Magento\Framework\Api\ExtensibleDataInterface
6+
{
7+
const PRODUCT_ID = 'product_id';
8+
const QTY = 'qty';
9+
10+
/**
11+
* Associated product id
12+
*
13+
* @param int|string $value
14+
*/
15+
public function setProductId($value);
16+
17+
/**
18+
* Associated product id
19+
*
20+
* @return int|string
21+
*/
22+
public function getProductId();
23+
24+
/**
25+
* @param int|string $qty
26+
*/
27+
public function setQty($qty);
28+
29+
/**
30+
* @return int
31+
*/
32+
public function getQty();
33+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Magento\GroupedProduct\Model\Quote\Item;
4+
5+
use Magento\Framework\DataObject\Factory as ObjectFactory;
6+
use Magento\Quote\Api\Data\CartItemInterface;
7+
use Magento\Quote\Api\Data\ProductOptionInterface;
8+
use Magento\Quote\Model\Quote\Item\CartItemProcessorInterface;
9+
10+
/**
11+
* Class CartItemProcessor
12+
*/
13+
class CartItemProcessor implements CartItemProcessorInterface
14+
{
15+
/**
16+
* @var ObjectFactory
17+
*/
18+
protected $objectFactory;
19+
20+
/**
21+
* CartItemProcessor constructor.
22+
*
23+
* @param ObjectFactory $objectFactory
24+
*/
25+
public function __construct(ObjectFactory $objectFactory)
26+
{
27+
$this->objectFactory = $objectFactory;
28+
}
29+
30+
/**
31+
* @param CartItemInterface $cartItem
32+
*
33+
* @return \Magento\Framework\DataObject|null
34+
*/
35+
public function convertToBuyRequest(CartItemInterface $cartItem)
36+
{
37+
$productOption = $cartItem->getProductOption();
38+
if ($productOption instanceof ProductOptionInterface && $productOption->getExtensionAttributes()) {
39+
$superGroup = $productOption->getExtensionAttributes()->getSuperGroup();
40+
if (is_array($superGroup)) {
41+
$requestData = [];
42+
43+
/** @var GroupedItemQty $item */
44+
foreach ($superGroup as $item) {
45+
if (!isset($requestData['super_group'])) {
46+
$requestData['super_group'] = [];
47+
}
48+
49+
$requestData['super_group'][$item->getProductId()] = $item->getQty();
50+
}
51+
52+
if (!empty($requestData)) {
53+
return $this->objectFactory->create($requestData);
54+
}
55+
}
56+
}
57+
58+
return null;
59+
}
60+
61+
/**
62+
* @param CartItemInterface $cartItem
63+
*
64+
* @return CartItemInterface
65+
*/
66+
public function processOptions(CartItemInterface $cartItem)
67+
{
68+
return $cartItem;
69+
}
70+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Magento\GroupedProduct\Model\Quote\Item;
4+
5+
use Magento\Framework\Model\AbstractExtensibleModel;
6+
use Magento\GroupedProduct\Api\Data\GroupedItemQtyInterface;
7+
8+
/**
9+
* Class GroupedItemQty
10+
*/
11+
class GroupedItemQty extends AbstractExtensibleModel implements GroupedItemQtyInterface
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
public function setProductId($value)
17+
{
18+
$this->setData(self::PRODUCT_ID, $value);
19+
20+
return $this;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function getProductId()
27+
{
28+
return $this->getData(self::PRODUCT_ID);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function setQty($qty)
35+
{
36+
$this->setData(self::QTY, $qty);
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getQty()
45+
{
46+
return (int)$this->getData(self::QTY);
47+
}
48+
}

app/code/Magento/GroupedProduct/etc/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\GroupedProduct\Api\Data\GroupedItemQtyInterface" type="Magento\GroupedProduct\Model\Quote\Item\GroupedItemQty" />
10+
911
<type name="Magento\Quote\Model\Quote\Item\RelatedProducts">
1012
<arguments>
1113
<argument name="relatedProductTypes" xsi:type="array">
@@ -105,4 +107,11 @@
105107
</argument>
106108
</arguments>
107109
</type>
110+
<type name="Magento\Quote\Model\Quote\Item\Repository">
111+
<arguments>
112+
<argument name="cartItemProcessors" xsi:type="array">
113+
<item name="grouped" xsi:type="object">Magento\GroupedProduct\Model\Quote\Item\CartItemProcessor\Proxy</item>
114+
</argument>
115+
</arguments>
116+
</type>
108117
</config>

app/code/Magento/GroupedProduct/etc/extension_attributes.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
<extension_attributes for="Magento\Catalog\Api\Data\ProductLinkInterface">
1010
<attribute code="qty" type="float" />
1111
</extension_attributes>
12+
13+
<extension_attributes for="Magento\Quote\Api\Data\ProductOptionInterface">
14+
<attribute code="super_group" type="Magento\GroupedProduct\Api\Data\GroupedItemQtyInterface[]" />
15+
</extension_attributes>
1216
</config>

0 commit comments

Comments
 (0)