Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerQuoteObserver.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Customer\Model\Config\Share as ShareConfig;
13 
18 {
22  protected $config;
23 
27  protected $storeManager;
28 
32  protected $quoteRepository;
33 
39  public function __construct(
41  ShareConfig $config,
43  ) {
44  $this->storeManager = $storeManager;
45  $this->config = $config;
46  $this->quoteRepository = $quoteRepository;
47  }
48 
55  public function execute(Observer $observer)
56  {
58  $customer = $observer->getEvent()->getCustomerDataObject();
59  try {
60  $quote = $this->quoteRepository->getForCustomer($customer->getId());
61  if ($customer->getGroupId() !== $quote->getCustomerGroupId()) {
67  $websites = $this->config->isWebsiteScope()
68  ? [$this->storeManager->getWebsite($customer->getWebsiteId())]
69  : $this->storeManager->getWebsites();
70 
71  foreach ($websites as $website) {
72  $quote->setWebsite($website);
73  $quote->setCustomerGroupId($customer->getGroupId());
74  $quote->collectTotals();
75  $this->quoteRepository->save($quote);
76  }
77  }
78  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
79  }
80  }
81 }
$customer
Definition: customers.php:11
$quote
__construct(StoreManagerInterface $storeManager, ShareConfig $config, CartRepositoryInterface $quoteRepository)