|
5 | 5 | */
|
6 | 6 | namespace Magento\Multishipping\Model\Checkout\Type\Multishipping;
|
7 | 7 |
|
| 8 | +use Magento\Checkout\Model\Cart as CustomerCart; |
| 9 | +use Magento\Checkout\Model\Cart\RequestQuantityProcessor; |
| 10 | +use Magento\Checkout\Model\Session; |
| 11 | +use Magento\Framework\App\RequestInterface; |
| 12 | +use Magento\Framework\Locale\ResolverInterface; |
| 13 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class for afterSave and beforeSave plugin for quote |
| 17 | + * |
| 18 | + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) |
| 19 | + */ |
8 | 20 | class Plugin
|
9 | 21 | {
|
10 | 22 | /**
|
11 |
| - * @var \Magento\Checkout\Model\Session |
| 23 | + * @var Session |
12 | 24 | */
|
13 |
| - protected $checkoutSession; |
| 25 | + private $checkoutSession; |
14 | 26 |
|
15 | 27 | /**
|
16 |
| - * @var string |
| 28 | + * @var CartRepositoryInterface |
17 | 29 | */
|
18 |
| - protected $checkoutStateBegin; |
| 30 | + private $cartRepository; |
19 | 31 |
|
20 | 32 | /**
|
21 |
| - * @param \Magento\Checkout\Model\Session $checkoutSession |
| 33 | + * @var RequestInterface |
22 | 34 | */
|
23 |
| - public function __construct(\Magento\Checkout\Model\Session $checkoutSession) |
24 |
| - { |
| 35 | + private $request; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ResolverInterface |
| 39 | + */ |
| 40 | + private $locale; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var RequestQuantityProcessor |
| 44 | + */ |
| 45 | + private $quantityProcessor; |
| 46 | + |
| 47 | + /** |
| 48 | + * Initialize constructor |
| 49 | + * |
| 50 | + * @param Session $checkoutSession |
| 51 | + * @param CartRepositoryInterface $cartRepository |
| 52 | + * @param RequestInterface $request |
| 53 | + * @param ResolverInterface $locale |
| 54 | + * @param RequestQuantityProcessor $quantityProcessor |
| 55 | + */ |
| 56 | + public function __construct( |
| 57 | + Session $checkoutSession, |
| 58 | + CartRepositoryInterface $cartRepository, |
| 59 | + RequestInterface $request, |
| 60 | + ResolverInterface $locale, |
| 61 | + RequestQuantityProcessor $quantityProcessor |
| 62 | + ) { |
25 | 63 | $this->checkoutSession = $checkoutSession;
|
| 64 | + $this->cartRepository = $cartRepository; |
| 65 | + $this->request = $request; |
| 66 | + $this->locale = $locale; |
| 67 | + $this->quantityProcessor = $quantityProcessor; |
26 | 68 | }
|
27 | 69 |
|
28 | 70 | /**
|
29 | 71 | * Map STEP_SELECT_ADDRESSES to Cart::CHECKOUT_STATE_BEGIN
|
30 |
| - * @param \Magento\Checkout\Model\Cart $subject |
| 72 | + * |
| 73 | + * @param CustomerCart $subject |
31 | 74 | * @return void
|
32 | 75 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
33 | 76 | */
|
34 |
| - public function beforeSave(\Magento\Checkout\Model\Cart $subject) |
| 77 | + public function beforeSave(CustomerCart $subject) |
35 | 78 | {
|
36 | 79 | if ($this->checkoutSession->getCheckoutState() === State::STEP_SELECT_ADDRESSES) {
|
37 |
| - $this->checkoutSession->setCheckoutState(\Magento\Checkout\Model\Session::CHECKOUT_STATE_BEGIN); |
| 80 | + $this->checkoutSession->setCheckoutState(Session::CHECKOUT_STATE_BEGIN); |
38 | 81 | }
|
39 | 82 | }
|
| 83 | + |
| 84 | + /** |
| 85 | + * Disable MultiShipping mode before saving Quote. |
| 86 | + * |
| 87 | + * @param CustomerCart $customerCart |
| 88 | + * @param CustomerCart $resultCart |
| 89 | + * @return CustomerCart $resultCart |
| 90 | + */ |
| 91 | + public function afterSave(CustomerCart $customerCart, CustomerCart $resultCart): CustomerCart |
| 92 | + { |
| 93 | + $isMultipleShippingAddressesPresent = $customerCart->getCheckoutSession()->getMultiShippingAddressesFlag(); |
| 94 | + if ($isMultipleShippingAddressesPresent) { |
| 95 | + $customerCart->getCheckoutSession()->setMultiShippingAddressesFlag(false); |
| 96 | + $params = $this->request->getParams(); |
| 97 | + if (isset($params['qty'])) { |
| 98 | + $this->recollectCartSummary($params['qty'], $resultCart); |
| 99 | + } |
| 100 | + } |
| 101 | + return $resultCart; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Recollect and recalculate cart summary data |
| 106 | + * |
| 107 | + * @param int|float|string|array $quantity |
| 108 | + * @param CustomerCart $cart |
| 109 | + */ |
| 110 | + private function recollectCartSummary($quantity, CustomerCart $cart): void |
| 111 | + { |
| 112 | + $quote = $cart->getQuote(); |
| 113 | + |
| 114 | + $filter = new \Zend_Filter_LocalizedToNormalized( |
| 115 | + ['locale' => $this->locale->getLocale()] |
| 116 | + ); |
| 117 | + $newQty = $this->quantityProcessor->prepareQuantity($quantity); |
| 118 | + $newQty = $filter->filter($newQty); |
| 119 | + |
| 120 | + $quote->setItemsQty($quote->getItemsQty() + $newQty); |
| 121 | + $quote->setTotalsCollectedFlag(false); |
| 122 | + $quote->collectTotals(); |
| 123 | + $this->cartRepository->save($quote); |
| 124 | + } |
40 | 125 | }
|
0 commit comments