/** * Calculate product price based on price rule data and previous information * * @param array $ruleData * @param null|array $productData * @return float */ protected function _calcRuleProductPrice($ruleData, $productData = null) { if ($productData !== null && isset($productData['rule_price'])) { $productPrice = $productData['rule_price']; } else { $websiteId = $ruleData['website_id']; if (isset($ruleData['website_' . $websiteId . '_price'])) { $productPrice = $ruleData['website_' . $websiteId . '_price']; } else { $productPrice = $ruleData['default_price']; } } $productPrice = $this->_catalogRuleData->calcPriceRule($ruleData['action_operator'], $ruleData['action_amount'], $productPrice); return $this->priceCurrency->round($productPrice); }
/** * 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; }
/** * Test price rule calculation * * @param string $actionOperator * @param int|float $ruleAmount * @param int|float $price * @param int|float $expectedAmount * * @dataProvider calcPriceRuleDataProvider */ public function testCalcPriceRule($actionOperator, $ruleAmount, $price, $expectedAmount) { $this->assertEquals($expectedAmount, $this->helper->calcPriceRule($actionOperator, $ruleAmount, $price)); }