Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Adding Values to a Custom Dropdown via Script
 
koopkoop2
Member
 
Total Posts:  38
Joined:  2009-02-04
 

I have a script that will create custom options for individual products. I can do a text field with no problems, but I can’t figure out how to add a drop down. A drop down is harder than a text field because it’s a two part process. Step one is to add the dropdown itself and step two involves adding the values that are selectable in the drop down. I think I’ve got part one figured out, but I need to know which method to use to add values to my newly created dropdown. The code I have so far is below :

$product Mage::getModel('catalog/product');
$product->load(1);

$opt Mage::getModel('catalog/product_option');
$opt->setProduct($product);

$values = array(
        
'title'                => 'Names',
        
'type'                => 'drop_down',
        
'is_require'        => true,
        
'sort_order'        => 10,
        );

$options->setCustomOptions($values)

//NOW WHAT DO I PUT HERE TO ADD VALUES TO MY NEWLY CREATED DROPDOWN//
 
Magento Community Magento Community
Magento Community
Magento Community
 
koopkoop2
Member
 
Total Posts:  38
Joined:  2009-02-04
 

Found ‘AddValues’ method for the options object, but there’s no clue as to what arguments I actually need to pass to it.

Still stuck on this...does anyone know??

 
Magento Community Magento Community
Magento Community
Magento Community
 
koopkoop2
Member
 
Total Posts:  38
Joined:  2009-02-04
 

Someone must’ve done this before. Please help!

I have a feeling this is one of those things that’s easy - it’s just that there’s no documentation for it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

doublepost

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

doublepost

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

doublepost

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

doublepost

The forum is so slow that it’s unusable at times!  >:-(

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

This is what I have used:

$option = array(
            
'id' => $optionId,
            
'option_id' => $optionId,
            
'is_delete' => '',
            
'previous_type' => '',
            
'previous_group' => '',
            
'title' => 'TITLE',
            
'type' => 'radio',
            
'is_require' => 1,
            
'sort_order' => '',
            
'values' => array()
        );
        foreach (
$form as $formValue)  {
                $valueId 
0;
                
$option['values'][] = array(
                    
'is_delete' => $formValue['is_delete'],
                    
'option_type_id' => $valueId,
                    
'price' => $formValue['price'],
                    
'price_type' => 'fixed'// 'percent'
                    
'sku' => $formValue['sku'],
                    
'sort_order' => '',
                    
'title' => $formValue['sku']
                
);
        
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
koopkoop2
Member
 
Total Posts:  38
Joined:  2009-02-04
 

Cool. Thank you so much! You have no idea how much time you just saved me!

In your example though, your foreach loop is always writing to the same $option[’values’] array resulting in the creation of only one option value. What’s the format to create more than one value? Is it as simple as changing $option[’values’] to $option[’values’][ ], in the foreach?

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

OMG doublepost again.  >_<

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

Glad it helped!

Yes, that was a typo, it should be [’values’][] as you wrote. I left out much stuff only relevant to my specific use case. There might be some confusion as to where to get the option_type_id. I got this information by debugging the Admin. Much interesting example code can be found in the Mage_Adminhtml_Catalog_ProductController and in Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option (the getOptionValues() method is very useful to understand).

 
Magento Community Magento Community
Magento Community
Magento Community
 
koopkoop2
Member
 
Total Posts:  38
Joined:  2009-02-04
 

I don’t think you need to set option_type_id.

If you look at the database, this is the automatically generated unique ID (primary key) for each option value. In fact, attempting to manuallly set this should theoretically create relational errors within the option values and between the main option to its value’s. However, you don’t seem to be experiencing any problems - unless there’s another reason for manually setting it that I’m not aware of?

Also, thanks for the links to those files. Been trying to figure out what files the admin page’s “Save” buttons were tied to.

 
Magento Community Magento Community
Magento Community
Magento Community
 
demonkoryu
Guru
 
Avatar
Total Posts:  328
Joined:  2008-11-26
Dortmund, Germany
 

This was for updating previously entered options. You don’t need them to create the options.

 
Magento Community Magento Community
Magento Community
Magento Community
 
RevolutionPoet
Jr. Member
 
Total Posts:  9
Joined:  2009-04-03
 

Would you guys mind posting the script you are using for creating custom options? I would greatly appreciate it!

EDIT: WORKING CODE

$product Mage::getModel('catalog/product');
    
$product->load123456 );
    
    
$opt Mage::getModel('catalog/product_option');
    
$opt->setProduct($product);
    
    
    
$option = array(
    
//'id' => '',
    
'is_delete' => '',
    
'is_require' => true,
    
//'option_id' => 0,
    
'previous_group' => '',
    
'previous_type' => '',
    
'sort_order' => '',
    
'option_type_id' => '',
    
'title' => 'My Option Title',
    
'type' => 'drop_down',
    
'values' => array(
            array(
            
'is_delete' => false,
            
//'option_type_id' => '',
            
'price' => '',
            
'price_type' => 'fixed'// 'percent'
            
'sku' => '',
            
'sort_order' => '',
            
'title' => "drop down 1",
             ),
             array(
            
'is_delete' => false,
            
//'option_type_id' => '',
            
'price' => '',
            
'price_type' => 'fixed'// 'percent'
            
'sku' => '',
            
'sort_order' => '',
            
'title' => "drop down 2",
             ),
          ),
    );
    
    
$opt->addOption$option);
    
$opt->saveOptions( );
 
Magento Community Magento Community
Magento Community
Magento Community
 
RevolutionPoet
Jr. Member
 
Total Posts:  9
Joined:  2009-04-03
 

So are any of you guys willing to post a working sample of the custom options script with drop down ?

EDIT: SEE ABOVE.

 
Magento Community Magento Community
Magento Community
Magento Community
 
DoomStar
Jr. Member
 
Total Posts:  3
Joined:  2009-05-28
 

For 1.4.1.1
I needed this at the end of the code:

$product->setData('has_options'1);
$product->save();
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top