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
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityMagento 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
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;
}
}
If you want to use event observer for the price change on the product page. You may use following event
catalog_controller_product_view
and in the observer you may use following code.
public function execute(Observer $observer)
{
$customPrice = 4;
$product = $observer->getEvent()->getProduct();
$product->setFinalPrice($customPrice);
}