Reputation: 49
A few of the products on my Magento site have custom options (not attributes). For one product is available in Gold or Silver an it has a drop down with those two options. How do I get the name of the option the user selected to show next to product name on the shopping cart page?
Upvotes: 1
Views: 18199
Reputation: 76
If you choose to go with Mage_Catalog_Model_Product_Type_Configurable::getOrderOptions($product) as suggested by others, make sure you don't call it on disabled products as in version CE 1.9.* (possibly in other versions, too) this leads to a nasty function call on null. Unless you haven't added a custom module that purges disabled products from carts, this can make your site crash for every customer who added a later disabled product to their cart.
Luckily, you don't need to worry about that if you're using or extending Magento's cart item renderer Mage_Checkout_Block_Cart_Item_Renderer. It offers method getOptionList() which will return an array of all selected options to you, custom options included:
//$this = Mage_Checkout_Block_Cart_Item_Renderer
$options = $this->getOptionList();
This method getOptionList() calls Mage_Catalog_Helper_Product_Configuration which will be your answer if you're not using the Magento renderers or if you want a list of only custom options.
Here's an example of how you can get an array of the selected custom options by calling the helper directly:
$_item = $this->getItem(); // item can represent a simple, configurable or grouped product
$helper = Mage::helper('catalog/product_configuration');
if($onlyCustomOptions){
// get an array of only custom options
$options = $helper->getCustomOptions($_item);
} else {
// get an array of configurable & custom options
$options = $helper->getOptions($_item);
}
(Note: $options will be an empty array if the item has no options.)
Upvotes: 2
Reputation: 1685
Using the following code you can get product custom option values.
$productOptions= $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
Upvotes: 4
Reputation: 746
To get product custom option value at cart page which are set at 'AddtoCart' time try with following code.
$cart = Mage::helper('checkout/cart')->getCart()->getQuote()->getAllItems();
/* cart item loop */
foreach($cart as $item) {
/* This will get custom option value of cart item */
$_customOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
/* Each custom option loop */
foreach($_customOptions['options'] as $_option){
echo $_option['label'] .'=>'. $_option['value']."<br/>";
// Do your further logic here
}
}
Upvotes: 9
Reputation: 5381
If not display then you should try this.
$product = Mage::getModel('catalog/product')->load($product_id);
$options = $product->getProductOptions();
foreach ($options as $option){ print_r($option->getValues()); }
You will find options value
Upvotes: 1
Reputation: 2338
Load the product using the following code:
$product = Mage::getModel('catalog/product')->load($product_id);
And then get the custom options with this:
$options = $product->getProductOptions();
Hope this was helpful,
Pesach
Upvotes: 0