This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
AddSimpleProductToCart.php
102 lines (92 loc) · 3.04 KB
/
AddSimpleProductToCart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\QuoteGraphQl\Model\Cart;
use Exception;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder;
/**
* Add simple product to cart
*/
class AddSimpleProductToCart
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var BuyRequestBuilder
*/
private $buyRequestBuilder;
/**
* @param ProductRepositoryInterface $productRepository
* @param BuyRequestBuilder $buyRequestBuilder
*/
public function __construct(
ProductRepositoryInterface $productRepository,
BuyRequestBuilder $buyRequestBuilder
) {
$this->productRepository = $productRepository;
$this->buyRequestBuilder = $buyRequestBuilder;
}
/**
* Add simple product to cart
*
* @param Quote $cart
* @param array $cartItemData
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(Quote $cart, array $cartItemData): void
{
$sku = $this->extractSku($cartItemData);
try {
$product = $this->productRepository->get($sku, false, null, true);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
}
try {
$result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData));
} catch (Exception $e) {
throw new GraphQlInputException(
__(
'Could not add the product with SKU %sku to the shopping cart: %message',
['sku' => $sku, 'message' => $e->getMessage()]
)
);
}
if (is_string($result)) {
$e = new GraphQlInputException(__('Cannot add product to cart'));
$errors = array_unique(explode("\n", $result));
foreach ($errors as $error) {
$e->addError(new GraphQlInputException(__($error)));
}
throw $e;
}
}
/**
* Extract SKU from cart item data
*
* @param array $cartItemData
* @return string
* @throws GraphQlInputException
*/
private function extractSku(array $cartItemData): string
{
// Need to keep this for configurable product and backward compatibility.
if (!empty($cartItemData['parent_sku'])) {
return (string)$cartItemData['parent_sku'];
}
if (empty($cartItemData['data']['sku'])) {
throw new GraphQlInputException(__('Missed "sku" in cart item data'));
}
return (string)$cartItemData['data']['sku'];
}
}