SetShippingMethodsOnCart.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Resolver;
  8. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  9. use Magento\Framework\GraphQl\Query\ResolverInterface;
  10. use Magento\Framework\GraphQl\Config\Element\Field;
  11. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  12. use Magento\Framework\Stdlib\ArrayManager;
  13. use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
  14. use Magento\QuoteGraphQl\Model\Cart\SetShippingMethodOnCart;
  15. /**
  16. * Class SetShippingMethodsOnCart
  17. *
  18. * Mutation resolver for setting shipping methods for shopping cart
  19. */
  20. class SetShippingMethodsOnCart implements ResolverInterface
  21. {
  22. /**
  23. * @var SetShippingMethodOnCart
  24. */
  25. private $setShippingMethodOnCart;
  26. /**
  27. * @var ArrayManager
  28. */
  29. private $arrayManager;
  30. /**
  31. * @var GetCartForUser
  32. */
  33. private $getCartForUser;
  34. /**
  35. * @param ArrayManager $arrayManager
  36. * @param GetCartForUser $getCartForUser
  37. * @param SetShippingMethodOnCart $setShippingMethodOnCart
  38. */
  39. public function __construct(
  40. ArrayManager $arrayManager,
  41. GetCartForUser $getCartForUser,
  42. SetShippingMethodOnCart $setShippingMethodOnCart
  43. ) {
  44. $this->arrayManager = $arrayManager;
  45. $this->getCartForUser = $getCartForUser;
  46. $this->setShippingMethodOnCart = $setShippingMethodOnCart;
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
  52. {
  53. $shippingMethods = $this->arrayManager->get('input/shipping_methods', $args);
  54. $maskedCartId = $this->arrayManager->get('input/cart_id', $args);
  55. if (!$maskedCartId) {
  56. throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
  57. }
  58. if (!$shippingMethods) {
  59. throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing'));
  60. }
  61. $shippingMethod = reset($shippingMethods); // This point can be extended for multishipping
  62. if (!$shippingMethod['cart_address_id']) {
  63. throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
  64. }
  65. if (!$shippingMethod['shipping_carrier_code']) {
  66. throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
  67. }
  68. if (!$shippingMethod['shipping_method_code']) {
  69. throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
  70. }
  71. $userId = $context->getUserId();
  72. $cart = $this->getCartForUser->execute((string) $maskedCartId, $userId);
  73. $this->setShippingMethodOnCart->execute(
  74. $cart,
  75. $shippingMethod['cart_address_id'],
  76. $shippingMethod['shipping_carrier_code'],
  77. $shippingMethod['shipping_method_code']
  78. );
  79. return [
  80. 'cart' => [
  81. 'cart_id' => $maskedCartId,
  82. 'model' => $cart
  83. ]
  84. ];
  85. }
  86. }