Magento – Magento 2 – how to set final price for product on view page

magento2priceproduct-view

Magento frontend product view page i need to set custom final price,

how to set? I tried Magento\Catalog\Model\Product\Type\Price.php in this file getFinalprice function but not working with plugin

can anyone give suggestion

Best Answer

The class you're looking for is Magento\Catalog\Pricing\Price\FinalPrice

The below code will double the final price as an example:

etc/frontend/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Pricing\Price\FinalPrice">
        <plugin name="vendorname_modulename_catalog_pricing_price_finalprice" type="VendorName\ModuleName\Plugin\FinalPrice" />
    </type>
</config>

Plugin/FinalPrice.php

<?php

namespace VendorName\ModuleName\Plugin;

class FinalPrice
{
    /**
     * @param \Magento\Catalog\Pricing\Price\FinalPrice $subject
     * @param float $result
     * @return float
     */
    public function afterGetValue(\Magento\Catalog\Pricing\Price\FinalPrice $subject, $result)
    {
        return $result * 2;
    }
}
Related Topic