How to Override Frontend Store Product Price in Magento 2

How to Override Frontend Store Product Price in Magento 2

Pricing is simple but it will make or break your business in many ways. Setting up high price leads to cart abandon and setting up law may result in you in loss of money. That’s why Pricing strategies are a mix of art and science. Assigning an Optimal price to a product each product, it’s important that can both improve your profits and increase store customer satisfaction should be increased sequentially.

Recently, a client came to us with a requirement of increasing all store product price by 15% without changing each product price individually from store backend. After thinking for a while, we pop out with a plan to develop a custom extension and increase store product price by overriding actual price of a product. Follow these 3 easy steps and you are done.

First Create ‘Module.xml’ file at below location using following code. It will help to override our custom layout file.
app\code\Vendor\Extension\etc\module.xml

<pre class="lang:default decode:true">
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
	<module name="Vendor_Extension" setup_version="1.0.0" active="true">
    	<sequence>
        	<module name="Magento_Catalog"/>
        	<module name="Magento_CatalogWidget"/>
        	<module name="Magento_Widget"/>
    	</sequence>
	</module>
</config>
</pre>

Now it’s time to create our own custom layout at following path using this code.
app\code\Vendor\Extension\view\base\layout\catalog_product_prices.xml

<pre class="lang:default decode:true">
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
	<block class="Magento\Framework\Pricing\Render\RendererPool" name="render.product.prices">
    	<arguments>
            <argument name="default" xsi:type="array">
            	<item name="default_render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item>
            	<item name="default_render_template" xsi:type="string">Magento_Catalog::product/price/default.phtml</item>
            	<item name="default_amount_render_class" xsi:type="string">Magento\Framework\Pricing\Render\Amount</item>
               <strong><item name="default_amount_render_template" xsi:type="string">Vendor_Extension::catalog/product/price/amount/default.phtml</item></strong>
            	<item name="prices" xsi:type="array">
                	<item name="special_price" xsi:type="array">
                    	<item name="render_template" xsi:type="string">Magento_Catalog::product/price/special_price.phtml</item>
                	</item>
                	<item name="tier_price" xsi:type="array">
                    	<item name="render_template" xsi:type="string">Magento_Catalog::product/price/tier_prices.phtml</item>
                	</item>
                	<item name="final_price" xsi:type="array">
                    	<item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item>
                    	<item name="render_template" xsi:type="string">Magento_Catalog::product/price/final_price.phtml</item>
                	</item>
                	<item name="custom_option_price" xsi:type="array">
                    	<item name="amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/default.phtml</item>
                	</item>
                	<item name="configured_price" xsi:type="array">
    	                <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\ConfiguredPriceBox</item>
                    	<item name="render_template" xsi:type="string">Magento_Catalog::product/price/configured_price.phtml</item>
   	             </item>
            	</item>
            	<!--<item name="adjustments" xsi:type="array"></item>-->
        	</argument>
    	</arguments>
	</block>
</layout>
</pre>

Lastly, we have to create one more file ‘default.phtml’ with our logic of overriding default price.
app\code\Vendor\Extension\view\base\templates\catalog\product\price\amount\default.phtml

<pre class="lang:default decode:true">
<?php
$om = \Magento\Framework\App\ObjectManager::getInstance();
$price_arr = explode('-', $block->getPriceId());
foreach ($price_arr as $item)
{
	if(is_numeric($item)){
    	$pid=$item;
    	break;
	}
}
$product = $om->get('Magento\Catalog\Model\ProductFactory')->create()->load($pid);
$priceHelper = $om->create('Magento\Framework\Pricing\Helper\Data'); ?>
<span class="price-container <?= /* @escapeNotVerified */ $block->getAdjustmentCssClasses() ?>"
	<?= $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
<?php if ($block->getDisplayLabel()): ?>
	<span class="price-label"><?= /* @escapeNotVerified */ $block->getDisplayLabel() ?></span>
<?php endif; ?>
	<?php $ finalprice =$product->getFinalPrice();
    	$finalprice+=(($finalprice*15)/100); ?>
	<span class="price-label">
<h4>
<b><?php echo $priceHelper->currency($finalprice, true, false);  ?></b>
</h4>
</span>
</span>
</pre>

You can change $finalprice equation as per your requirement of increasing or decreasing price. Also, you can play & manipulate with these codes according to your needs.

Lastly, hit that stars if you love reading our blogs and comment down below if you face any issue while using this code.
Happy Overriding!

Previous Article

Adobe Acquires Magento Commerce at $1.68 Billion - Commerce to Cloud

Next Article

How to add Customer Address Field in Magento 2 Sign up form

Write a Comment
  1. Fernando Torres

    you shouldn’t be using ObjectManager. Could you update your solution using any helper instead?

    We are using Magento 2, NOT Magento 1, so please can you give us a solution using dependency injection instead the ObjectManager?

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