Ejemplo n.º 1
0
 /**
  * @return int
  */
 protected function getCustomerGroupId()
 {
     if ($this->product->getCustomerGroupId()) {
         return (int) $this->product->getCustomerGroupId();
     }
     return (int) $this->customerSession->getCustomerGroupId();
 }
 /**
  * Apply catalog price rules to product on frontend
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $product = $observer->getEvent()->getProduct();
     $pId = $product->getId();
     $storeId = $product->getStoreId();
     if ($observer->hasDate()) {
         $date = new \DateTime($observer->getEvent()->getDate());
     } else {
         $date = $this->localeDate->scopeDate($storeId);
     }
     if ($observer->hasWebsiteId()) {
         $wId = $observer->getEvent()->getWebsiteId();
     } else {
         $wId = $this->storeManager->getStore($storeId)->getWebsiteId();
     }
     if ($observer->hasCustomerGroupId()) {
         $gId = $observer->getEvent()->getCustomerGroupId();
     } elseif ($product->hasCustomerGroupId()) {
         $gId = $product->getCustomerGroupId();
     } else {
         $gId = $this->customerSession->getCustomerGroupId();
     }
     $key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}";
     if (!$this->rulePricesStorage->hasRulePrice($key)) {
         $rulePrice = $this->resourceRuleFactory->create()->getRulePrice($date, $wId, $gId, $pId);
         $this->rulePricesStorage->setRulePrice($key, $rulePrice);
     }
     if ($this->rulePricesStorage->getRulePrice($key) !== false) {
         $finalPrice = min($product->getData('final_price'), $this->rulePricesStorage->getRulePrice($key));
         $product->setFinalPrice($finalPrice);
     }
     return $this;
 }
 /**
  * Before generate Xml
  *
  * @param \Magento\Framework\View\LayoutInterface $subject
  * @return array
  */
 public function beforeGenerateXml(\Magento\Framework\View\LayoutInterface $subject)
 {
     if ($this->depersonalizeChecker->checkIfDepersonalize($subject)) {
         $this->customerGroupId = $this->customerSession->getCustomerGroupId();
         $this->formKey = $this->session->getData(\Magento\Framework\Data\Form\FormKey::FORM_KEY);
     }
     return [];
 }
Ejemplo n.º 4
0
 /**
  * Returns catalog rule value
  *
  * @return float|boolean
  */
 public function getValue()
 {
     if (null === $this->value) {
         $this->value = $this->resourceRuleFactory->create()->getRulePrice($this->dateTime->scopeTimeStamp($this->storeManager->getStore()->getId()), $this->storeManager->getStore()->getWebsiteId(), $this->customerSession->getCustomerGroupId(), $this->product->getId());
         $this->value = $this->value ? floatval($this->value) : false;
     }
     return $this->value;
 }
 /**
  * {@inheritdoc}
  */
 public function build($productId)
 {
     $timestamp = $this->localeDate->scopeTimeStamp($this->storeManager->getStore());
     $currentDate = $this->dateTime->formatDate($timestamp, false);
     $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
     $productTable = $this->resource->getTableName('catalog_product_entity');
     return [$this->resource->getConnection()->select()->from(['parent' => $productTable], '')->joinInner(['link' => $this->resource->getTableName('catalog_product_relation')], "link.parent_id = parent.{$linkField}", [])->joinInner(['child' => $productTable], "child.entity_id = link.child_id", ['entity_id'])->joinInner(['t' => $this->resource->getTableName('catalogrule_product_price')], 't.product_id = child.entity_id', [])->where('parent.entity_id = ? ', $productId)->where('t.website_id = ?', $this->storeManager->getStore()->getWebsiteId())->where('t.customer_group_id = ?', $this->customerSession->getCustomerGroupId())->where('t.rule_date = ?', $currentDate)->order('t.rule_price ' . Select::SQL_ASC)->limit(1)];
 }
