|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Multishipping\Model\Cart; |
| 9 | + |
| 10 | +use Magento\Checkout\Controller\Cart; |
| 11 | +use Magento\Checkout\Controller\Sidebar\UpdateItemQty; |
| 12 | +use Magento\Checkout\Model\Session; |
| 13 | +use Magento\Checkout\Model\Cart as CartModel; |
| 14 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 15 | +use Magento\Framework\App\RequestInterface; |
| 16 | +use Magento\Framework\Exception\LocalizedException; |
| 17 | +use Magento\Multishipping\Model\DisableMultishipping; |
| 18 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 19 | +use Magento\Quote\Model\Quote; |
| 20 | + |
| 21 | +/** |
| 22 | + * Cleans shipping addresses and item assignments after MultiShipping flow |
| 23 | + * |
| 24 | + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) |
| 25 | + */ |
| 26 | +class MultishippingClearItemAddress |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var CartRepositoryInterface |
| 30 | + */ |
| 31 | + private $cartRepository; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Session |
| 35 | + */ |
| 36 | + private $checkoutSession; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var AddressRepositoryInterface |
| 40 | + */ |
| 41 | + private $addressRepository; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var DisableMultishipping |
| 45 | + */ |
| 46 | + private $disableMultishipping; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var CartModel |
| 50 | + */ |
| 51 | + private $cartmodel; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param CartRepositoryInterface $cartRepository |
| 55 | + * @param Session $checkoutSession |
| 56 | + * @param AddressRepositoryInterface $addressRepository |
| 57 | + * @param DisableMultishipping $disableMultishipping |
| 58 | + * @param CartModel $cartmodel |
| 59 | + */ |
| 60 | + public function __construct( |
| 61 | + CartRepositoryInterface $cartRepository, |
| 62 | + Session $checkoutSession, |
| 63 | + AddressRepositoryInterface $addressRepository, |
| 64 | + DisableMultishipping $disableMultishipping, |
| 65 | + CartModel $cartmodel |
| 66 | + ) { |
| 67 | + $this->cartRepository = $cartRepository; |
| 68 | + $this->checkoutSession = $checkoutSession; |
| 69 | + $this->addressRepository = $addressRepository; |
| 70 | + $this->disableMultishipping = $disableMultishipping; |
| 71 | + $this->cartmodel = $cartmodel; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Cleans shipping addresses and item assignments after MultiShipping flow |
| 76 | + * |
| 77 | + * @param Cart|UpdateItemQty $subject |
| 78 | + * @param RequestInterface $request |
| 79 | + * @throws LocalizedException |
| 80 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 81 | + */ |
| 82 | + public function clearAddressItem($subject, $request) |
| 83 | + { |
| 84 | + /** @var Quote $quote */ |
| 85 | + $quote = $this->checkoutSession->getQuote(); |
| 86 | + $isMultipleShippingAddressesPresent = $quote->isMultipleShippingAddresses(); |
| 87 | + if ($isMultipleShippingAddressesPresent || $this->isDisableMultishippingRequired($request, $quote)) { |
| 88 | + $this->disableMultishipping->execute($quote); |
| 89 | + foreach ($quote->getAllShippingAddresses() as $address) { |
| 90 | + $quote->removeAddress($address->getId()); |
| 91 | + } |
| 92 | + |
| 93 | + $shippingAddress = $quote->getShippingAddress(); |
| 94 | + $defaultShipping = $quote->getCustomer()->getDefaultShipping(); |
| 95 | + if ($defaultShipping) { |
| 96 | + $defaultCustomerAddress = $this->addressRepository->getById($defaultShipping); |
| 97 | + $shippingAddress->importCustomerAddressData($defaultCustomerAddress); |
| 98 | + } |
| 99 | + if ($isMultipleShippingAddressesPresent) { |
| 100 | + $this->checkoutSession->setMultiShippingAddressesFlag(true); |
| 101 | + } |
| 102 | + $this->cartRepository->save($quote); |
| 103 | + if ($subject instanceof UpdateItemQty) { |
| 104 | + $quote = $this->cartRepository->get($quote->getId()); |
| 105 | + $this->cartmodel->setQuote($quote); |
| 106 | + } |
| 107 | + } elseif ($this->disableMultishipping->execute($quote) && $this->isVirtualItemInQuote($quote)) { |
| 108 | + $quote->setTotalsCollectedFlag(false); |
| 109 | + $this->cartRepository->save($quote); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Checks whether quote has virtual items |
| 115 | + * |
| 116 | + * @param Quote $quote |
| 117 | + * @return bool |
| 118 | + */ |
| 119 | + private function isVirtualItemInQuote(Quote $quote): bool |
| 120 | + { |
| 121 | + $items = $quote->getItems(); |
| 122 | + if (!empty($items)) { |
| 123 | + foreach ($items as $item) { |
| 124 | + if ($item->getIsVirtual()) { |
| 125 | + return true; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Check if we have to disable multishipping mode depends on the request action name |
| 135 | + * |
| 136 | + * We should not disable multishipping mode if we are adding a new product item to the existing quote |
| 137 | + * |
| 138 | + * @param RequestInterface $request |
| 139 | + * @param Quote $quote |
| 140 | + * @return bool |
| 141 | + */ |
| 142 | + private function isDisableMultishippingRequired(RequestInterface $request, Quote $quote): bool |
| 143 | + { |
| 144 | + return $request->getActionName() !== "add" && $quote->getIsMultiShipping(); |
| 145 | + } |
| 146 | +} |
0 commit comments