/** * 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) { } } } }
/** * 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(); } } } }
protected function getWebsiteIds() { if (null == $this->websiteIds) { $websitesList = $this->storeManager->getWebsites(true); $this->websiteIds = array_keys($websitesList); } return $this->websiteIds; }
/** * Initialize website values. * * @return $this */ protected function _initWebsites() { /** @var $website \Magento\Store\Model\Website */ foreach ($this->storeManager->getWebsites() as $website) { $this->websiteCodeToId[$website->getCode()] = $website->getId(); $this->websiteCodeToStoreIds[$website->getCode()] = array_flip($website->getStoreCodes()); } return $this; }
/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return int|void */ protected function execute(InputInterface $input, OutputInterface $output) { if ($input->getOption('format') === null) { $this->writeSection($output, 'Magento Websites'); } foreach ($this->storeManager->getWebsites() as $website) { $table[$website->getId()] = array($website->getId(), $website->getCode()); } ksort($table); $this->getHelper('table')->setHeaders(array('id', 'code'))->renderByFormat($output, $table, $input->getOption('format')); }
/** * Gets store tree in a format easily walked over * for config path value comparison * * @return array */ public function getScopeTree() { $tree = array('websites' => array()); $websites = $this->storeManager->getWebsites(); /* @var $website Website */ foreach ($websites as $website) { $tree['websites'][$website->getId()] = array('stores' => array()); /* @var $store Store */ foreach ($website->getStores() as $store) { $tree['websites'][$website->getId()]['stores'][] = $store->getId(); } } return $tree; }
/** * Get data * * @return array */ public function getData() { if ($this->storeManager->isSingleStoreMode()) { $websites = $this->storeManager->getWebsites(); $singleStoreWebsite = array_shift($websites); $this->addFilter($this->filterBuilder->setField('store_website_id')->setValue($singleStoreWebsite->getId())->create()); $this->addFilter($this->filterBuilder->setField('store_group_id')->setConditionType('null')->create()); } $data = parent::getData(); foreach ($data['items'] as &$item) { $item += ['default' => __('Global')]; } return $data; }
/** * @return array */ public function toOptionArray() { if (!$this->_options) { $this->_options = []; foreach ($this->_storeManager->getWebsites() as $website) { $id = $website->getId(); $name = $website->getName(); if ($id != 0) { $this->_options[] = ['value' => $id, 'label' => $name]; } } } return $this->_options; }
/** * Initialize website values * * @param bool $withDefault * @return $this */ protected function _initWebsites($withDefault = false) { /** @var $website \Magento\Store\Model\Website */ foreach ($this->_storeManager->getWebsites($withDefault) as $website) { $this->_websiteCodeToId[$website->getCode()] = $website->getId(); } return $this; }
/** * Retrieve the entities for the given scope * * @param string $scope Scope * @return \Magento\Store\Api\Data\WebsiteInterface[]|\Magento\Store\Api\Data\StoreInterface[] */ private function getEntityStore($scope) { if (isset($this->entityStore[$scope])) { return $this->entityStore[$scope]; } switch ($scope) { case self::SCOPE_STORES: $this->entityStore[$scope] = $this->storeManager->getStores(true, true); break; case self::SCOPE_WEBSITES: $this->entityStore[$scope] = $this->storeManager->getWebsites(true, true); break; default: throw new ScopeConvertException(sprintf('Unknown scope "%s"', $scope)); } return $this->entityStore[$scope]; }
/** * Get websites as id => name associative array * * @param bool $withDefault * @param string $attribute * @return array */ public function getWebsiteOptionHash($withDefault = false, $attribute = 'name') { $options = []; foreach ($this->_storeManager->getWebsites((bool) $withDefault && $this->_isAdminScopeAllowed) as $website) { $options[$website->getId()] = $website->getDataUsingMethod($attribute); } return $options; }
/** * Loading and caching of default website, store and store view * * @return bool */ protected function loadStore() { $isLoaded = true; if (!$this->website) { $isLoaded = false; $websites = $this->storeManager->getWebsites(); foreach ($websites as $website) { if ($website->getIsDefault()) { $this->website = $website; $this->group = $website->getDefaultGroup(); $this->store = $website->getDefaultStore(); $isLoaded = true; break; } } } return $isLoaded; }
/** * @return Page */ public function execute() { if (!$this->getSelectedStore()) { // If we are not under a store view, then redirect to the first // found one. Nosto is configured per store. foreach ($this->_storeManager->getWebsites() as $website) { $storeId = $website->getDefaultGroup()->getDefaultStoreId(); if (!empty($storeId)) { return $this->resultRedirectFactory->create()->setPath('*/*/index', ['store' => $storeId]); } } } /** @var Page $result */ $result = $this->_resultPageFactory->create(); $result->setActiveMenu(self::ADMIN_RESOURCE); $result->getConfig()->getTitle()->prepend(__('Nosto - Account Settings')); return $result; }
/** * @param Product|null $product * @throws \Exception * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function applyAllRules(Product $product = null) { $write = $this->getWriteAdapter(); $fromDate = mktime(0, 0, 0, date('m'), date('d') - 1); $toDate = mktime(0, 0, 0, date('m'), date('d') + 1); $productId = $product ? $product->getId() : null; /** * Update products rules prices per each website separately * because of max join limit in mysql */ foreach ($this->storeManager->getWebsites(false) as $website) { $productsStmt = $this->getRuleProductsStmt($website->getId(), $productId); $dayPrices = []; $stopFlags = []; $prevKey = null; while ($ruleData = $productsStmt->fetch()) { $ruleProductId = $ruleData['product_id']; $productKey = $ruleProductId . '_' . $ruleData['website_id'] . '_' . $ruleData['customer_group_id']; if ($prevKey && $prevKey != $productKey) { $stopFlags = []; if (count($dayPrices) > $this->batchCount) { $this->saveRuleProductPrices($dayPrices); $dayPrices = []; } } /** * Build prices for each day */ for ($time = $fromDate; $time <= $toDate; $time += self::SECONDS_IN_DAY) { if (($ruleData['from_time'] == 0 || $time >= $ruleData['from_time']) && ($ruleData['to_time'] == 0 || $time <= $ruleData['to_time'])) { $priceKey = $time . '_' . $productKey; if (isset($stopFlags[$priceKey])) { continue; } if (!isset($dayPrices[$priceKey])) { $dayPrices[$priceKey] = ['rule_date' => $time, 'website_id' => $ruleData['website_id'], 'customer_group_id' => $ruleData['customer_group_id'], 'product_id' => $ruleProductId, 'rule_price' => $this->calcRuleProductPrice($ruleData), 'latest_start_date' => $ruleData['from_time'], 'earliest_end_date' => $ruleData['to_time']]; } else { $dayPrices[$priceKey]['rule_price'] = $this->calcRuleProductPrice($ruleData, $dayPrices[$priceKey]); $dayPrices[$priceKey]['latest_start_date'] = max($dayPrices[$priceKey]['latest_start_date'], $ruleData['from_time']); $dayPrices[$priceKey]['earliest_end_date'] = min($dayPrices[$priceKey]['earliest_end_date'], $ruleData['to_time']); } if ($ruleData['action_stop']) { $stopFlags[$priceKey] = true; } } } $prevKey = $productKey; } $this->saveRuleProductPrices($dayPrices); } $write->delete($this->getTable('catalogrule_group_website'), []); $timestamp = $this->dateTime->gmtTimestamp(); $select = $write->select()->distinct(true)->from($this->getTable('catalogrule_product'), ['rule_id', 'customer_group_id', 'website_id'])->where("{$timestamp} >= from_time AND (({$timestamp} <= to_time AND to_time > 0) OR to_time = 0)"); $query = $select->insertFromSelect($this->getTable('catalogrule_group_website')); $write->query($query); return $this; }
/** * @param ResultCollection $results * @param $checkGroupClass * @param $check */ private function checkWebsites(ResultCollection $results, $checkGroupClass, $check) { if (!($websites = $this->storeManager->getWebsites())) { $this->_markCheckWarning($results, 'websites', $checkGroupClass); } foreach ($websites as $website) { $check->check($results, $website); } }
/** * Check if the given website id is a valid website id * * @param int $websiteId Website ID * @return bool */ private function isValidWebsiteId($websiteId) { $websites = $this->storeManager->getWebsites(); if (array_key_exists($websiteId, $websites)) { return true; } if (is_numeric($websiteId)) { // Dont bother checking website codes on numeric input return false; } // @todo hs: build up array of websiteCodes, to prevent wasting time looping /** @var Website $website */ foreach ($websites as $website) { if ($website->getCode() == $websiteId) { return true; } } return false; }
/** * @param \Magento\Catalog\Model\Product $product * @return void */ private function assignProductToWebsites(\Magento\Catalog\Model\Product $product) { if (!$this->storeManager->hasSingleStore()) { if ($this->storeManager->getStore()->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) { $websiteIds = array_keys($this->storeManager->getWebsites()); } else { $websiteIds = [$this->storeManager->getStore()->getWebsiteId()]; } $product->setWebsiteIds(array_unique(array_merge($product->getWebsiteIds(), $websiteIds))); } }
/** * Retrieve website collection array * * @return array */ protected function _getWebsites() { if ($this->_websites === null) { try { $this->_websites = $this->_storeManager->getWebsites(); } catch (\Exception $e) { $this->_errors[] = $e->getMessage(); } } return $this->_websites; }
/** * @inheritdoc */ public function get() { $defaultScope = ['scope' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 'scope_id' => null, 'scopes' => []]; /** @var Website $website */ foreach ($this->storeManager->getWebsites() as $website) { $websiteScope = ['scope' => ScopeInterface::SCOPE_WEBSITES, 'scope_id' => $website->getId(), 'scopes' => []]; /** @var Group $group */ foreach ($website->getGroups() as $group) { $groupScope = ['scope' => ScopeInterface::SCOPE_GROUP, 'scope_id' => $group->getId(), 'scopes' => []]; /** @var Store $store */ foreach ($group->getStores() as $store) { $storeScope = ['scope' => ScopeInterface::SCOPE_STORES, 'scope_id' => $store->getId(), 'scopes' => []]; $groupScope['scopes'][] = $storeScope; } $websiteScope['scopes'][] = $groupScope; } $defaultScope['scopes'][] = $websiteScope; } return $defaultScope; }
/** * @param string $scopeType * * @return array */ private function getScopesByCode($scopeType) { $scopesByType = []; if ($scopeType == ScopeInterface::SCOPE_STORES) { $scopesByType = $this->storeManager->getStores(false, true); } if ($scopeType == ScopeInterface::SCOPE_WEBSITES) { $scopesByType = $this->storeManager->getWebsites(false, true); } return $scopesByType; }
/** * Retrieve correct scope corresponding single store mode configuration * * @param string $scope * @param string $scopeId * @return array */ protected function getCorrectScope($scope, $scopeId) { $isSingleStoreMode = $this->storeManager->isSingleStoreMode(); if ($isSingleStoreMode) { $websites = $this->storeManager->getWebsites(); $singleStoreWebsite = array_shift($websites); $scope = ScopeInterface::SCOPE_WEBSITES; $scopeId = $singleStoreWebsite->getId(); } return ['scope' => $scope, 'scopeId' => $scopeId]; }
/** * Returns shared website Ids. * * @param int $websiteId the ID to use if website scope is on * @return int[] */ public function getSharedWebsiteIds($websiteId) { $ids = []; if ($this->isWebsiteScope()) { $ids[] = $websiteId; } else { foreach ($this->_storeManager->getWebsites() as $website) { $ids[] = $website->getId(); } } return $ids; }
/** * @param Product|null $product * @throws \Exception * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function applyAllRules(Product $product = null) { $fromDate = mktime(0, 0, 0, date('m'), date('d') - 1); $toDate = mktime(0, 0, 0, date('m'), date('d') + 1); /** * Update products rules prices per each website separately * because of max join limit in mysql */ foreach ($this->storeManager->getWebsites() as $website) { $productsStmt = $this->getRuleProductsStmt($website->getId(), $product); $dayPrices = []; $stopFlags = []; $prevKey = null; while ($ruleData = $productsStmt->fetch()) { $ruleProductId = $ruleData['product_id']; $productKey = $ruleProductId . '_' . $ruleData['website_id'] . '_' . $ruleData['customer_group_id']; if ($prevKey && $prevKey != $productKey) { $stopFlags = []; if (count($dayPrices) > $this->batchCount) { $this->saveRuleProductPrices($dayPrices); $dayPrices = []; } } $ruleData['from_time'] = $this->roundTime($ruleData['from_time']); $ruleData['to_time'] = $this->roundTime($ruleData['to_time']); /** * Build prices for each day */ for ($time = $fromDate; $time <= $toDate; $time += self::SECONDS_IN_DAY) { if (($ruleData['from_time'] == 0 || $time >= $ruleData['from_time']) && ($ruleData['to_time'] == 0 || $time <= $ruleData['to_time'])) { $priceKey = $time . '_' . $productKey; if (isset($stopFlags[$priceKey])) { continue; } if (!isset($dayPrices[$priceKey])) { $dayPrices[$priceKey] = ['rule_date' => $time, 'website_id' => $ruleData['website_id'], 'customer_group_id' => $ruleData['customer_group_id'], 'product_id' => $ruleProductId, 'rule_price' => $this->calcRuleProductPrice($ruleData), 'latest_start_date' => $ruleData['from_time'], 'earliest_end_date' => $ruleData['to_time']]; } else { $dayPrices[$priceKey]['rule_price'] = $this->calcRuleProductPrice($ruleData, $dayPrices[$priceKey]); $dayPrices[$priceKey]['latest_start_date'] = max($dayPrices[$priceKey]['latest_start_date'], $ruleData['from_time']); $dayPrices[$priceKey]['earliest_end_date'] = min($dayPrices[$priceKey]['earliest_end_date'], $ruleData['to_time']); } if ($ruleData['action_stop']) { $stopFlags[$priceKey] = true; } } } $prevKey = $productKey; } $this->saveRuleProductPrices($dayPrices); } return $this->updateCatalogRuleGroupWebsiteData(); }
/** * Set correct scope if isSingleStoreMode = true * * @param \Magento\Backend\Model\Config\Structure\Element\Field $fieldConfig * @param \Magento\Framework\App\Config\ValueInterface $dataObject * @return void */ protected function _checkSingleStoreMode(\Magento\Backend\Model\Config\Structure\Element\Field $fieldConfig, $dataObject) { $isSingleStoreMode = $this->_storeManager->isSingleStoreMode(); if (!$isSingleStoreMode) { return; } if (!$fieldConfig->showInDefault()) { $websites = $this->_storeManager->getWebsites(); $singleStoreWebsite = array_shift($websites); $dataObject->setScope('websites'); $dataObject->setWebsiteCode($singleStoreWebsite->getCode()); $dataObject->setScopeCode($singleStoreWebsite->getCode()); $dataObject->setScopeId($singleStoreWebsite->getId()); } }
/** * Get websites list * * @return array */ protected function getWebsites() { $websites = [['label' => __('All Websites') . ' [' . $this->directoryHelper->getBaseCurrencyCode() . ']', 'value' => 0]]; $product = $this->locator->getProduct(); if (!$this->isScopeGlobal() && $product->getStoreId()) { /** @var \Magento\Store\Model\Website $website */ $website = $this->getStore()->getWebsite(); $websites[] = ['label' => $website->getName() . '[' . $website->getBaseCurrencyCode() . ']', 'value' => $website->getId()]; } elseif (!$this->isScopeGlobal()) { $websitesList = $this->storeManager->getWebsites(); $productWebsiteIds = $product->getWebsiteIds(); foreach ($websitesList as $website) { /** @var \Magento\Store\Model\Website $website */ if (!in_array($website->getId(), $productWebsiteIds)) { continue; } $websites[] = ['label' => $website->getName() . '[' . $website->getBaseCurrencyCode() . ']', 'value' => $website->getId()]; } } return $websites; }
/** * Get all websites. * * @param bool|false $default * * @return \Magento\Store\Api\Data\WebsiteInterface[] */ public function getWebsites($default = false) { return $this->storeManager->getWebsites($default); }
/** * @param \Magento\Store\Model\StoreManagerInterface $storage * @param string $scopeCode * @return null|string */ protected function _getStoreByWebsite(\Magento\Store\Model\StoreManagerInterface $storage, $scopeCode) { $websites = $storage->getWebsites(true, true); if (!isset($websites[$scopeCode])) { return null; } if (!$websites[$scopeCode]->getDefaultGroupId()) { return null; } return $this->_getStoreByGroup($storage, $websites[$scopeCode]->getDefaultGroupId()); }
/** * Prepare website map * * @return array */ protected function _getWebsitesMap() { $map = []; $websites = $this->_storeManager->getWebsites(); foreach ($websites as $website) { // Continue if website has no store to be able to create catalog rule for website without store if ($website->getDefaultStore() === null) { continue; } $map[$website->getId()] = $website->getDefaultStore()->getId(); } return $map; }
/** * Retrieve a list of available websites * * @return \Magento\Store\Model\Website[] */ public function getScopes() { return $this->_storeManager->getWebsites(); }
/** * Prepare website map * * @return array */ protected function _getWebsitesMap() { $map = []; $websites = $this->_storeManager->getWebsites(true); foreach ($websites as $website) { $map[$website->getId()] = $website->getDefaultStore()->getId(); } return $map; }