Search code examples
magentomagento-1.9

Magento Get Option Label or Title


I'm using the Magento function, getCustomOptions which seems to return all the values I need except for the option label or title. Here is my testing code:

    foreach($product->getCustomOptions() as $o) {
        echo '<br /><strong>[Item ID]</strong> ' . $o->getItemId() . '<br /><strong>[Value ID]</strong> ' . $o->getId() . '<br /><strong>[Value]</strong> ' . $o->getValue() . '<br /><strong>[Code]</strong> ' . $o->getCode() . '<br />';
    }

Here is the resulting output:

[Item ID] 288
[Value ID] 1035
[Value] a:9:{s:7:"product";s:2:"81";s:8:"form_key";s:16:"MJfb8w59zQw2j3T6";s:7:"handles";a:13:{i:0;s:7:"default";i:1;s:13:"STORE_default";i:2;s:27:"THEME_frontend_argento_mall";i:3;s:20:"catalog_product_view";i:4;s:25:"PRODUCT_TYPE_configurable";i:5;s:10:"PRODUCT_81";i:6;s:19:"customer_logged_out";i:7;s:9:"MAP_popup";i:8;s:19:"MAP_price_msrp_item";i:9;s:14:"SHORTCUT_popup";i:10;s:17:"SHORTCUT_uk_popup";i:11;s:33:"tm_lihgtboxpro_product_info_media";i:12;s:15:"page_one_column";}s:7:"ajaxpro";s:1:"1";s:7:"in_cart";s:1:"1";s:15:"related_product";s:0:"";s:15:"super_attribute";a:3:{i:92;s:3:"107";i:151;s:3:"114";i:161;s:2:"67";}s:7:"options";a:1:{i:35;s:7:"536";}s:3:"qty";s:1:"1";}
[Code] info_buyRequest

[Item ID] 288
[Value ID] 1036
[Value] 35
[Code] option_ids

[Item ID] 288
[Value ID] 1037
[Value] 536
[Code] option_35

[Item ID] 288
[Value ID] 1038
[Value] a:3:{i:92;s:3:"107";i:151;s:3:"114";i:161;s:2:"67";}
[Code] attributes

[Item ID] 288
[Value ID] 1039
[Value] 1
[Code] product_qty_71

[Item ID] 288
[Value ID] 1040
[Value] 71
[Code] simple_product

Now I need to know how to use the Item ID or Code to get the option label (or title).


Solution

  • Here is how I'd go about getting the value and label of a specific custom option from the cart page (assuming your custom options all use the same label on the frontend).

    $params = Mage::app()->getRequest()->getParams();
    /** @var Mage_Catalog_Model_Product $product */
    $info = new Varien_Object($params);
    
    // Don't throw an exception if required options are missing
    $processMode = Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_LITE;
    
    $options = array();
    foreach ($product->getOptions() as $option) {
        /* @var $option Mage_Catalog_Model_Product_Option */
        $group = $option->groupFactory($option->getType())
            ->setOption($option)
            ->setProduct($product)
            ->setRequest($info)
            ->setProcessMode($processMode)
            ->validateUserValue($info->getOptions());
    
        $optionValue = $info->getData('options/' . $option->getId());
        $options[] = array(
            'label' => $option->getTitle(),
            'option_id' => $option->getId(),
            'option_type' => $option->getType()
        );
        if($options[0]['label'] == 'Your Option Label') {
            $thecode = 'option_' . $options[0]['option_id'];    //Append the Option ID to "option_" resulting in "option_XX" which you'll need in the next step
            foreach($product->getCustomOptions() as $o) {
                if($o->getCode() == $thecode) {             //if Custom Option code = $thecode
                    $opt_val = $o->getValue();
                }
            }
        }
    }