SetBillingAddressOnCart.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Api\BillingAddressManagementInterface;
  14. use Magento\Customer\Api\AddressRepositoryInterface;
  15. /**
  16. * Set billing address for a specified shopping cart
  17. */
  18. class SetBillingAddressOnCart
  19. {
  20. /**
  21. * @var BillingAddressManagementInterface
  22. */
  23. private $billingAddressManagement;
  24. /**
  25. * @var Address
  26. */
  27. private $addressModel;
  28. /**
  29. * @var CheckCustomerAccount
  30. */
  31. private $checkCustomerAccount;
  32. /**
  33. * @var GetCustomerAddress
  34. */
  35. private $getCustomerAddress;
  36. /**
  37. * @param BillingAddressManagementInterface $billingAddressManagement
  38. * @param AddressRepositoryInterface $addressRepository
  39. * @param Address $addressModel
  40. * @param CheckCustomerAccount $checkCustomerAccount
  41. * @param GetCustomerAddress $getCustomerAddress
  42. */
  43. public function __construct(
  44. BillingAddressManagementInterface $billingAddressManagement,
  45. AddressRepositoryInterface $addressRepository,
  46. Address $addressModel,
  47. CheckCustomerAccount $checkCustomerAccount,
  48. GetCustomerAddress $getCustomerAddress
  49. ) {
  50. $this->billingAddressManagement = $billingAddressManagement;
  51. $this->addressRepository = $addressRepository;
  52. $this->addressModel = $addressModel;
  53. $this->checkCustomerAccount = $checkCustomerAccount;
  54. $this->getCustomerAddress = $getCustomerAddress;
  55. }
  56. /**
  57. * Set billing address for a specified shopping cart
  58. *
  59. * @param ContextInterface $context
  60. * @param CartInterface $cart
  61. * @param array $billingAddress
  62. * @return void
  63. * @throws GraphQlInputException
  64. */
  65. public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
  66. {
  67. $customerAddressId = $billingAddress['customer_address_id'] ?? null;
  68. $addressInput = $billingAddress['address'] ?? null;
  69. $useForShipping = $billingAddress['use_for_shipping'] ?? false;
  70. if (null === $customerAddressId && null === $addressInput) {
  71. throw new GraphQlInputException(
  72. __('The billing address must contain either "customer_address_id" or "address".')
  73. );
  74. }
  75. if ($customerAddressId && $addressInput) {
  76. throw new GraphQlInputException(
  77. __('The billing address cannot contain "customer_address_id" and "address" at the same time.')
  78. );
  79. }
  80. $addresses = $cart->getAllShippingAddresses();
  81. if ($useForShipping && count($addresses) > 1) {
  82. throw new GraphQlInputException(
  83. __('Using the "use_for_shipping" option with multishipping is not possible.')
  84. );
  85. }
  86. if (null === $customerAddressId) {
  87. $billingAddress = $this->addressModel->addData($addressInput);
  88. } else {
  89. $this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
  90. $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
  91. $billingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
  92. }
  93. $this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
  94. }
  95. }