Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customer to specify associated product qtys when adding grouped product to cart via RESTful API #27845

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
dcc6381
Added super_group extension attribute to Magento\Quote\Api\Data\Produ…
Apr 13, 2020
de1f626
In Magento\GroupedProduct\Model\Quote\Item\CartItemProcessor updated …
Apr 14, 2020
8d2c280
Added copyright notices, end blank lines, extension attributes getter…
Apr 19, 2020
e9fa0a8
Added in missing method short descriptions in GroupedItemQtyInterface…
Apr 19, 2020
d5160db
Chopped down long class description to be less than 120 chars
Apr 19, 2020
8b141e1
Merge remote-tracking branch 'upstream/2.4-develop' into 26909-groupe…
Apr 22, 2020
bb9c1e2
Merge branch '2.4-develop' into 26909-grouped-product-restapi
Sep 14, 2020
3bcdd48
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie Sep 24, 2020
7f49335
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie Sep 28, 2020
3626d9c
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie Sep 29, 2020
cc0c942
Allow customer to specify associated product qtys when adding grouped…
engcom-Charlie Sep 29, 2020
74709d2
test coverage
engcom-Charlie Sep 29, 2020
fa4c7b8
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie Sep 30, 2020
24c27a4
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie Nov 11, 2020
be84eb1
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie Nov 12, 2020
c936161
change super_group to grouped_options
engcom-Charlie Nov 12, 2020
7af13d7
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie Nov 18, 2020
4240a6b
change fix
engcom-Charlie Nov 18, 2020
768b305
improve fix Allow customer to specify associated product qtys when ad…
engcom-Charlie Nov 26, 2020
4aa5527
Merge branch '2.4-develop' into 26909-grouped-product-restapi
Nov 26, 2020
0ace0c3
27845 fix when qty or id not specified in grouped_options
engcom-Charlie Nov 27, 2020
bdfe7ec
27845 test when not all params specified in grouped_options
engcom-Charlie Nov 27, 2020
769653a
Merge remote-tracking branch 'shawnabramson/26909-grouped-product-res…
engcom-Charlie Nov 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GroupedProduct\Api\Data;

use Magento\Framework\Api\ExtensibleDataInterface;

/**
* Represents `product item id with qty` of a grouped product.
*/
interface GroupedOptionsInterface extends ExtensibleDataInterface
{
/**
* Get associated product id
*
* @return int|null
*/
public function getId(): ?int;

/**
* Get associated product qty
*
* @return int|null
*/
public function getQty(): ?int;

/**
* Set extension attributes
*
* @param \Magento\GroupedProduct\Api\Data\GroupedOptionsExtensionInterface $extensionAttributes
* @return void
*/
public function setExtensionAttributes(GroupedOptionsExtensionInterface $extensionAttributes): void;

/**
* Get extension attributes
*
* @return \Magento\GroupedProduct\Api\Data\GroupedOptionsExtensionInterface|null
*/
public function getExtensionAttributes(): ?GroupedOptionsExtensionInterface;
}
124 changes: 124 additions & 0 deletions app/code/Magento/GroupedProduct/Model/Quote/Item/CartItemProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GroupedProduct\Model\Quote\Item;

use Magento\Framework\DataObject;
use Magento\Framework\DataObject\Factory as ObjectFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\GroupedProduct\Api\Data\GroupedOptionsInterface;
use Magento\GroupedProduct\Model\Product\Type\Grouped;
use Magento\Quote\Api\Data as QuoteApi;
use Magento\Quote\Api\Data\CartItemInterface;
use Magento\Quote\Model\Quote\Item\CartItemProcessorInterface;

