I need to change the logic of the tier price calculation. I have special prices on my site and their calculation depends on a product base price, but I need to calculate it depending on the special price. I found a partial solution in How to override magento2 product tierprice? and it works only in a PDP when I visit the cart - it doesn't change the price with changing qty, the price for a is always special price... My code is:
di.xml
<type name="Magento\Catalog\Model\Product">
<plugin name="after_final_price" type="Vendor\TierPriceOnSpecialPrice\Plugin\ChangeTierPrice" sortOrder="999" disabled="false" />
</type>
<type name="Magento\Catalog\Pricing\Price\FinalPrice">
<plugin name="after_final_price_info" type="Vendor\TierPriceOnSpecialPrice\Plugin\ChangeTierPriceInfo" sortOrder="999" disabled="false" />
</type>
Plugins code the same like below:
public function beforeGetFinalPrice($subject, $qty = null)
{
$specialPrice = $subject->getSpecialPrice();
$oldTierPrices = $subject->getData('tier_price');
$newTierPrices = [];
if ($oldTierPrices) {
foreach ($oldTierPrices as $tierPriceName => $tierPriceData) {
$calculatedTierPrice = $specialPrice - ($tierPriceData['percentage_value'] / 100) * $specialPrice;
$newTierPrices[$tierPriceName] = [
'price_id' => $tierPriceData['price_id'],
'website_id' => $tierPriceData['website_id'],
'all_groups' => $tierPriceData['all_groups'],
'cust_group' => $tierPriceData['cust_group'],
'price' => $calculatedTierPrice,
'price_qty' => $tierPriceData['price_qty'],
'percentage_value' => $tierPriceData['percentage_value'],
'product_id' => $tierPriceData['product_id'],
'website_price' => $calculatedTierPrice
];
}
}
//You need to create your own array or modify the default tier price here and set in to product object
$subject->setData('tier_price', $newTierPrices);
}