SetShippingMethodOnCart.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Framework\Exception\InputException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Framework\Exception\StateException;
  11. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  12. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  13. use Magento\Quote\Model\Quote;
  14. use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
  15. use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
  16. use Magento\Checkout\Model\ShippingInformationFactory;
  17. use Magento\Checkout\Api\ShippingInformationManagementInterface;
  18. use Magento\Checkout\Model\ShippingInformation;
  19. /**
  20. * Class SetShippingMethodsOnCart
  21. *
  22. * Set shipping method for a specified shopping cart address
  23. */
  24. class SetShippingMethodOnCart
  25. {
  26. /**
  27. * @var ShippingInformationFactory
  28. */
  29. private $shippingInformationFactory;
  30. /**
  31. * @var QuoteAddressFactory
  32. */
  33. private $quoteAddressFactory;
  34. /**
  35. * @var QuoteAddressResource
  36. */
  37. private $quoteAddressResource;
  38. /**
  39. * @var ShippingInformationManagementInterface
  40. */
  41. private $shippingInformationManagement;
  42. /**
  43. * @param ShippingInformationManagementInterface $shippingInformationManagement
  44. * @param QuoteAddressFactory $quoteAddressFactory
  45. * @param QuoteAddressResource $quoteAddressResource
  46. * @param ShippingInformationFactory $shippingInformationFactory
  47. */
  48. public function __construct(
  49. ShippingInformationManagementInterface $shippingInformationManagement,
  50. QuoteAddressFactory $quoteAddressFactory,
  51. QuoteAddressResource $quoteAddressResource,
  52. ShippingInformationFactory $shippingInformationFactory
  53. ) {
  54. $this->shippingInformationManagement = $shippingInformationManagement;
  55. $this->quoteAddressResource = $quoteAddressResource;
  56. $this->quoteAddressFactory = $quoteAddressFactory;
  57. $this->shippingInformationFactory = $shippingInformationFactory;
  58. }
  59. /**
  60. * Sets shipping method for a specified shopping cart address
  61. *
  62. * @param Quote $cart
  63. * @param int $cartAddressId
  64. * @param string $carrierCode
  65. * @param string $methodCode
  66. * @throws GraphQlInputException
  67. * @throws GraphQlNoSuchEntityException
  68. */
  69. public function execute(Quote $cart, int $cartAddressId, string $carrierCode, string $methodCode): void
  70. {
  71. $quoteAddress = $this->quoteAddressFactory->create();
  72. $this->quoteAddressResource->load($quoteAddress, $cartAddressId);
  73. /** @var ShippingInformation $shippingInformation */
  74. $shippingInformation = $this->shippingInformationFactory->create();
  75. /* If the address is not a shipping address (but billing) the system will find the proper shipping address for
  76. the selected cart and set the information there (actual for single shipping address) */
  77. $shippingInformation->setShippingAddress($quoteAddress);
  78. $shippingInformation->setShippingCarrierCode($carrierCode);
  79. $shippingInformation->setShippingMethodCode($methodCode);
  80. try {
  81. $this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
  82. } catch (NoSuchEntityException $exception) {
  83. throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
  84. } catch (StateException $exception) {
  85. throw new GraphQlInputException(__($exception->getMessage()));
  86. } catch (InputException $exception) {
  87. throw new GraphQlInputException(__($exception->getMessage()));
  88. }
  89. }
  90. }