Ejemplo n.º 6
0
 /**
  * @param Collection $productCollection
  * @param bool $printQuery
  * @param bool $logQuery
  * @return array
  */
 public function beforeLoad(Collection $productCollection, $printQuery = false, $logQuery = false)
 {
     if (!$productCollection->hasFlag('catalog_rule_loaded')) {
         $connection = $this->resource->getConnection();
         $store = $this->storeManager->getStore();
         $productCollection->getSelect()->joinLeft(['catalog_rule' => $this->resource->getTableName('catalogrule_product_price')], implode(' AND ', ['catalog_rule.product_id = e.entity_id', $connection->quoteInto('catalog_rule.website_id = ?', $store->getWebsiteId()), $connection->quoteInto('catalog_rule.customer_group_id = ?', $this->customerSession->getCustomerGroupId()), $connection->quoteInto('catalog_rule.rule_date = ?', $this->dateTime->formatDate($this->localeDate->scopeDate($store->getId()), false))]), [CatalogRulePrice::PRICE_CODE => 'rule_price']);
         $productCollection->setFlag('catalog_rule_loaded', true);
     }
     return [$printQuery, $logQuery];
 }
Ejemplo n.º 7
0
 /**
  * @param Product $saleableItem
  * @param float $quantity
  * @param CalculatorInterface $calculator
  * @param Session $customerSession
  */
 public function __construct(Product $saleableItem, $quantity, CalculatorInterface $calculator, Session $customerSession)
 {
     $quantity = $quantity ?: 1;
     parent::__construct($saleableItem, $quantity, $calculator);
     $this->customerSession = $customerSession;
     if ($saleableItem->hasCustomerGroupId()) {
         $this->customerGroup = (int) $saleableItem->getCustomerGroupId();
     } else {
         $this->customerGroup = (int) $this->customerSession->getCustomerGroupId();
     }
 }
Ejemplo n.º 8
0
 public function testGetCustomerMissingCustomer()
 {
     $moduleManager = $this->objectManager->get('Magento\\Framework\\Module\\Manager');
     if ($moduleManager->isEnabled('Magento_PageCache')) {
         $customerDataFactory = $this->objectManager->create('Magento\\Customer\\Api\\Data\\CustomerInterfaceFactory');
         $customerData = $customerDataFactory->create()->setGroupId($this->_customerSession->getCustomerGroupId());
         $this->assertEquals($customerData, $this->_block->getCustomer());
     } else {
         $this->assertNull($this->_block->getCustomer());
     }
 }
 /**
  * @magentoAppArea frontend
  * @magentoConfigFixture current_store persistent/options/shopping_cart 1
  * @magentoConfigFixture current_store persistent/options/logout_clear 0
  * @magentoConfigFixture current_store persistent/options/enabled 1
  */
 public function testEmulateCustomer()
 {
     $observer = new \Magento\Framework\Event\Observer();
     $this->_customerSession->loginById(1);
     $this->_customerSession->logout();
     $this->assertNull($this->_customerSession->getCustomerId());
     $this->assertEquals(\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID, $this->_customerSession->getCustomerGroupId());
     $this->_observer->execute($observer);
     $customer = $this->customerRepository->getById($this->_persistentSessionHelper->getSession()->getCustomerId());
     $this->assertEquals($customer->getId(), $this->_customerSession->getCustomerId());
     $this->assertEquals($customer->getGroupId(), $this->_customerSession->getCustomerGroupId());
 }
Ejemplo n.º 10
0
 /**
  * @param Product $saleableItem
  * @param float $quantity
  * @param CalculatorInterface $calculator
  * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  * @param Session $customerSession
  * @param GroupManagementInterface $groupManagement
  */
 public function __construct(Product $saleableItem, $quantity, CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, Session $customerSession, GroupManagementInterface $groupManagement)
 {
     $quantity = $quantity ?: 1;
     parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
     $this->customerSession = $customerSession;
     $this->groupManagement = $groupManagement;
     if ($saleableItem->hasCustomerGroupId()) {
         $this->customerGroup = (int) $saleableItem->getCustomerGroupId();
     } else {
         $this->customerGroup = (int) $this->customerSession->getCustomerGroupId();
     }
 }
