/** * Set new customer group to all his quotes * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var CustomerData $customerDataObject */ $customerDataObject = $observer->getEvent()->getCustomerDataObject(); /** @var CustomerData $origCustomerDataObject */ $origCustomerDataObject = $observer->getEvent()->getOrigCustomerDataObject(); if ($customerDataObject->getGroupId() !== $origCustomerDataObject->getGroupId()) { /** * 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() ? array($this->_storeManager->getWebsite($customerDataObject->getWebsiteId())) : $this->_storeManager->getWebsites(); foreach ($websites as $website) { $quote = $this->_quoteFactory->create(); $quote->setWebsite($website); $quote->loadByCustomer($customerDataObject->getId()); if ($quote->getId()) { $quote->setCustomerGroupId($customerDataObject->getGroupId()); $quote->collectTotals(); $quote->save(); } } } }
/** * Set new customer group to all his quotes * * @param Observer $observer * @return void */ public function dispatch(Observer $observer) { /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $observer->getEvent()->getCustomerDataObject(); /** @var \Magento\Customer\Api\Data\CustomerInterface $origCustomer */ $origCustomer = $observer->getEvent()->getOrigCustomerDataObject(); if ($customer->getGroupId() !== $origCustomer->getGroupId()) { /** * 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) { try { $quote = $this->quoteRepository->getForCustomer($customer->getId()); $quote->setWebsite($website); $quote->setCustomerGroupId($customer->getGroupId()); $quote->collectTotals(); $this->quoteRepository->save($quote); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { } } } }
/** * @param \Magento\Customer\Model\Config\Share $configShare * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param string $namespace * @param array $data */ public function __construct(\Magento\Customer\Model\Config\Share $configShare, \Magento\Store\Model\StoreManagerInterface $storeManager, $namespace = 'customer', array $data = []) { if ($configShare->isWebsiteScope()) { $namespace .= '_' . $storeManager->getWebsite()->getCode(); } parent::__construct($namespace, $data); }
/** * {@inheritDoc} */ public function isCustomerInStore($customerWebsiteId, $storeId) { $ids = []; if ((bool) $this->configShare->isWebsiteScope()) { $ids = $this->storeManager->getWebsite($customerWebsiteId)->getStoreIds(); } else { foreach ($this->storeManager->getStores() as $store) { $ids[] = $store->getId(); } } return in_array($storeId, $ids); }