MultishippingSavePlugin.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Plugin\Multishipping\Checkout;
  6. use Magento\Framework\Api\AttributeInterface;
  7. use Magento\Framework\Api\AttributeInterfaceFactory;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Multishipping\Model\Checkout\Type\Multishipping;
  11. use Temando\Shipping\Api\Data\Checkout\AddressInterface;
  12. use Temando\Shipping\Api\Data\Checkout\AddressInterfaceFactory;
  13. use Temando\Shipping\Model\Checkout\Schema\CheckoutFieldsSchema;
  14. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  15. use Temando\Shipping\Model\ResourceModel\Repository\AddressRepositoryInterface;
  16. /**
  17. * @package Temando\Shipping\Plugin
  18. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  19. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link https://www.temando.com/
  21. */
  22. class MultishippingSavePlugin
  23. {
  24. /**
  25. * @var RequestInterface
  26. */
  27. private $request;
  28. /**
  29. * @var AttributeInterfaceFactory
  30. */
  31. private $attributeFactory;
  32. /**
  33. * @var AddressRepositoryInterface
  34. */
  35. private $addressRepository;
  36. /**
  37. * @var AddressInterfaceFactory
  38. */
  39. private $addressFactory;
  40. /**
  41. * @var CheckoutFieldsSchema
  42. */
  43. private $schema;
  44. /**
  45. * @var ModuleConfigInterface
  46. */
  47. private $moduleConfig;
  48. /**
  49. * MultishippingSavePlugin constructor.
  50. *
  51. * @param RequestInterface $request
  52. * @param AddressRepositoryInterface $addressRepository
  53. * @param AddressInterfaceFactory $addressFactory
  54. * @param CheckoutFieldsSchema $schema
  55. * @param AttributeInterfaceFactory $attributeFactory
  56. * @param ModuleConfigInterface $moduleConfig
  57. */
  58. public function __construct(
  59. RequestInterface $request,
  60. AddressRepositoryInterface $addressRepository,
  61. AddressInterfaceFactory $addressFactory,
  62. CheckoutFieldsSchema $schema,
  63. AttributeInterfaceFactory $attributeFactory,
  64. ModuleConfigInterface $moduleConfig
  65. ) {
  66. $this->request = $request;
  67. $this->addressRepository = $addressRepository;
  68. $this->addressFactory = $addressFactory;
  69. $this->schema = $schema;
  70. $this->attributeFactory = $attributeFactory;
  71. $this->moduleConfig = $moduleConfig;
  72. }
  73. /**
  74. * Convert user input into attribute types as required for the
  75. * checkout_fields extension attribute.
  76. *
  77. * @param string[] $quoteItem
  78. * @return AttributeInterface[]
  79. */
  80. private function extractCheckoutFields(array $quoteItem)
  81. {
  82. $availableFields = $this->schema->getFields();
  83. $checkoutAttributes = array_intersect_key($quoteItem, $availableFields);
  84. $checkoutFields = [];
  85. foreach ($checkoutAttributes as $attributeCode => $value) {
  86. if (!isset($availableFields[$attributeCode])) {
  87. continue;
  88. }
  89. /** @var \Temando\Shipping\Model\Checkout\Schema\CheckoutField $fieldDefinition */
  90. $fieldDefinition = $availableFields[$attributeCode];
  91. if ($fieldDefinition->getType() === 'checkbox') {
  92. $value = (bool) $value;
  93. }
  94. $attribute = $this->attributeFactory->create();
  95. $attribute->setAttributeCode($attributeCode);
  96. $attribute->setValue($value);
  97. $checkoutFields[$attributeCode] = $attribute;
  98. }
  99. return $checkoutFields;
  100. }
  101. /**
  102. * Prevent different checkout field values for the same address.
  103. *
  104. * @param AttributeInterface[] $newCheckoutFields
  105. * @param AttributeInterface[] $originalCheckoutFields
  106. * @return void
  107. * @throws LocalizedException
  108. */
  109. private function verifyCheckoutFieldSelection($newCheckoutFields, $originalCheckoutFields)
  110. {
  111. $msg = __('Please do not use different configurations for the same address.');
  112. $diff = array_merge(
  113. array_diff_key($newCheckoutFields, $originalCheckoutFields),
  114. array_diff_key($originalCheckoutFields, $newCheckoutFields)
  115. );
  116. if (!empty($diff)) {
  117. // fields mismatch detected
  118. throw new LocalizedException($msg);
  119. }
  120. $attributeCodes = array_keys($newCheckoutFields);
  121. foreach ($attributeCodes as $attributeCode) {
  122. $newCheckoutField = $newCheckoutFields[$attributeCode];
  123. $originalCheckoutField = $originalCheckoutFields[$attributeCode];
  124. if ($newCheckoutField->getValue() !== $originalCheckoutField->getValue()) {
  125. // field value mismatch detected
  126. throw new LocalizedException($msg);
  127. }
  128. }
  129. }
  130. /**
  131. * Set additional checkout fields selection to shipping address for rates
  132. * processing in multi shipping checkout.
  133. *
  134. * @param Multishipping $subject
  135. *
  136. * @return Multishipping
  137. * @throws LocalizedException
  138. */
  139. public function afterSave(Multishipping $subject)
  140. {
  141. $storeId = $subject->getQuote()->getStoreId();
  142. if (!$this->moduleConfig->isEnabled($storeId)) {
  143. return $subject;
  144. }
  145. $ship = $this->request->getParam('ship');
  146. if (empty($ship)) {
  147. return $subject;
  148. }
  149. /** @var \Magento\Quote\Model\Quote\Address[] $recollectAddresses */
  150. $recollectAddresses = [];
  151. // keep user input in session, prefill fields in case of error or when going back.
  152. $subject->getCheckoutSession()->setData('checkoutFieldSelection', $ship);
  153. foreach ($ship as $quoteItems) {
  154. foreach ($quoteItems as $itemId => $quoteItem) {
  155. // Skip item if it has no address (virtual or downloadable...)
  156. if (!isset($quoteItem['address'])) {
  157. continue;
  158. }
  159. // obtain shipping address for current quote item
  160. $addressId = $quoteItem['address'];
  161. $shippingAddress = $subject->getQuote()->getShippingAddressByCustomerAddressId($addressId);
  162. if (!$shippingAddress) {
  163. continue;
  164. }
  165. try {
  166. $address = $this->addressRepository->getByQuoteAddressId($shippingAddress->getId());
  167. } catch (LocalizedException $e) {
  168. $address = $this->addressFactory->create(['data' => [
  169. AddressInterface::SHIPPING_ADDRESS_ID => $shippingAddress->getId()
  170. ]]);
  171. }
  172. $checkoutFields = $this->extractCheckoutFields($quoteItem);
  173. if ($address->getServiceSelection()) {
  174. $this->verifyCheckoutFieldSelection($checkoutFields, $address->getServiceSelection());
  175. }
  176. $address->setServiceSelection($checkoutFields);
  177. $this->addressRepository->save($address);
  178. $recollectAddresses[$shippingAddress->getId()] = $shippingAddress;
  179. }
  180. }
  181. // re-collect rates with checkout field selection
  182. foreach ($recollectAddresses as $shippingAddress) {
  183. $shippingAddress->setCollectShippingRates(true);
  184. $shippingAddress->collectShippingRates();
  185. }
  186. return $subject;
  187. }
  188. }