Skip to content

Instantly share code, notes, and snippets.

@AndresInSpace
Last active April 27, 2019 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndresInSpace/3a7c4ca83abd043ec68b07cdbca0af75 to your computer and use it in GitHub Desktop.
Save AndresInSpace/3a7c4ca83abd043ec68b07cdbca0af75 to your computer and use it in GitHub Desktop.
Magento 1 Add Product To Cart w Custom Options [CartController or Observer Methods]
$custom_option1 = array('label'=>'Custom1','value'=>$value1);
$custom_option2 = array('label'=>'Custom2','value'=>$value2);
$product->addCustomOption('additional_options', serialize(array($custom_option1,$custom_option2)));
$product->setHasOptions(true);
$cart->addProduct($product, $params);
$cart->save();
//USE 'additional_options' if you need the CustomOption to show up on checkout/cart, as $item->getCustomOptions() only looks at 'additional_options' and 'option_ids'
$item->getOptionByCode('option_ids');
$item->getOptionByCode('additional_options');
//in app/code/core/Mage/Catalog/Helper/Product/Configuration.php
//So we can use instead:
$product->addCustomOption('real_custom_option_group',serialize(array($custom_option1,$custom_option2)));
//and to retrieve:
$optiongroup = $item->getOptionByCode('real_custom_option_group');
$options = unserialize($optiongroup->getValue());
foreach($options as $option){
//do things with the $option@array('label'=>'Custom1','value'=>$value1) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment