Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartPlugin.php
Go to the documentation of this file.
1 <?php
7 
8 class CartPlugin
9 {
13  private $cartRepository;
14 
18  private $checkoutSession;
19 
23  private $addressRepository;
24 
30  public function __construct(
31  \Magento\Quote\Api\CartRepositoryInterface $cartRepository,
32  \Magento\Checkout\Model\Session $checkoutSession,
33  \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
34  ) {
35  $this->cartRepository = $cartRepository;
36  $this->checkoutSession = $checkoutSession;
37  $this->addressRepository = $addressRepository;
38  }
39 
46  public function beforeDispatch(
47  \Magento\Checkout\Controller\Cart $subject,
48  \Magento\Framework\App\RequestInterface $request
49  ) {
51  $quote = $this->checkoutSession->getQuote();
52 
53  // Clear shipping addresses and item assignments after MultiShipping flow
54  if ($quote->isMultipleShippingAddresses()) {
55  foreach ($quote->getAllShippingAddresses() as $address) {
56  $quote->removeAddress($address->getId());
57  }
58 
59  $shippingAddress = $quote->getShippingAddress();
60  $defaultShipping = $quote->getCustomer()->getDefaultShipping();
61  if ($defaultShipping) {
62  $defaultCustomerAddress = $this->addressRepository->getById(
63  $defaultShipping
64  );
65  $shippingAddress->importCustomerAddressData($defaultCustomerAddress);
66  }
67  $this->cartRepository->save($quote);
68  }
69  }
70 }
$addressRepository
$cartRepository
Definition: quote.php:18
$quote
$shippingAddress
Definition: order.php:40
$address
Definition: customer.php:38
__construct(\Magento\Quote\Api\CartRepositoryInterface $cartRepository, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository)
Definition: CartPlugin.php:30