/** * 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(); } } } }
/** * @return array */ public function toOptionArray() { if (!$this->_options) { $this->_options = array(); foreach ($this->_storeManager->getWebsites() as $website) { $id = $website->getId(); $name = $website->getName(); if ($id != 0) { $this->_options[] = array('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; }
/** * Get websites as id => name associative array * * @param bool $withDefault * @param string $attribute * @return array */ public function getWebsiteOptionHash($withDefault = false, $attribute = 'name') { $options = array(); foreach ($this->_storeManager->getWebsites((bool) $withDefault && $this->_isAdminScopeAllowed) as $website) { $options[$website->getId()] = $website->getDataUsingMethod($attribute); } return $options; }
/** * Retrieve website collection array * * @return array */ protected function _getWebsites() { if (is_null($this->_websites)) { try { $this->_websites = $this->_storeManager->getWebsites(); } catch (\Exception $e) { $this->_errors[] = $e->getMessage(); } } return $this->_websites; }
/** * Returns shared website Ids. * * @param int $websiteId the ID to use if website scope is on * @return int[] */ public function getSharedWebsiteIds($websiteId) { $ids = array(); if ($this->isWebsiteScope()) { $ids[] = $websiteId; } else { foreach ($this->_storeManager->getWebsites() as $website) { $ids[] = $website->getId(); } } return $ids; }
/** * 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()); } }
/** * Prepare website map * * @return array */ protected function _getWebsitesMap() { $map = array(); $websites = $this->_storeManager->getWebsites(true); foreach ($websites as $website) { $map[$website->getId()] = $website->getDefaultStore()->getId(); } return $map; }
/** * @param \Magento\Framework\StoreManagerInterface $storage * @param string $scopeCode * @return null|string */ protected function _getStoreByWebsite(\Magento\Framework\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()); }
/** * Generate catalog price rules prices for specified date range * If from date is not defined - will be used previous day by UTC * If to date is not defined - will be used next day by UTC * * @param int|string|null $fromDate * @param int|string|null $toDate * @param int $productId * @return $this * @throws \Exception */ public function applyAllRulesForDateRange($fromDate = null, $toDate = null, $productId = null) { $write = $this->_getWriteAdapter(); $write->beginTransaction(); $this->_eventManager->dispatch('catalogrule_before_apply', array('resource' => $this)); $clearOldData = false; if ($fromDate === null) { $fromDate = mktime(0, 0, 0, date('m'), date('d') - 1); /** * If fromDate not specified we can delete all data oldest than 1 day * We have run it for clear table in case when cron was not installed * and old data exist in table */ $clearOldData = true; } if (is_string($fromDate)) { $fromDate = strtotime($fromDate); } if ($toDate === null) { $toDate = mktime(0, 0, 0, date('m'), date('d') + 1); } if (is_string($toDate)) { $toDate = strtotime($toDate); } $product = null; if ($productId instanceof Product) { $product = $productId; $productId = $productId->getId(); } $this->removeCatalogPricesForDateRange($fromDate, $toDate, $productId); if ($clearOldData) { $this->deleteOldData($fromDate, $productId); } $dayPrices = array(); try { /** * 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($fromDate, $toDate, $productId, $website->getId()); $dayPrices = array(); $stopFlags = array(); $prevKey = null; while ($ruleData = $productsStmt->fetch()) { $ruleProductId = $ruleData['product_id']; $productKey = $ruleProductId . '_' . $ruleData['website_id'] . '_' . $ruleData['customer_group_id']; if ($prevKey && $prevKey != $productKey) { $stopFlags = array(); } /** * 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] = array('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; if (count($dayPrices) > 1000) { $this->_saveRuleProductPrices($dayPrices); $dayPrices = array(); } } $this->_saveRuleProductPrices($dayPrices); } $this->_saveRuleProductPrices($dayPrices); $write->delete($this->getTable('catalogrule_group_website'), array()); $timestamp = $this->_coreDate->gmtTimestamp(); $select = $write->select()->distinct(true)->from($this->getTable('catalogrule_product'), array('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); $write->commit(); } catch (\Exception $e) { $this->_logger->logException($e); $write->rollback(); throw $e; } $productCondition = $this->_conditionFactory->create()->setTable($this->getTable('catalogrule_affected_product'))->setPkFieldName('product_id'); $this->_eventManager->dispatch('catalogrule_after_apply', array('product' => $product, 'product_condition' => $productCondition)); $write->delete($this->getTable('catalogrule_affected_product')); return $this; }
/** * 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; }
/** * Retrieve shared website ids * * @return int[] */ public function getSharedWebsiteIds() { $ids = $this->_getData('shared_website_ids'); if ($ids === null) { $ids = array(); if ((bool) $this->getSharingConfig()->isWebsiteScope()) { $ids[] = $this->getWebsiteId(); } else { foreach ($this->_storeManager->getWebsites() as $website) { $ids[] = $website->getId(); } } $this->setData('shared_website_ids', $ids); } return $ids; }