3

How to get Product custom options name?

$data = $this->getRequest()->getParams();
    echo "<pre>"; print_r($data);  die;

    Array
    (
        [options] => Array
            (
                [536] => 6535
                [447] => 2985
            )

        [qty] => 1
        [product] => 543

    )

In options array, 536 is option label name & 6535 is value of selected option. same for [447] is option label name => 2985 is value of selected option.

But i want option array in below format instead of option_id & value of option id.

 [options] => Array
        (
            [color] => red
            [size] => large
        )

Please help. thanx in advance

Updated:

i have create my own custom controller.

Custom_Inquiry_IndexController extends Mage_Core_Controller_Front_Action
{
     public function indexAction()
    {   
         $this->loadLayout(array('default'));
         $data = $this->getRequest()->getParams();
         echo "<pre>"; print_r($data);  die;
         $this->renderLayout();
    }

}
1

Try code below :

$product = Mage::getModel('catalog/product');
$productId = 10; /* you can change $productId to your product id you want display custom options name */
$product->load($productId);

/**
 * In Magento Models or database schema level, the product's Custom Options are
 * executed & maintained as only "options". So, when checking whether any product has
 * Custom Options or not, we should check by using this method "hasOptions()" only.
 */
if($product->hasOptions()) {
    echo '<pre>';

    foreach ($product->getOptions() as $o) {
        $optionType = $o->getType();
        echo 'Type = '.$optionType;

        if ($optionType == 'drop_down') {
            $values = $o->getValues();

            foreach ($values as $k => $v) {
                print_r($v);
            }
        }
        else {
            print_r($o);
        }
    }

    echo '</pre>';
}

Let me know if you have any question !

0

I don't know in which controller are you getting this result, but the solution is to put the values of the attributes instead of the id in the page in question before sending them to the controller, or you load a product model in the controller with your product id then you get all your attributes and you get what you need. the other solution is to parse all the keys and values of your array, then you replace them with the value but this is not the right approach !.

So for me, you should instead go on this way:

$_productId = $sessdata[product]; // 543
$_product   = Mage::getModel('catalog/product')->load($_productId);
$color      = $_product->getColor(); //red
$size       = $_product->getSize(); //large

//If even you get the attribute id instead of value, you can use this:

$_product->getAttributeText('attr_id');
  • see my updated question – Magento 2 Learner Sep 11 '17 at 5:04
  • 1
    @Pankaj Please accept some answer that you find helpful – PЯINCƏ Sep 16 '17 at 2:00
0

To get Option array as your format you have to do two thing-

First you have to get attribute code from your attribute id

// in your case for color attrId is 536
$attrId = 536;

$attrCode = Mage::getModel('eav/entity_attribute')->load($attrId)->getAttributeCode();
 // here in $attrCode you will get 'color'

After that you have to load attribute option label by attribute code and attribute option value

//here option id is value of color
$optionId = 6535;
$attribute = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode);
$label = $attribute->getStoreLabel();
$optionLabel = $attribute->getFrontend()->getOption($optionId);// red

you have to repeat it for other attributes, you can loop it in foreach

For the product Custom option you can get All the custom option by product using this

$_product = Mage::getModel("catalog/product")->load('product_id');
$theoptions = $_product->getOptions();
$i=0;
$array = array();
foreach($theoptions as $opkey=> $opval)
{
   $title = $opval->getTitle();
   if($opval->getType() == 'drop_down'){
      foreach($opval->getValues() as $value) {
         $valueTitle = $value->getTitle();
      }
   }
}
  • above code is not working for me. – Magento 2 Learner Sep 11 '17 at 5:56
  • is it generating any error? – Piyush Sep 11 '17 at 5:59
  • $attrId = 536; $attrCode = Mage::getModel('eav/entity_attribute')->load($attrId)->getAttributeCode(); echo "sdsdsd".$attrCode; die; this print only sdsdsd – Magento 2 Learner Sep 11 '17 at 6:02
  • One more thing, actually there are custom option of simple products not attribute. [options] => Array ( [536] => 6535 [447] => 2985 ) – Magento 2 Learner Sep 11 '17 at 6:03
  • check my update ,you can get custom option detail using that – Piyush Sep 11 '17 at 6:33
0

This is what I coded years ago, placed it in your resource model:

/**
 * Get option title
 * 
 * @param int option_id
 * @param int store_id
 * @return string                    
 */
public function getOptionTitle($optionId, $storeId=0)
{
    $bind    = array('option_id' => $optionId, 'store_id'=>$storeId);
    $adapter = $this->_getReadAdapter();
    $select  = $adapter->select()
        ->from($this->getTable('catalog/product_option_title'), array('title'))
        ->where('`option_id` = :option_id')
        ->where('`store_id` = :store_id')
        ;

    return $adapter->fetchOne($select, $bind);
}

/**
 * Get the option_type_title (text in dropdown or radio or checkbox) given option_type_id
 * 
 * @param int
 * @param int
 * @return string               
 */              
public function getOptionTypeTitle($optionTypeId, $storeId = 0)
{
    $bind    = array('option_type_id' => $optionTypeId, 'store_id'=> $storeId);
    $adapter = $this->_getReadAdapter();
    $select  = $adapter->select()
        ->from($this->getTable('catalog/product_option_type_title'), array('title'))
        ->where('`option_type_id` = :option_type_id')
        ->where('`store_id` = :store_id');
    return $adapter->fetchOne($select, $bind); 
}

In the given example:
$optionId is the keys 536 and 447
$optionTypeId is the values 6535 and 2985

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.