Ejemplo n.º 11
0
 /**
  * @magentoAppArea frontend
  * @magentoConfigFixture current_store persistent/options/shopping_cart 1
  * @magentoConfigFixture current_store persistent/options/logout_clear 0
  * @magentoConfigFixture current_store persistent/options/enabled 1
  */
 public function testEmulateCustomer()
 {
     $observer = new \Magento\Framework\Event\Observer();
     $this->_customerSession->loginById(1);
     $this->_customerSession->logout();
     $this->assertNull($this->_customerSession->getCustomerId());
     $this->assertEquals(\Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID, $this->_customerSession->getCustomerGroupId());
     $this->_observer->execute($observer);
     $customer = $this->_customerAccountService->getCustomer($this->_persistentSessionHelper->getSession()->getCustomerId());
     $this->assertEquals($customer->getId(), $this->_customerSession->getCustomerId());
     $this->assertEquals($customer->getGroupId(), $this->_customerSession->getCustomerGroupId());
 }
 /**
  * {@inheritdoc}
  */
 public function build($productId)
 {
     $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
     $productTable = $this->resource->getTableName('catalog_product_entity');
     $priceSelect = $this->resource->getConnection()->select()->from(['parent' => $productTable], '')->joinInner(['link' => $this->resource->getTableName('catalog_product_relation')], "link.parent_id = parent.{$linkField}", [])->joinInner(['child' => $productTable], "child.entity_id = link.child_id", ['entity_id'])->joinInner(['t' => $this->resource->getTableName('catalog_product_entity_tier_price')], "t.{$linkField} = child.{$linkField}", [])->where('parent.entity_id = ? ', $productId)->where('t.all_groups = 1 OR customer_group_id = ?', $this->customerSession->getCustomerGroupId())->where('t.qty = ?', 1)->order('t.value ' . Select::SQL_ASC)->limit(1);
     $priceSelectDefault = clone $priceSelect;
     $priceSelectDefault->where('t.website_id = ?', self::DEFAULT_WEBSITE_ID);
     $select[] = $priceSelectDefault;
     if (!$this->catalogHelper->isPriceGlobal()) {
         $priceSelect->where('t.website_id = ?', $this->storeManager->getStore()->getWebsiteId());
         $select[] = $priceSelect;
     }
     return $select;
 }
Ejemplo n.º 13
0
 /**
  * Returns catalog rule value
  *
  * @return float|boolean
  */
 public function getValue()
 {
     if (null === $this->value) {
         if ($this->product->hasData(self::PRICE_CODE)) {
             $this->value = floatval($this->product->getData(self::PRICE_CODE)) ?: false;
         } else {
             $this->value = $this->getRuleResource()->getRulePrice($this->dateTime->scopeDate($this->storeManager->getStore()->getId()), $this->storeManager->getStore()->getWebsiteId(), $this->customerSession->getCustomerGroupId(), $this->product->getId());
             $this->value = $this->value ? floatval($this->value) : false;
             if ($this->value) {
                 $this->value = $this->priceCurrency->convertAndRound($this->value);
             }
         }
     }
     return $this->value;
 }
Ejemplo n.º 14
0
 /**
  * Getter for customer group id, return current customer group if not set
  *
  * @return int
  */
 public function getCustomerGroupId()
 {
     if ($this->customerGroupId === null) {
         return $this->customerSession->getCustomerGroupId();
     }
     return $this->customerGroupId;
 }
