storeManager = $storeManager; $this->config = $config; $this->quoteRepository = $quoteRepository; } /** * Set new customer group to all his quotes * * @param Observer $observer * @return void */ public function execute(Observer $observer) { /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $observer->getEvent()->getCustomerDataObject(); try { $quote = $this->quoteRepository->getForCustomer($customer->getId()); if ($customer->getGroupId() !== $quote->getCustomerGroupId()) { /** * It is needed to process customer's quotes for all websites * if customer accounts are shared between all of them */ /** @var $websites \Magento\Store\Model\Website[] */ $websites = $this->config->isWebsiteScope() ? [$this->storeManager->getWebsite($customer->getWebsiteId())] : $this->storeManager->getWebsites(); foreach ($websites as $website) { $quote->setWebsite($website); $quote->setCustomerGroupId($customer->getGroupId()); $quote->collectTotals(); $this->quoteRepository->save($quote); } } } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { } } }