SetShippingAddressOnCart.php 3.3 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\Cart;
  8. use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
  9. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  10. use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
  11. use Magento\Quote\Api\Data\CartInterface;
  12. use Magento\Quote\Model\Quote\Address;
  13. use Magento\Quote\Model\ShippingAddressManagementInterface;
  14. /**
  15. * Set single shipping address for a specified shopping cart
  16. */
  17. class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
  18. {
  19. /**
  20. * @var ShippingAddressManagementInterface
  21. */
  22. private $shippingAddressManagement;
  23. /**
  24. * @var Address
  25. */
  26. private $addressModel;
  27. /**
  28. * @var CheckCustomerAccount
  29. */
  30. private $checkCustomerAccount;
  31. /**
  32. * @var GetCustomerAddress
  33. */
  34. private $getCustomerAddress;
  35. /**
  36. * @param ShippingAddressManagementInterface $shippingAddressManagement
  37. * @param Address $addressModel
  38. * @param CheckCustomerAccount $checkCustomerAccount
  39. * @param GetCustomerAddress $getCustomerAddress
  40. */
  41. public function __construct(
  42. ShippingAddressManagementInterface $shippingAddressManagement,
  43. Address $addressModel,
  44. CheckCustomerAccount $checkCustomerAccount,
  45. GetCustomerAddress $getCustomerAddress
  46. ) {
  47. $this->shippingAddressManagement = $shippingAddressManagement;
  48. $this->addressModel = $addressModel;
  49. $this->checkCustomerAccount = $checkCustomerAccount;
  50. $this->getCustomerAddress = $getCustomerAddress;
  51. }
  52. /**
  53. * @inheritdoc
  54. *
  55. * @param ContextInterface $context
  56. * @param CartInterface $cart
  57. * @param array $shippingAddresses
  58. * @throws GraphQlInputException
  59. */
  60. public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddresses): void
  61. {
  62. if (count($shippingAddresses) > 1) {
  63. throw new GraphQlInputException(
  64. __('You cannot specify multiple shipping addresses.')
  65. );
  66. }
  67. $shippingAddress = current($shippingAddresses);
  68. $customerAddressId = $shippingAddress['customer_address_id'] ?? null;
  69. $addressInput = $shippingAddress['address'] ?? null;
  70. if (null === $customerAddressId && null === $addressInput) {
  71. throw new GraphQlInputException(
  72. __('The shipping address must contain either "customer_address_id" or "address".')
  73. );
  74. }
  75. if ($customerAddressId && $addressInput) {
  76. throw new GraphQlInputException(
  77. __('The shipping address cannot contain "customer_address_id" and "address" at the same time.')
  78. );
  79. }
  80. if (null === $customerAddressId) {
  81. $shippingAddress = $this->addressModel->addData($addressInput);
  82. } else {
  83. $this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
  84. $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
  85. $shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
  86. }
  87. $this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
  88. }
  89. }