Ejemplo n.º 15
0
 /**
  * @param Product $product
  * @return int
  */
 protected function _getCustomerGroupId($product)
 {
     if ($product->getCustomerGroupId() !== null) {
         return $product->getCustomerGroupId();
     }
     return $this->_customerSession->getCustomerGroupId();
 }
Ejemplo n.º 16
0
 /**
  * {@inheritDoc}
  */
 public function addFacetToCollection($config = [])
 {
     $facetField = $this->getFilterField();
     $facetType = BucketInterface::TYPE_HISTOGRAM;
     $customerGroupId = $this->customerSession->getCustomerGroupId();
     $facetConfig = ['nestedFilter' => ['price.customer_group_id' => $customerGroupId], 'minDocCount' => 1];
     $calculation = $this->dataProvider->getRangeCalculationValue();
     if ($calculation === \Magento\Catalog\Model\Layer\Filter\DataProvider\Price::RANGE_CALCULATION_MANUAL) {
         if ((int) $this->dataProvider->getRangeStepValue() > 0) {
             $facetConfig['interval'] = (int) $this->dataProvider->getRangeStepValue();
         }
     }
     $productCollection = $this->getLayer()->getProductCollection();
     $productCollection->addFacet($facetField, $facetType, $facetConfig);
     return $this;
 }
Ejemplo n.º 17
0
 /**
  * Retrieve clean select with joined price index table
  *
  * @return \Magento\Framework\DB\Select
  */
 protected function _getSelect()
 {
     $collection = $this->layer->getProductCollection();
     $collection->addPriceData($this->session->getCustomerGroupId(), $this->storeManager->getStore()->getWebsiteId());
     if ($collection->getCatalogPreparedSelect() !== null) {
         $select = clone $collection->getCatalogPreparedSelect();
     } else {
         $select = clone $collection->getSelect();
     }
     // reset columns, order and limitation conditions
     $select->reset(\Magento\Framework\DB\Select::COLUMNS);
     $select->reset(\Magento\Framework\DB\Select::ORDER);
     $select->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
     $select->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
     // remove join with main table
     $fromPart = $select->getPart(\Magento\Framework\DB\Select::FROM);
     if (!isset($fromPart[\Magento\Catalog\Model\ResourceModel\Product\Collection::INDEX_TABLE_ALIAS]) || !isset($fromPart[\Magento\Catalog\Model\ResourceModel\Product\Collection::MAIN_TABLE_ALIAS])) {
         return $select;
     }
     // processing FROM part
     $priceIndexJoinPart = $fromPart[\Magento\Catalog\Model\ResourceModel\Product\Collection::INDEX_TABLE_ALIAS];
     $priceIndexJoinConditions = explode('AND', $priceIndexJoinPart['joinCondition']);
     $priceIndexJoinPart['joinType'] = \Magento\Framework\DB\Select::FROM;
     $priceIndexJoinPart['joinCondition'] = null;
     $fromPart[\Magento\Catalog\Model\ResourceModel\Product\Collection::MAIN_TABLE_ALIAS] = $priceIndexJoinPart;
     unset($fromPart[\Magento\Catalog\Model\ResourceModel\Product\Collection::INDEX_TABLE_ALIAS]);
     $select->setPart(\Magento\Framework\DB\Select::FROM, $fromPart);
     foreach ($fromPart as $key => $fromJoinItem) {
         $fromPart[$key]['joinCondition'] = $this->_replaceTableAlias($fromJoinItem['joinCondition']);
     }
     $select->setPart(\Magento\Framework\DB\Select::FROM, $fromPart);
     // processing WHERE part
     $wherePart = $select->getPart(\Magento\Framework\DB\Select::WHERE);
     foreach ($wherePart as $key => $wherePartItem) {
         $wherePart[$key] = $this->_replaceTableAlias($wherePartItem);
     }
     $select->setPart(\Magento\Framework\DB\Select::WHERE, $wherePart);
     $excludeJoinPart = \Magento\Catalog\Model\ResourceModel\Product\Collection::MAIN_TABLE_ALIAS . '.entity_id';
     foreach ($priceIndexJoinConditions as $condition) {
         if (strpos($condition, $excludeJoinPart) !== false) {
             continue;
         }
         $select->where($this->_replaceTableAlias($condition));
     }
     $select->where($this->_getPriceExpression($select) . ' IS NOT NULL');
     return $select;
 }
