-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
magento-engcom-team
merged 23 commits into
magento:2.4-develop
from
shawnabramson:26909-grouped-product-restapi
Dec 10, 2020
Merged
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…
de1f626
In Magento\GroupedProduct\Model\Quote\Item\CartItemProcessor updated …
8d2c280
Added copyright notices, end blank lines, extension attributes getter…
e9fa0a8
Added in missing method short descriptions in GroupedItemQtyInterface…
d5160db
Chopped down long class description to be less than 120 chars
8b141e1
Merge remote-tracking branch 'upstream/2.4-develop' into 26909-groupe…
bb9c1e2
Merge branch '2.4-develop' into 26909-grouped-product-restapi
3bcdd48
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie 7f49335
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie 3626d9c
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie cc0c942
Allow customer to specify associated product qtys when adding grouped…
engcom-Charlie 74709d2
test coverage
engcom-Charlie fa4c7b8
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie 24c27a4
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie be84eb1
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie c936161
change super_group to grouped_options
engcom-Charlie 7af13d7
Merge branch '2.4-develop' into 26909-grouped-product-restapi
engcom-Charlie 4240a6b
change fix
engcom-Charlie 768b305
improve fix Allow customer to specify associated product qtys when ad…
engcom-Charlie 4aa5527
Merge branch '2.4-develop' into 26909-grouped-product-restapi
0ace0c3
27845 fix when qty or id not specified in grouped_options
engcom-Charlie bdfe7ec
27845 test when not all params specified in grouped_options
engcom-Charlie 769653a
Merge remote-tracking branch 'shawnabramson/26909-grouped-product-res…
engcom-Charlie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
app/code/Magento/GroupedProduct/Api/Data/GroupedOptionsInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
124
app/code/Magento/GroupedProduct/Model/Quote/Item/CartItemProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
/** | ||
* @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; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
app/code/Magento/GroupedProduct/Model/Quote/Item/GroupedOptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofsuper_group