Skip to content

Commit 8d413dd

Browse files
committed
Merge remote-tracking branch 'marjan/add-to-cart-category-permissions-29930' into catalog-permissions-29880
2 parents abd5481 + 6c41266 commit 8d413dd

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor;
9+
10+
use Magento\GraphQl\Model\Query\ContextInterface;
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
class ItemDataCompositeProcessor implements ItemDataProcessorInterface
16+
{
17+
/**
18+
* @var ItemDataProcessorInterface[]
19+
*/
20+
private $itemDataProcessors;
21+
22+
/**
23+
* @param ItemDataProcessorInterface[] $itemDataProcessors
24+
*/
25+
public function __construct(array $itemDataProcessors = [])
26+
{
27+
$this->itemDataProcessors = $itemDataProcessors;
28+
}
29+
30+
/**
31+
* Process cart item data
32+
*
33+
* @param array $cartItemData
34+
* @param ContextInterface $context
35+
* @return array
36+
*/
37+
public function process(array $cartItemData, ContextInterface $context): array
38+
{
39+
foreach ($this->itemDataProcessors as $itemDataProcessor) {
40+
$cartItemData = $itemDataProcessor->process($cartItemData, $context);
41+
}
42+
43+
return $cartItemData;
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor;
9+
10+
use Magento\GraphQl\Model\Query\ContextInterface;
11+
12+
/**
13+
* Process Cart Item Data
14+
*/
15+
interface ItemDataProcessorInterface
16+
{
17+
/**
18+
* Process cart item data
19+
*
20+
* @param array $cartItemData
21+
* @param ContextInterface $context
22+
* @return array
23+
*/
24+
public function process(array $cartItemData, ContextInterface $context): array;
25+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/AddProductsToCart.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\GraphQl\Model\Query\ContextInterface;
1415
use Magento\Quote\Model\Cart\AddProductsToCart as AddProductsToCartService;
1516
use Magento\Quote\Model\Cart\Data\AddProductsToCartOutput;
1617
use Magento\Quote\Model\Cart\Data\CartItemFactory;
1718
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1819
use Magento\Quote\Model\Cart\Data\Error;
20+
use Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataProcessorInterface;
1921

2022
/**
2123
* Resolver for addProductsToCart mutation
@@ -34,16 +36,24 @@ class AddProductsToCart implements ResolverInterface
3436
*/
3537
private $addProductsToCartService;
3638

39+
/**
40+
* @var ItemDataProcessorInterface
41+
*/
42+
private $itemDataProcessor;
43+
3744
/**
3845
* @param GetCartForUser $getCartForUser
3946
* @param AddProductsToCartService $addProductsToCart
47+
* @param ItemDataProcessorInterface $itemDataProcessor
4048
*/
4149
public function __construct(
4250
GetCartForUser $getCartForUser,
43-
AddProductsToCartService $addProductsToCart
51+
AddProductsToCartService $addProductsToCart,
52+
ItemDataProcessorInterface $itemDataProcessor
4453
) {
4554
$this->getCartForUser = $getCartForUser;
4655
$this->addProductsToCartService = $addProductsToCart;
56+
$this->itemDataProcessor = $itemDataProcessor;
4757
}
4858

4959
/**
@@ -68,6 +78,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6878

6979
$cartItems = [];
7080
foreach ($cartItemsData as $cartItemData) {
81+
if (!$this->itemIsAllowedToCart($cartItemData, $context)) {
82+
continue;
83+
}
7184
$cartItems[] = (new CartItemFactory())->create($cartItemData);
7285
}
7386

@@ -90,4 +103,21 @@ function (Error $error) {
90103
)
91104
];
92105
}
106+
107+
/**
108+
* Check if the item can be added to cart
109+
*
110+
* @param array $cartItemData
111+
* @param ContextInterface $context
112+
* @return bool
113+
*/
114+
private function itemIsAllowedToCart(array $cartItemData, ContextInterface $context): bool
115+
{
116+
$cartItemData = $this->itemDataProcessor->process($cartItemData, $context);
117+
if (isset($cartItemData['grant_checkout']) && $cartItemData['grant_checkout'] === false) {
118+
return false;
119+
}
120+
121+
return true;
122+
}
93123
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValueInterface" type="Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Composite" />
10+
<preference for="Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataProcessorInterface" type="Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataCompositeProcessor" />
1011
<type name="Magento\QuoteGraphQl\Model\Resolver\CartItemTypeResolver">
1112
<arguments>
1213
<argument name="supportedTypes" xsi:type="array">

0 commit comments

Comments
 (0)