Ejemplo n.º 18
0
 /**
  * Before generate Xml
  *
  * @param \Magento\Framework\View\LayoutInterface $subject
  * @return array
  */
 public function beforeGenerateXml(\Magento\Framework\View\LayoutInterface $subject)
 {
     if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && !$this->request->isAjax() && $subject->isCacheable()) {
         $this->customerGroupId = $this->customerSession->getCustomerGroupId();
         $this->formKey = $this->session->getData(\Magento\Framework\Data\Form\FormKey::FORM_KEY);
     }
     return array();
 }
Ejemplo n.º 19
0
 /**
  * Retrieve active customer group id
  *
  * @return int
  */
 public function getCustomerGroupId()
 {
     $customerGroupId = $this->_getData('customer_group_id');
     if (is_null($customerGroupId)) {
         $customerGroupId = $this->_customerSession->getCustomerGroupId();
     }
     return $customerGroupId;
 }
Ejemplo n.º 20
0
 /**
  * {@inheritdoc}
  */
 public function getDataSet(BucketInterface $bucket, array $dimensions)
 {
     $currentScope = $dimensions['scope']->getValue();
     $attribute = $this->eavConfig->getAttribute(Product::ENTITY, $bucket->getField());
     if ($attribute->getAttributeCode() == 'price') {
         /** @var \Magento\Store\Model\Store $store */
         $store = $this->scopeResolver->getScope($currentScope);
         if (!$store instanceof \Magento\Store\Model\Store) {
             throw new \RuntimeException('Illegal scope resolved');
         }
         $table = $this->resource->getTableName('catalog_product_index_price');
         $select = $this->getSelect();
         $select->from(['main_table' => $table], null)->columns([BucketInterface::FIELD_VALUE => 'main_table.min_price'])->where('main_table.customer_group_id = ?', $this->customerSession->getCustomerGroupId())->where('main_table.website_id = ?', $store->getWebsiteId());
     } else {
         $currentScopeId = $this->scopeResolver->getScope($currentScope)->getId();
         $select = $this->getSelect();
         $table = $this->resource->getTableName('catalog_product_index_eav' . ($attribute->getBackendType() == 'decimal' ? '_decimal' : ''));
         $select->from(['main_table' => $table], ['value'])->where('main_table.attribute_id = ?', $attribute->getAttributeId())->where('main_table.store_id = ? ', $currentScopeId);
     }
     return $select;
 }
Ejemplo n.º 21
0
 /**
  * @param Product $product
  * @return int
  */
 protected function _getDiscountPercentForProduct($product)
 {
     $website = $this->_storeManager->getStore()->getWebsiteId();
     $group = $this->_customerSession->getCustomerGroupId();
     $key = implode('-', array($website, $group, $product->getId()));
     if (!isset($this->_productDiscounts[$key])) {
         $this->_productDiscounts[$key] = (int) $this->getResource()->getProductDiscountPercent($product->getId(), $website, $group);
     }
     if ($value = $this->_productDiscounts[$key]) {
         return 100 - min(100, max(0, $value));
     } else {
         return 0;
     }
 }
