Skip to main content

Magento - afterHasSpecialPrice plugin

To change the HasSpecialPrice value you need to create a new module then after that you can add this code to the 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\Catalog\Pricing\Render\FinalPriceBox">
        <plugin name="ItWevCaCatalogPricingRendererFinalPriceBox"
                type="{Vendor}\{Module}\Plugin\Pricing\Render\FinalPriceBox"
                sortOrder="10"/>
    </type>
</config>

Here is the content of the FinalPriceBox

 

<?php

namespace {Vendor}\{Module}\Plugin\Pricing\Render;

use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\Catalog\Pricing\Price\SpecialPrice;
use Magento\Catalog\Pricing\Render\FinalPriceBox as MagentoFinalPriceBox;

class FinalPriceBox
{

    /**
     * After has special price
     * Make sure the tier pricing does not show a special price
     * @param MagentoFinalPriceBox $subject
     * @param $return
     * @return bool
     */
    public function afterHasSpecialPrice(MagentoFinalPriceBox $subject, $return)
    {

        //You can write your logic here
        $specialPrice = $subject->getPriceType(SpecialPrice::PRICE_CODE)->getAmount()->getValue();
        $finalPrice = $subject->getPriceType(FinalPrice::PRICE_CODE)->getAmount()->getValue();
        if (!$specialPrice) {
            return false;
        }
        return $return;
    }
}