Display Custom Option Prices on Cart/Checkout Pages in Magento
As an experienced e-commerce developer, I often receive queries about customizing the behavior of Magento’s checkout process. One common request is to display the price of each selected custom option right on the cart and checkout pages. Let me guide you through a step-by-step solution for this issue.
Understanding the Current Behavior
By default, Magento groups all custom options together under one field in the cart and checkout summary views. The individual prices for each option are not displayed separately, which can make it difficult for customers to understand the breakdown of their order total.
Changing the Configuration File: A Wrong Approach
You mentioned that you tried modifying the app/code/core/Mage/Catalog/Helper/Product/Configuration.php file at line 75. While this is a valid approach for custom option fields (text boxes), it won’t work for other types like select boxes or radio buttons because the logic for these options is handled differently in Magento’s core.
The Correct Approach: Overriding the Configuration Object
To achieve your goal, we need to override the Mage_Catalog_Model_Product_Option object. This model handles the rendering of all custom options, regardless of their type. Here’s how you can do it:
-
Create a new file structure
Navigate to the root directory of your Magento installation and create a new folder named
app/code/local/YourCompany/CustomOptions. Inside this folder, create another folder namedOption. -
Override the Configuration Object
In the
app/code/local/YourCompany/CustomOptions/Optiondirectory, create a file namedModel.php. -
Add your custom logic to the Model
Open the newly created
Model.phpfile and add the following code:
class YourCompany_CustomOptions_Model_Option extends Mage_Catalog_Model_Product_Option
{
public function getFormattedOptionValue($option)
{
$formattedValue = parent::getFormattedOptionValue($option);
// Add the option price for each selected value
if ($option->getType() == 'select' || $option->getType() == 'radio') {
$optionPrice = $this->getOptionValuePrice($option);
$formattedValue .= ' (Price: ' . Mage::helper('core')->currency($optionPrice, true, false) . ')';
}
return $formattedValue;
}
}
-
Create a new XML configuration file
In the
app/code/local/YourCompany/CustomOptions/etcdirectory, create a new folder namedconfig.xml. Inside this folder, create another folder namedxml. -
Update the XML Configuration
Open the newly created
config.xmlfile in thexmlsub-folder and add the following code:
<config>
<modules>
<YourCompany_CustomOptions>
<version>0.1.0</version>
</YourCompany_CustomOptions>
</modules>
<global>
<models>
<catalog>
<rewrite>
<product_option>YourCompany_CustomOptions_Model_Option</product_option>
</rewrite>
</catalog>
</models>
</global>
</config>
Conclusion
By following this approach, you will effectively override the default behavior of Magento’s custom option rendering. This will ensure that the price for each selected option is displayed separately in both the cart and checkout summary views.
Remember to clear your cache after making these changes using the command php shell/indexer.php reindexall or via the admin panel under the System > Cache Management menu.
This modification does not require any core file hacks, which makes it a more sustainable solution for your long-term development needs. It also adheres to Magento’s best practices for code organization and customization.
For further reading on Magento development best practices and advanced customization techniques, I recommend checking out Laravel Company where we provide comprehensive guides and tutorials for e-commerce developers at all levels.