CartPlugin.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Model\Cart\Controller;
  7. class CartPlugin
  8. {
  9. /**
  10. * @var \Magento\Quote\Api\CartRepositoryInterface
  11. */
  12. private $cartRepository;
  13. /**
  14. * @var \Magento\Checkout\Model\Session
  15. */
  16. private $checkoutSession;
  17. /**
  18. * @var \Magento\Customer\Api\AddressRepositoryInterface
  19. */
  20. private $addressRepository;
  21. /**
  22. * @param \Magento\Quote\Api\CartRepositoryInterface $cartRepository
  23. * @param \Magento\Checkout\Model\Session $checkoutSession
  24. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  25. */
  26. public function __construct(
  27. \Magento\Quote\Api\CartRepositoryInterface $cartRepository,
  28. \Magento\Checkout\Model\Session $checkoutSession,
  29. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  30. ) {
  31. $this->cartRepository = $cartRepository;
  32. $this->checkoutSession = $checkoutSession;
  33. $this->addressRepository = $addressRepository;
  34. }
  35. /**
  36. * @param \Magento\Checkout\Controller\Cart $subject
  37. * @param \Magento\Framework\App\RequestInterface $request
  38. * @return void
  39. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  40. */
  41. public function beforeDispatch(
  42. \Magento\Checkout\Controller\Cart $subject,
  43. \Magento\Framework\App\RequestInterface $request
  44. ) {
  45. /** @var \Magento\Quote\Model\Quote $quote */
  46. $quote = $this->checkoutSession->getQuote();
  47. // Clear shipping addresses and item assignments after MultiShipping flow
  48. if ($quote->isMultipleShippingAddresses()) {
  49. foreach ($quote->getAllShippingAddresses() as $address) {
  50. $quote->removeAddress($address->getId());
  51. }
  52. $shippingAddress = $quote->getShippingAddress();
  53. $defaultShipping = $quote->getCustomer()->getDefaultShipping();
  54. if ($defaultShipping) {
  55. $defaultCustomerAddress = $this->addressRepository->getById(
  56. $defaultShipping
  57. );
  58. $shippingAddress->importCustomerAddressData($defaultCustomerAddress);
  59. }
  60. $this->cartRepository->save($quote);
  61. }
  62. }
  63. }