Ejemplo n.º 22
0
 /**
  * Set Customer Data Layer
  */
 protected function setCustomerDataLayer()
 {
     $customer = [];
     if ($this->_customerSession->isLoggedIn()) {
         $customer['isLoggedIn'] = true;
         $customer['id'] = $this->_customerSession->getCustomerId();
         $customer['groupId'] = $this->_customerSession->getCustomerGroupId();
         //$customer['groupCode'] = ;
     } else {
         $customer['isLoggedIn'] = false;
     }
     $this->addVariable('customer', $customer);
     return $this;
 }
 /**
  * Apply catalog price rules to product on frontend
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /* @var $collection ProductCollection */
     $collection = $observer->getEvent()->getCollection();
     $store = $this->storeManager->getStore($observer->getEvent()->getStoreId());
     $websiteId = $store->getWebsiteId();
     if ($observer->getEvent()->hasCustomerGroupId()) {
         $groupId = $observer->getEvent()->getCustomerGroupId();
     } else {
         if ($this->customerSession->isLoggedIn()) {
             $groupId = $this->customerSession->getCustomerGroupId();
         } else {
             $groupId = $this->groupManagement->getNotLoggedInGroup()->getId();
         }
     }
     if ($observer->getEvent()->hasDate()) {
         $date = new \DateTime($observer->getEvent()->getDate());
     } else {
         $date = (new \DateTime())->setTimestamp($this->localeDate->scopeTimeStamp($store));
     }
     $productIds = [];
     /* @var $product Product */
     foreach ($collection as $product) {
         $key = implode('|', [$date->format('Y-m-d H:i:s'), $websiteId, $groupId, $product->getId()]);
         if (!$this->rulePricesStorage->hasRulePrice($key)) {
             $productIds[] = $product->getId();
         }
     }
     if ($productIds) {
         $rulePrices = $this->resourceRuleFactory->create()->getRulePrices($date, $websiteId, $groupId, $productIds);
         foreach ($productIds as $productId) {
             $key = implode('|', [$date->format('Y-m-d H:i:s'), $websiteId, $groupId, $productId]);
             $this->rulePricesStorage->setRulePrice($key, isset($rulePrices[$productId]) ? $rulePrices[$productId] : false);
         }
     }
     return $this;
 }
Ejemplo n.º 24
0
 /**
  * Calculate price using catalog price rule of product
  *
  * @param Product $product
  * @param float $price
  * @return float|null
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function calcProductPriceRule(Product $product, $price)
 {
     $priceRules = null;
     $productId = $product->getId();
     $storeId = $product->getStoreId();
     $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId();
     if ($product->hasCustomerGroupId()) {
         $customerGroupId = $product->getCustomerGroupId();
     } else {
         $customerGroupId = $this->_customerSession->getCustomerGroupId();
     }
     $dateTs = $this->_localeDate->scopeTimeStamp($storeId);
     $cacheKey = date('Y-m-d', $dateTs) . "|{$websiteId}|{$customerGroupId}|{$productId}|{$price}";
     if (!array_key_exists($cacheKey, self::$_priceRulesData)) {
         $rulesData = $this->_getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
         if ($rulesData) {
             foreach ($rulesData as $ruleData) {
                 if ($product->getParentId()) {
                     if (!empty($ruleData['sub_simple_action'])) {
                         $priceRules = $this->_catalogRuleData->calcPriceRule($ruleData['sub_simple_action'], $ruleData['sub_discount_amount'], $priceRules ? $priceRules : $price);
                     } else {
                         $priceRules = $priceRules ? $priceRules : $price;
                     }
                     if ($ruleData['action_stop']) {
                         break;
                     }
                 } else {
                     $priceRules = $this->_catalogRuleData->calcPriceRule($ruleData['action_operator'], $ruleData['action_amount'], $priceRules ? $priceRules : $price);
                     if ($ruleData['action_stop']) {
                         break;
                     }
                 }
             }
             return self::$_priceRulesData[$cacheKey] = $priceRules;
         } else {
             self::$_priceRulesData[$cacheKey] = null;
         }
     } else {
         return self::$_priceRulesData[$cacheKey];
     }
     return null;
 }
Ejemplo n.º 25
0
 /**
  * @param EventObserver $observer
  * @return $this
  */
 public function prepareCatalogProductCollectionPrices(EventObserver $observer)
 {
     /* @var $collection ProductCollection */
     $collection = $observer->getEvent()->getCollection();
     $store = $this->_storeManager->getStore($observer->getEvent()->getStoreId());
     $websiteId = $store->getWebsiteId();
     if ($observer->getEvent()->hasCustomerGroupId()) {
         $groupId = $observer->getEvent()->getCustomerGroupId();
     } else {
         if ($this->_customerSession->isLoggedIn()) {
             $groupId = $this->_customerSession->getCustomerGroupId();
         } else {
             $groupId = Group::NOT_LOGGED_IN_ID;
         }
     }
     if ($observer->getEvent()->hasDate()) {
         $date = $observer->getEvent()->getDate();
     } else {
         $date = $this->_localeDate->scopeTimeStamp($store);
     }
     $productIds = array();
     /* @var $product Product */
     foreach ($collection as $product) {
         $key = implode('|', array($date, $websiteId, $groupId, $product->getId()));
         if (!isset($this->_rulePrices[$key])) {
             $productIds[] = $product->getId();
         }
     }
     if ($productIds) {
         $rulePrices = $this->_resourceRuleFactory->create()->getRulePrices($date, $websiteId, $groupId, $productIds);
         foreach ($productIds as $productId) {
             $key = implode('|', array($date, $websiteId, $groupId, $productId));
             $this->_rulePrices[$key] = isset($rulePrices[$productId]) ? $rulePrices[$productId] : false;
         }
     }
     return $this;
 }
