Displaying Custom Option Prices on Magento Product Detail Page
Are you struggling to display custom option prices alongside their names within the price box of your Magento product detail page? Don’t worry, you’re not alone! In this post, we’ll guide you through a step-by-step solution to achieve exactly that.
Understanding the Problem
Magento’s default behavior is to display custom options separately from the base price. While it provides flexibility, it can lead to confusion for your customers when they view the product detail page. To address this, we need to modify how Magento renders the price box.
The Solution: Overriding the Price Box Template
The key lies in overriding the core catalog_product_prices.phtml template file located at:
app/design/frontend/<Your_Theme>/<Your_Theme>/template/catalog/product/price
Here’s what you need to do:
-
Create a copy of the original template: In your theme directory, create a new folder structure if it doesn’t exist:
catalog_product/view/. Inside this folder, create another folder namedfrontend/<Your_Theme>/<Your_Theme>. Finally, inside this folder, copy theprice.phtmlfile fromapp/design/frontend/base/default/template/catalog/product/to your newly created path. -
Modify the copied template: Open the copied
price.phtmlfile in a text editor. You’ll notice that Magento uses a conditional statement to display the base price and additional prices:
<?php if ($this->getExcludeTaxFromPrice(): ?>
<p class="old-price">
<?php echo $this->__('Special Price') ?>: <?php echo $this->formatPrice($this->getProduct()->getFinalPrice()); ?>
</p>
<?php endif; ?>
To display custom option prices within the price box, you need to make a few modifications:
Displaying Custom Option Prices
<?php if ($this->getExcludeTaxFromPrice() || $this->hasOptions()): ?>
<p class="special-price">
<?php echo $this->__('Special Price') ?>: <?php echo $this->formatPrice($this->getProduct()->getFinalPrice()); ?>
</p>
<?php endif; ?>
<!-- Display custom option prices -->
<?php if ($this->hasOptions()): ?>
<div class="custom-option-prices">
<?php foreach ($this->getProduct()->getOptions() as $option): ?>
<?php foreach ($option->getValues() as $value): ?>
<p>
<?php echo $option->getTitle(); ?>: <?php echo $this->formatPrice($value->getPrice()); ?>
</p>
<?php endforeach; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
In the modified code, we’ve added a new conditional statement that checks if the product has any options. If it does, we loop through each option and its values to display their titles and prices using the formatPrice method.
Conclusion
By overriding the core price template and making these modifications, you can now display custom option prices directly within the price box on your Magento product detail page. This enhances the user experience by providing a clear overview of all pricing information in one place.
Remember to flush your cache after making these changes for them to take effect.
If you found this solution helpful, consider checking out our comprehensive tutorials and guides at Laravel Company. We cover everything from Magento development best practices to advanced customization techniques.