Handling an Alternate “price.phtml” Template in Magento
Are you trying to use an alternate price.phtml template file for displaying product prices in specific locations within your Magento store? You’re not alone! This is a common requirement, especially when dealing with special pricing rules that can’t be calculated by the built-in catalog price rules.
In this post, we’ll explore how to achieve this without having to modify the existing price.phtml file or use CSS hacks. We’ll create a custom module and swap out the template file conditionally based on the page being viewed.
Understanding the Magento Layout System
Before diving into the solution, let’s quickly recap how Magento handles layout XML files like catalog.xml. These files are responsible for defining the structure of your store’s pages. However, dealing with the price.phtml file directly isn’t straightforward because it isn’t defined in these layout files.
Creating a Custom Layout File
To begin, let’s create a custom layout XML file where we can define our conditional template swap:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/config.xsd">
<type name="Magento\Catalog\Block\Product\AbstractProduct">
<block type="Magento\Catalog\Block\Product\Price" template="catalog/product/price-custom.phtml"
condition="eq[block_name|price]:catalog_product_price"/>
</type>
</config>
This layout file uses the block_condition.handle approach to apply a condition (eq[block_name|price]:catalog_product_price) that checks if the block name is catalog_product_price. This is the unique identifier for the price block on the product page.
Creating the Custom Price Template
Next, create your custom price-custom.phtml template file under the following path:
app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/price-custom.phtml
Now this template will be used whenever the condition in our layout XML is metâi.e., on the product page where the main product is being displayed.
Adding Custom Pricing Logic
In your custom price-custom.phtml file, you can now add any custom pricing logic or additional special prices that you need to display for this specific scenario:
<?php
$product = $block->getProduct();
$specialPrice = $this->yourCustomModule->calculateSpecialPrice($product);
?>
<div class="price-box">
<p class="regular-price"><?= $block->getProductPriceHtml($_product, $type) ?></p>
<p class="special-price"><?php echo $specialPrice; ?></p>
</div>
Notice how we’re using the existing getProductPriceHtml() method to display the regular price and then manually adding our custom special price below it.
Conclusion
By following this approach, you can easily swap out the price.phtml template for specific pages within your Magento store. This keeps your code organized and makes it simple to maintain different pricing display scenarios without modifying the core template or resorting to CSS hacks.
Remember that creating custom modules and layout files is a more advanced topic in Magento development. If you’re new to this process, I recommend checking out our comprehensive guide on creating custom modules for a deeper understanding of the system.
Don’t forget to test your changes thoroughly before deploying them to a live environment. Happy coding!