Ejemplo n.º 26
0
 /**
  * Apply front-end price limitation filters to the collection
  *
  * @return $this
  */
 public function applyFrontendPriceLimitations()
 {
     $this->_productLimitationFilters->setUsePriceIndex(true);
     if (!isset($this->_productLimitationFilters['customer_group_id'])) {
         $customerGroupId = $this->_customerSession->getCustomerGroupId();
         $this->_productLimitationFilters['customer_group_id'] = $customerGroupId;
     }
     if (!isset($this->_productLimitationFilters['website_id'])) {
         $websiteId = $this->_storeManager->getStore($this->getStoreId())->getWebsiteId();
         $this->_productLimitationFilters['website_id'] = $websiteId;
     }
     $this->_applyProductLimitations();
     return $this;
 }
Ejemplo n.º 27
0
 /**
  * @param PriceBox $subject
  * @param string $result
  * @return string
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function afterGetCacheKey(PriceBox $subject, $result)
 {
     return implode('-', [$result, $this->priceCurrency->getCurrencySymbol(), $this->dateTime->scopeDate($this->scopeResolver->getScope()->getId())->format('Ymd'), $this->scopeResolver->getScope()->getId(), $this->customerSession->getCustomerGroupId()]);
 }
Ejemplo n.º 28
0
 /**
  * Get product price with all tax settings processing
  *
  * @param   \Magento\Catalog\Model\Product $product
  * @param   float $price inputted product price
  * @param   bool $includingTax return price include tax flag
  * @param   null|\Magento\Customer\Model\Address\AbstractAddress $shippingAddress
  * @param   null|\Magento\Customer\Model\Address\AbstractAddress $billingAddress
  * @param   null|int $ctc customer tax class
  * @param   null|string|bool|int|\Magento\Store\Model\Store $store
  * @param   bool $priceIncludesTax flag what price parameter contain tax
  * @param   bool $roundPrice
  * @return  float
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function getTaxPrice($product, $price, $includingTax = null, $shippingAddress = null, $billingAddress = null, $ctc = null, $store = null, $priceIncludesTax = null, $roundPrice = true)
 {
     if (!$price) {
         return $price;
     }
     $store = $this->_storeManager->getStore($store);
     if ($this->_taxConfig->needPriceConversion($store)) {
         if ($priceIncludesTax === null) {
             $priceIncludesTax = $this->_taxConfig->priceIncludesTax($store);
         }
         $shippingAddressDataObject = null;
         if ($shippingAddress === null) {
             $shippingAddressDataObject = $this->convertDefaultTaxAddress($this->_customerSession->getDefaultTaxShippingAddress());
         } elseif ($shippingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) {
             $shippingAddressDataObject = $shippingAddress->getDataModel();
         }
         $billingAddressDataObject = null;
         if ($billingAddress === null) {
             $billingAddressDataObject = $this->convertDefaultTaxAddress($this->_customerSession->getDefaultTaxBillingAddress());
         } elseif ($billingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) {
             $billingAddressDataObject = $billingAddress->getDataModel();
         }
         $taxClassKey = $this->_taxClassKeyFactory->create();
         $taxClassKey->setType(TaxClassKeyInterface::TYPE_ID)->setValue($product->getTaxClassId());
         if ($ctc === null && $this->_customerSession->getCustomerGroupId() != null) {
             $ctc = $this->customerGroupRepository->getById($this->_customerSession->getCustomerGroupId())->getTaxClassId();
         }
         $customerTaxClassKey = $this->_taxClassKeyFactory->create();
         $customerTaxClassKey->setType(TaxClassKeyInterface::TYPE_ID)->setValue($ctc);
         $item = $this->_quoteDetailsItemFactory->create();
         $item->setQuantity(1)->setCode($product->getSku())->setShortDescription($product->getShortDescription())->setTaxClassKey($taxClassKey)->setIsTaxIncluded($priceIncludesTax)->setType('product')->setUnitPrice($price);
         $quoteDetails = $this->_quoteDetailsFactory->create();
         $quoteDetails->setShippingAddress($shippingAddressDataObject)->setBillingAddress($billingAddressDataObject)->setCustomerTaxClassKey($customerTaxClassKey)->setItems([$item])->setCustomerId($this->_customerSession->getCustomerId());
         $storeId = null;
         if ($store) {
             $storeId = $store->getId();
         }
         $taxDetails = $this->_taxCalculationService->calculateTax($quoteDetails, $storeId, $roundPrice);
         $items = $taxDetails->getItems();
         $taxDetailsItem = array_shift($items);
         if ($includingTax !== null) {
             if ($includingTax) {
                 $price = $taxDetailsItem->getPriceInclTax();
             } else {
                 $price = $taxDetailsItem->getPrice();
             }
         } else {
             switch ($this->_taxConfig->getPriceDisplayType($store)) {
                 case Config::DISPLAY_TYPE_EXCLUDING_TAX:
                 case Config::DISPLAY_TYPE_BOTH:
                     $price = $taxDetailsItem->getPrice();
                     break;
                 case Config::DISPLAY_TYPE_INCLUDING_TAX:
                     $price = $taxDetailsItem->getPriceInclTax();
                     break;
                 default:
                     break;
             }
         }
     }
     if ($roundPrice) {
         return $this->priceCurrency->round($price);
     } else {
         return $price;
     }
 }
Ejemplo n.º 29
0
 /**
  * @param \Magento\Framework\App\ActionInterface $subject
  * @param callable $proceed
  * @param \Magento\Framework\App\RequestInterface $request
  * @return mixed
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundDispatch(\Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request)
 {
     $this->httpContext->setValue(Context::CONTEXT_GROUP, $this->customerSession->getCustomerGroupId(), GroupManagement::NOT_LOGGED_IN_ID);
     $this->httpContext->setValue(Context::CONTEXT_AUTH, $this->customerSession->isLoggedIn(), false);
     return $proceed($request);
 }
Ejemplo n.º 30
0
 /**
  * @param Select $select
  * @return Select
  */
 private function setCustomerGroupId($select)
 {
     return $select->where('customer_group_id = ?', $this->customerSession->getCustomerGroupId());
 }