/**
* Converts grouped_options to super_group for the grouped product.
*/
class CartItemProcessor implements CartItemProcessorInterface
{
private const SUPER_GROUP_CODE = 'super_group';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it should be grouped_options instead of super_group


/**
* @var ObjectFactory
*/
private $objectFactory;

/**
* @var QuoteApi\ProductOptionExtensionFactory
*/
private $productOptionExtensionFactory;

/**
* @var QuoteApi\ProductOptionInterfaceFactory
*/
private $productOptionFactory;

/**
* @var array|null
*/
private $groupedOptions;

/**
* @param ObjectFactory $objectFactory
* @param QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory
* @param QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
*/
public function __construct(
ObjectFactory $objectFactory,
QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory,
QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
) {
$this->objectFactory = $objectFactory;
$this->productOptionExtensionFactory = $productOptionExtensionFactory;
$this->productOptionFactory = $productOptionFactory;
}

/**
* Converts the grouped_options request data into the same format as native frontend add-to-cart
*
* @param CartItemInterface $cartItem
* @return DataObject|null
*/
public function convertToBuyRequest(CartItemInterface $cartItem): ?DataObject
{
if ($cartItem->getProductOption()
&& $cartItem->getProductOption()->getExtensionAttributes()
&& $cartItem->getProductOption()->getExtensionAttributes()->getGroupedOptions()
) {
$groupedOptions = $cartItem->getProductOption()->getExtensionAttributes()->getGroupedOptions();
$this->groupedOptions = $groupedOptions;

return $this->objectFactory->create($this->getConvertedData($groupedOptions));
}

return null;
}

/**
* Returns grouped_options converted to super_group data
*
* @param GroupedOptionsInterface[] $groupedOptions
* @return array
* @throws LocalizedException
*/
private function getConvertedData(array $groupedOptions): array
{
$requestData = [];
foreach ($groupedOptions as $item) {
/** @var GroupedOptionsInterface $item */
if ($item->getQty() === null || $item->getId() === null) {
throw new LocalizedException(__('Please specify id and qty for grouped options.'));
}
$requestData[self::SUPER_GROUP_CODE][$item->getId()] = $item->getQty();
}

return $requestData;
}

/**
* Option processor
*
* @param CartItemInterface $cartItem
* @return CartItemInterface
*/
public function processOptions(CartItemInterface $cartItem): CartItemInterface
{
if (empty($this->groupedOptions) || $cartItem->getProductType() !== Grouped::TYPE_CODE) {
return $cartItem;
}

$extension = $this->productOptionExtensionFactory->create()
->setGroupedOptions($this->groupedOptions);
if (!$cartItem->getProductOption()) {
$cartItem->setProductOption($this->productOptionFactory->create());
}
$cartItem->getProductOption()->setExtensionAttributes($extension);

return $cartItem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GroupedProduct\Model\Quote\Item;

use Magento\GroupedProduct\Api\Data\GroupedOptionsInterface;
use Magento\GroupedProduct\Api\Data\GroupedOptionsExtensionInterface;

/**
* @inheritDoc
*/
class GroupedOptions implements GroupedOptionsInterface
{
/**
* @var int|null
*/
private $qty;

/**
* @var int|null
*/
private $id;

/**
* @var GroupedOptionsExtensionInterface|null
*/
private $extensionAttributes;

/**
* @param int|null $id
* @param int|null $qty
* @param GroupedOptionsExtensionInterface|null $extensionAttributes
*/
public function __construct(
?int $id = null,
?int $qty = null,
?GroupedOptionsExtensionInterface $extensionAttributes = null
) {
$this->id = $id;
$this->qty = $qty;
$this->extensionAttributes = $extensionAttributes;
}

/**
* @inheritDoc
*/
public function getId(): ?int
{
return $this->id;
}

/**
* @inheritDoc
*/
public function getQty(): ?int
{
return $this->qty;
}

/**
* @inheritDoc
*/
public function setExtensionAttributes(GroupedOptionsExtensionInterface $extensionAttributes): void
{
$this->extensionAttributes = $extensionAttributes;
}

/**
* @inheritDoc
*/
public function getExtensionAttributes(): ?GroupedOptionsExtensionInterface
{
return $this->extensionAttributes;
}
}
9 changes: 9 additions & 0 deletions app/code/Magento/GroupedProduct/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\GroupedProduct\Api\Data\GroupedOptionsInterface" type="Magento\GroupedProduct\Model\Quote\Item\GroupedOptions" />

<type name="Magento\Quote\Model\Quote\Item\RelatedProducts">
<arguments>
<argument name="relatedProductTypes" xsi:type="array">
Expand Down Expand Up @@ -105,6 +107,13 @@
</argument>
</arguments>
</type>
<type name="Magento\Quote\Model\Quote\Item\Repository">
<arguments>
<argument name="cartItemProcessors" xsi:type="array">
<item name="grouped" xsi:type="object">Magento\GroupedProduct\Model\Quote\Item\CartItemProcessor\Proxy</item>
</argument>
</arguments>
</type>
<type name="Magento\CatalogInventory\Observer\SaveInventoryDataObserver">
<arguments>
<argument name="parentItemProcessorPool" xsi:type="array">
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/GroupedProduct/etc/extension_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<extension_attributes for="Magento\Catalog\Api\Data\ProductLinkInterface">
<attribute code="qty" type="float" />
</extension_attributes>

<extension_attributes for="Magento\Quote\Api\Data\ProductOptionInterface">
<attribute code="grouped_options" type="Magento\GroupedProduct\Api\Data\GroupedOptionsInterface[]" />
</extension_attributes>
</config>
Loading