ToOrderAddressPlugin.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Plugin\Quote\Address;
  6. use Magento\Framework\Exception\NoSuchEntityException;
  7. use Magento\Quote\Model\Quote\Address;
  8. use Magento\Quote\Model\Quote\Address\ToOrderAddress;
  9. use Magento\Sales\Api\Data\OrderAddressExtensionInterface;
  10. use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
  11. use Magento\Sales\Api\Data\OrderAddressInterface;
  12. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  13. use Temando\Shipping\Model\ResourceModel\Repository\AddressRepositoryInterface;
  14. /**
  15. * @package Temando\Shipping\Plugin
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link https://www.temando.com/
  19. */
  20. class ToOrderAddressPlugin
  21. {
  22. /**
  23. * @var ModuleConfigInterface
  24. */
  25. private $config;
  26. /**
  27. * @var AddressRepositoryInterface
  28. */
  29. private $addressRepository;
  30. /**
  31. * @var OrderAddressExtensionInterfaceFactory
  32. */
  33. private $addressExtensionFactory;
  34. /**
  35. * ToOrderAddressPlugin constructor.
  36. *
  37. * @param ModuleConfigInterface $config
  38. * @param AddressRepositoryInterface $addressRepository
  39. * @param OrderAddressExtensionInterfaceFactory $addressExtensionFactory
  40. */
  41. public function __construct(
  42. ModuleConfigInterface $config,
  43. AddressRepositoryInterface $addressRepository,
  44. OrderAddressExtensionInterfaceFactory $addressExtensionFactory
  45. ) {
  46. $this->config = $config;
  47. $this->addressRepository = $addressRepository;
  48. $this->addressExtensionFactory = $addressExtensionFactory;
  49. }
  50. /**
  51. * Copy extension attributes (dynamic fields selected in checkout) to order
  52. * shipping address. When the order gets placed in Magento, these checkout
  53. * fields must be transmitted to the API. There is no need to persist them
  54. * to data storage though.
  55. *
  56. * @see \Temando\Shipping\Plugin\Sales\OrderRepositoryPlugin::afterSave
  57. *
  58. * @param ToOrderAddress $subject
  59. * @param OrderAddressInterface $orderAddress
  60. * @param Address $quoteAddress
  61. * @return OrderAddressInterface
  62. */
  63. public function afterConvert(
  64. ToOrderAddress $subject,
  65. OrderAddressInterface $orderAddress,
  66. Address $quoteAddress
  67. ) {
  68. $storeId = $quoteAddress->getQuote()->getStoreId();
  69. if (!$this->config->isEnabled($storeId)) {
  70. return $orderAddress;
  71. }
  72. if ($quoteAddress->getAddressType() !== Address::ADDRESS_TYPE_SHIPPING) {
  73. // no need to handle billing addresses
  74. return $orderAddress;
  75. }
  76. try {
  77. $checkoutAddress = $this->addressRepository->getByQuoteAddressId($quoteAddress->getId());
  78. } catch (NoSuchEntityException $e) {
  79. // no additional fields selected during checkout
  80. return $orderAddress;
  81. }
  82. $extensionAttributes = $orderAddress->getExtensionAttributes();
  83. if (!$extensionAttributes instanceof OrderAddressExtensionInterface) {
  84. $extensionAttributes = $this->addressExtensionFactory->create();
  85. }
  86. $extensionAttributes->setCheckoutFields($checkoutAddress->getServiceSelection());
  87. $orderAddress->setExtensionAttributes($extensionAttributes);
  88. return $orderAddress;
  89. }
  90. }