Example #1
0
 /**
  * Calculate price using catalog price rule of product
  *
  * @param Product $product
  * @param float $price
  * @return float|null
  */
 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;
 }