I want to apply my catalog promotion price rule for products having tier price as well. Current magento functionality only checks the price minimum value and displays the least among the both but I need the percentage rule amount discounted on tier price too.
2 Answers
Native magento doesn't support applying catalog promotion rule on tier prices and special prices but we can achieve this by extending some of the core files in magento. You can find the _applyTierPrice() method in path
\vendor\magento\module-catalog\Model\Product\Type\Price.php
protected function _applyTierPrice($product, $qty, $finalPrice)
{
if ($qty === null) {
return $finalPrice;
}
$tierPrice = $product->getTierPrice($qty);
if (is_numeric($tierPrice)) {
$finalPrice = min($finalPrice, $tierPrice);
}
return $finalPrice;
}
and in this file down you can get the promotional rule applied price in method calculatePrice() around line no 578 like and check for the min value.
$rulePrice = $this->_ruleFactory->create()->getRulePrice($date, $wId, $gId, $productId);
The rule factory is in constructor like \Magento\CatalogRule\Model\ResourceModel
\RuleFactory $ruleFactory,`
you can also refer the calcProductPriceRule() in the
vendor\magento\module-catalog-rule\Model\Rule.php
to get the promo price of product and then comparing the values you can return your desired value to final price.
For archive your requirement you have to do some customization please check below solution.
NOTE:- BELOW SOLUTION WILL ONLY WORK FOR THE APPLY CATALOG PROMOTION DISCOUND ON TIER PRICE
Magento saved catalog rule price for the all the product in catalogrule_product_price when reindex is run, And magento will display that price on the frontend by using below function.
Magento\CatalogRule\Model\ResourceModel\Rule::getRulePrices()
For apply catalog price rule on tier price please follow below step.
1)Create before plugin.
Yourmodule/CompanyName/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\CatalogRule\Model\Indexer\ProductPriceCalculator">
<plugin name="calculate_price_for_wholesale" type="Yourmodule\CompanyName\Plugin\ProductPriceCalculator" />
</type>
</config>
2)Create Yourmodule/CompanyName/Plugin/ProductPriceCalculator.php
<?php
namespace Yourmodule\CompanyName\Plugin;
class ProductPriceCalculator
{
protected $_resourceConnection;
public function __construct(
\Magento\Framework\App\ResourceConnection $resourceConnection
) {
$this->_resourceConnection = $resourceConnection;
}
public function beforeCalculate(
\Magento\CatalogRule\Model\Indexer\ProductPriceCalculator $subject,
$ruleData,
$productData = null
)
{
$connection = $this->_resourceConnection->getConnection();
$selectQuery = "QUERY"; // SELECT TIER PRICE FROM THE catalog_product_entity_tier_price AS PER YOUR CONDITION
$case = $connection->fetchPairs($selectQuery);
if (isset($case[$ruleData['product_id']])) {
if ($case[$ruleData['product_id']] > 0) {
// IF TIER PRICE FOUND THEN SET THAT PRICE INTO $ruleData['default_price']
$ruleData['default_price'] = $case[$ruleData['product_id']];
}
}
}
return [$ruleData, $productData];
}
}
Run below command
php bin/magento s:d:c
php bin/magento ind:reset
php bin/magento ind:reind
Thanks.