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

Programatically assigning custom options
 
voidberg
Jr. Member
 
Total Posts:  2
Joined:  2008-08-16
 

Hello all,

I have a lot of products which share the same custom options. Manually adding them is not a good option for me so I’m thinking I have two choices here:

1) Hook in somehow after a product gets saved, check if it has options and add them. I am open to hacking core (I know, I know, it’s bad) or with an observer (searched through the forums and in the magento book… couldn’t find relevant bits).

2) Have a separate script which goes through all products and adds the custom options.

Can anyone share some code snippets, api methods to fetch / add options to a product, meaningful tables, anything?

Thanks,
Andu

 
Magento Community Magento Community
Magento Community
Magento Community
 
Web Man Walking
Jr. Member
 
Total Posts:  18
Joined:  2008-07-23
 

I too need to do this, any joy?

 
Magento Community Magento Community
Magento Community
Magento Community
 
voidberg
Jr. Member
 
Total Posts:  2
Joined:  2008-08-16
 

The very very ugly way. smile Still waiting for someone to help me do it the proper way.
What I did was add the option to a product, look at the modifications done to the database, reverse engineer them and add them for all products. This runs in a cronjob or can be run manually.

$options = array(
    array(
      
'value' => 10,
      
'type' => 'fixed',
      
'label' => 'Da',
      
'sku' => 'impachetat',
    ),
    array(
      
'value' => 0,
      
'type' => 'fixed',
      
'label' => 'Nu',
      
'sku' => '',
    ),
  );

  function 
dpr($o{
    
print '<pre>'print_r($o); print '</pre>';
  
}
  
  
function printb($t{
    
print $t;
    print 
'<br />';
  
}
  
  $res 
mysql_pconnect('server''user''pass');
  
mysql_select_db('magentodb');
  
  
$res mysql_query('select entity_id from catalog_product_entity');
  while (
$o mysql_fetch_array($res)) {
    printb
('Produsul cu id-ul '.$o['entity_id']);
    
$res_o mysql_query('select option_id from catalog_product_option where product_id='.$o['entity_id']);
    if (
mysql_num_rows($res_o)) {
    }
    
else {
      
// insert
      
printb('Nu are optiune.');
      
mysql_query('insert into catalog_product_option (product_id, type, is_require, sku) values ('.$o['entity_id'].', "drop_down", 1, "")');
      
$oid mysql_insert_id();

      
mysql_query('insert into catalog_product_option_type_value (option_id, sku) values ('.$oid.', "'.$options[0]['sku'].'")');
      
$ovid1 mysql_insert_id();

      
mysql_query('insert into catalog_product_option_type_value (option_id, sku) values ('.$oid.', "'.$options[1]['sku'].'")');
      
$ovid2 mysql_insert_id();

      
mysql_query('insert into catalog_product_option_type_title (option_type_id, store_id, title) values ('.$ovid1.', 0, "'.$options[0]['label'].'")');
      
mysql_query('insert into catalog_product_option_type_title (option_type_id, store_id, title) values ('.$ovid2.', 0, "'.$options[1]['label'].'")');

      
mysql_query('insert into catalog_product_option_type_price (option_type_id, store_id, price, price_type) values ('.$ovid1.', 0, '.$options[0]['value'].', "'.$options[0]['type'].'")');
      
mysql_query('insert into catalog_product_option_type_price (option_type_id, store_id, price, price_type) values ('.$ovid2.', 0, '.$options[1]['value'].', "'.$options[1]['type'].'")');

      
mysql_query('insert into catalog_product_option_title (option_id, store_id, title) values ('.$oid.', 0, "Impachetat pentru cadou")');
      
      
mysql_query('update catalog_product_entity set has_options=1 where entity_id='.$o['entity_id']);
    
}
  }
 
Magento Community Magento Community
Magento Community
Magento Community
 
Web Man Walking
Jr. Member
 
Total Posts:  18
Joined:  2008-07-23
 
voidberg - 07 October 2008 06:53 AM

The very very ugly way. smile Still waiting for someone to help me do it the proper way.
What I did was add the option to a product, look at the modifications done to the database, reverse engineer them and add them for all products. This runs in a cronjob or can be run manually.

Awesome thanks.  How did you see the mods?  Sorry dbs not my strong point?

E.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Web Man Walking
Jr. Member
 
Total Posts:  18
Joined:  2008-07-23
 

Be good if there was a stored procedure or equivalent to do this?

E.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Discovery
Enthusiast
 
Total Posts:  767
Joined:  2007-12-29
 

I needed to add some custom product options to lots of products - T shirts - and this script worked nicely, or a variant…

I am going to post a wiki article for the next person grin

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top