Skip to content

Commit

Permalink
Merge pull request #92 from AleksandrsKondratjevs/issue-3536
Browse files Browse the repository at this point in the history
issue-3536 Add provider for grouped products when adding to cart
  • Loading branch information
carinadues authored Oct 28, 2021
2 parents 939b335 + 2f4e17a commit 9df4b64
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Model/Cart/BuyRequest/GroupedDataProvider.php
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;
}
}
21 changes: 21 additions & 0 deletions src/etc/graphql/di.xml
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>

0 comments on commit 9df4b64

Please sign in to comment.