-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from AleksandrsKondratjevs/issue-3536
issue-3536 Add provider for grouped products when adding to cart
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
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,62 @@ | ||
<?php | ||
/** | ||
* ScandiPWA - Progressive Web App for Magento | ||
* | ||
* Copyright © Scandiweb, Inc. All rights reserved. | ||
* See LICENSE for license details. | ||
* | ||
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0) | ||
* @package scandipwa/quote-graphql | ||
* @link https://github.com/scandipwa/quote-graphql | ||
*/ | ||
|
||
namespace ScandiPWA\QuoteGraphQl\Model\Cart\BuyRequest; | ||
|
||
use Magento\Quote\Model\Cart\Data\CartItem; | ||
|
||
class GroupedDataProvider | ||
{ | ||
private const OPTION_TYPE = 'grouped'; | ||
|
||
/** | ||
* @inheritdoc | ||
* | ||
* @phpcs:disable Magento2.Functions.DiscouragedFunction | ||
*/ | ||
public function execute(CartItem $cartItem): array | ||
{ | ||
$groupedData = []; | ||
|
||
foreach ($cartItem->getSelectedOptions() as $optionData) { | ||
$optionData = \explode('/', base64_decode($optionData->getId())); | ||
|
||
if ($this->isProviderApplicable($optionData) === false) { | ||
continue; | ||
} | ||
|
||
[, $simpleProductId, $quantity] = $optionData; | ||
|
||
$groupedData[$simpleProductId] = $quantity; | ||
} | ||
|
||
if (empty($groupedData)) { | ||
return $groupedData; | ||
} | ||
|
||
$result = ['super_group' => $groupedData]; | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Checks whether this provider is applicable for the current option | ||
* | ||
* @param array $optionData | ||
* | ||
* @return bool | ||
*/ | ||
private function isProviderApplicable(array $optionData): bool | ||
{ | ||
return $optionData[0] === self::OPTION_TYPE; | ||
} | ||
} |
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,21 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* ScandiPWA - Progressive Web App for Magento | ||
* | ||
* Copyright © Scandiweb, Inc. All rights reserved. | ||
* See LICENSE for license details. | ||
* | ||
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0) | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Quote\Model\Cart\BuyRequest\BuyRequestBuilder"> | ||
<arguments> | ||
<argument name="providers" xsi:type="array"> | ||
<item name="grouped" xsi:type="object">ScandiPWA\QuoteGraphQl\Model\Cart\BuyRequest\GroupedDataProvider</item> | ||
</argument> | ||
</arguments> | ||
</type> | ||
</config> | ||
|