AddSimpleProductToCart.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\QuoteGraphQl\Model\Cart;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Framework\DataObject;
  10. use Magento\Framework\DataObjectFactory;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  13. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  14. use Magento\Framework\Stdlib\ArrayManager;
  15. use Magento\Quote\Model\Quote;
  16. /**
  17. * Add simple product to cart
  18. *
  19. * TODO: should be replaced for different types resolver
  20. */
  21. class AddSimpleProductToCart
  22. {
  23. /**
  24. * @var ArrayManager
  25. */
  26. private $arrayManager;
  27. /**
  28. * @var DataObjectFactory
  29. */
  30. private $dataObjectFactory;
  31. /**
  32. * @var ProductRepositoryInterface
  33. */
  34. private $productRepository;
  35. /**
  36. * @param ArrayManager $arrayManager
  37. * @param DataObjectFactory $dataObjectFactory
  38. * @param ProductRepositoryInterface $productRepository
  39. */
  40. public function __construct(
  41. ArrayManager $arrayManager,
  42. DataObjectFactory $dataObjectFactory,
  43. ProductRepositoryInterface $productRepository
  44. ) {
  45. $this->arrayManager = $arrayManager;
  46. $this->dataObjectFactory = $dataObjectFactory;
  47. $this->productRepository = $productRepository;
  48. }
  49. /**
  50. * Add simple product to cart
  51. *
  52. * @param Quote $cart
  53. * @param array $cartItemData
  54. * @return void
  55. * @throws GraphQlNoSuchEntityException
  56. * @throws GraphQlInputException
  57. * @throws \Magento\Framework\Exception\LocalizedException
  58. */
  59. public function execute(Quote $cart, array $cartItemData): void
  60. {
  61. $sku = $this->extractSku($cartItemData);
  62. $qty = $this->extractQty($cartItemData);
  63. $customizableOptions = $this->extractCustomizableOptions($cartItemData);
  64. try {
  65. $product = $this->productRepository->get($sku);
  66. } catch (NoSuchEntityException $e) {
  67. throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
  68. }
  69. try {
  70. $result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions));
  71. } catch (\Exception $e) {
  72. throw new GraphQlInputException(
  73. __(
  74. 'Could not add the product with SKU %sku to the shopping cart: %message',
  75. ['sku' => $sku, 'message' => $e->getMessage()]
  76. )
  77. );
  78. }
  79. if (is_string($result)) {
  80. throw new GraphQlInputException(__($result));
  81. }
  82. }
  83. /**
  84. * Extract SKU from cart item data
  85. *
  86. * @param array $cartItemData
  87. * @return string
  88. * @throws GraphQlInputException
  89. */
  90. private function extractSku(array $cartItemData): string
  91. {
  92. $sku = $this->arrayManager->get('data/sku', $cartItemData);
  93. if (!isset($sku)) {
  94. throw new GraphQlInputException(__('Missing key "sku" in cart item data'));
  95. }
  96. return (string)$sku;
  97. }
  98. /**
  99. * Extract Qty from cart item data
  100. *
  101. * @param array $cartItemData
  102. * @return float
  103. * @throws GraphQlInputException
  104. */
  105. private function extractQty(array $cartItemData): float
  106. {
  107. $qty = $this->arrayManager->get('data/qty', $cartItemData);
  108. if (!isset($qty)) {
  109. throw new GraphQlInputException(__('Missing key "qty" in cart item data'));
  110. }
  111. return (float)$qty;
  112. }
  113. /**
  114. * Extract Customizable Options from cart item data
  115. *
  116. * @param array $cartItemData
  117. * @return array
  118. */
  119. private function extractCustomizableOptions(array $cartItemData): array
  120. {
  121. $customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []);
  122. $customizableOptionsData = [];
  123. foreach ($customizableOptions as $customizableOption) {
  124. $customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
  125. }
  126. return $customizableOptionsData;
  127. }
  128. /**
  129. * Format GraphQl input data to a shape that buy request has
  130. *
  131. * @param float $qty
  132. * @param array $customOptions
  133. * @return DataObject
  134. */
  135. private function createBuyRequest(float $qty, array $customOptions): DataObject
  136. {
  137. return $this->dataObjectFactory->create([
  138. 'data' => [
  139. 'qty' => $qty,
  140. 'options' => $customOptions,
  141. ],
  142. ]);
  143. }
  144. }