Custom Option Pricing in Magento Shopping Cart
In Magento, handling custom options for products with varying prices can be a bit challenging, especially when you want to display them separately in the shopping cart. By default, Magento combines the product price with the option price and displays it as a single amount.
Your Current Situation
You have products that offer custom options, each with its own unique pricing. Currently, when these items are added to the cart, Magento calculates and displays the total price as the sum of the base product price and the selected options’ prices. For example:
Original Product Price: $10 Option 1 Price: +$5
Cart Displayed Total: $15 (Combined)
You want to change this behavior so that the original product price is displayed separately, followed by the individual option prices below it in the cart.
Accessing Option Data
The cart template you found uses the getOptionList() method to retrieve the custom options for a product. However, as you correctly noted, the returned list does not contain detailed information about the SKU or price of each option.
Similarly, when exploring getProductOptions(), you’ll find that it also lacks the specific pricing details for each selected option in the cart.
Retrieving Custom Option Prices
To achieve your desired layout, we need to access the custom options’ prices directly from the product model. Here’s how to do this:
-
Open your catalog_product template file (usually located under
app/design/frontend/[theme]/template/catalog/product/view/options.phtml). -
Inside the
<select name="options_${option_id}" id="${e.getId()}" class="required-entry super-attribute-select"block, you can add a hidden field to store the option price.
$_product = Mage::getModel('catalog/product')->load($_product->getId());
$optionPrices = $_product->getProductOptions();
foreach ($optionPrices as $option) {
foreach ($option['options'] as $optionItem) {
echo '<input type="hidden" name="option_price_'.$option['option_id'].'" value="'.$optionItem['price'].'" />';
}
}
- Now, in your cart template (usually
app/design/frontend/[theme]/template/checkout/cart/item/default.phtml), you can retrieve and display these hidden prices.
$options = $this->getOptionList();
// Loop through each option
foreach ($options as $option) {
echo 'Option: '. $option['title'] .'<br>';
// Get the hidden input field value for this option's price
$optionPrice = Mage::registry('option_price_' . $option['option_id']);
echo 'Price: $' . $optionPrice;
}
Displaying Option Prices Seamlessly
To keep your code clean and organized, consider creating a helper file (e.g., app/code/local/YourCompany/CustomOptionPricing/Model/Core.php) to handle the retrieval of option prices:
class YourCompany_CustomOptionPricing_Model_Core extends Mage_Core_Helper_Abstract
{
public function getOptionPrice($optionId)
{
$product = Mage::registry('current_product');
$options = $product->getProductOptions();
foreach ($options as $option) {
if ($option['option_id'] == $optionId) {
return $optionItem['price'];
}
}
}
}
Then, in your cart template:
$helper = Mage::helper('customoptionpricing/core');
echo 'Option: '. $option['title'] .'<br>';
echo 'Price: $' . $helper->getOptionPrice($option['option_id']);
Conclusion
By following these steps and utilizing the getProductOptions() method, you can successfully retrieve the prices of each selected custom option in the cart. This allows you to display them separately from the original product price.
Remember to clear your cache after making changes to the template files. If you’re using a custom theme, ensure that the paths mentioned above match your actual theme’s structure.
For more advanced customizations and best practices for Magento development, feel free to explore our comprehensive guides on Laravel Company.