Removing Price from Item-Options in Magento Checkout Cart
Introduction
Are you looking for a way to remove the price value of an item-option from your Magento checkout cart? This can be a useful feature if you want to display only the option text without the associated cost. In this guide, we will walk through the steps required to achieve this.
Understanding the Problem
The issue lies in the way Magento renders the item-options in the cart view. By default, it displays both the label and the formatted option value (including the price). You can find this code snippet in app/design/frontend/base/default/template/checkout/cart/item/default.phtml.
<?php if ($_options = $this->getOptionList()): ?>
<dl class="item-options">
<?php foreach ($_options as $_option) : ?>
<?php $_formatedOptionValue = $this->getFormatedOptionValue($_option) ?>
<dt><?php echo $this->htmlEscape($_option['label']) ?></dt>
<dd<?php if (isset($_formatedOptionValue['full_view'])): ?> class="truncated"<?php endif; ?>>
<?php echo $_formatedOptionValue['value'] ?>
<?php if (isset($_formatedOptionValue['full_view'])): ?>
<div class="truncated_full_value">
<dl class="item-options">
<dt><?php echo $this->htmlEscape($_option['label']) ?></dt>
<dd><?php echo $_formatedOptionValue['full_view'] ?></dd>
</dl>
</div>
<?php endif; ?>
</dd>
<?php endforeach; ?>
</dl>
<?php endif; ?>
In the <dd> element, both the option value and its price are being echoed out.
The Solution: Overriding the Template File
To remove the price from the item-option in the checkout cart, we need to override this template file. This can be done using theme inheritance or a module.
Using Theme Inheritance:
- Navigate to your theme directory (e.g.,
app/design/frontend/Vendor/Theme). - Create a new folder named
templateif it doesn’t exist. - Inside the
templatefolder, create another folder calledcheckout. - Inside the
checkoutfolder, create yet another folder calledcart. - Inside the
cartfolder, copy theitem/default.phtmlfile fromapp/design/frontend/base/default/template/checkout/cart/item/.
Using a Module: If you prefer to use a module:
- Create a new folder for your module (e.g.,
app/code/Vendor/Module). - Inside the
etcfolder, create the necessary XML files for theme configuration. - Inside the
view/frontend/templates/checkout/cart/item/folder, place a copy of thedefault.phtmlfile.
Editing the Template File
Once you have created the override file:
- Open your copied version of
item/default.phtml. - Locate the section where the option value is being echoed out.
- Modify this code as follows:
<?php if ($_options = $this->getOptionList()): ?>
<dl class="item-options">
<?php foreach ($_options as $_option) : ?>
<dt><?php echo $this->htmlEscape($_option['label']) ?></dt>
<dd class="truncated"><?php echo substr($_formatedOptionValue['value'], 0, strrpos($_formatedOptionValue['value'], 'â¬')) ?></dd>
<?php endforeach; ?>
</dl>
<?php endif; ?>
We are using the substr() function to truncate the option value string at the position of the currency symbol (â¬), effectively removing the price.
Conclusion
Removing the price from item-options in a Magento checkout cart is a straightforward process that involves overriding the default template file. By following the steps outlined above, you can achieve this without having to modify any core files.
Remember that overriding templates using themes or modules allows for easier updates and reduces the risk of losing your changes during Magento upgrades. Always ensure you have a backup before making these modifications.
For more advanced customization techniques and professional support, consider reaching out to Laravel Company, where our team of experienced developers can help you optimize your Magento store’s performance and functionality.
Happy coding!