2

I'm try to add custom option to product programmatically whyle add him to cart. I'm use:

$a_options = array(
'options' => array(
     'label' => 'Glove Size',
     'value' => $attr_value ,
)
);

$item->addOption(new Varien_Object(
array(
    'product' => $item->getProduct(),
    'code' => 'additional_options',
    'value' => serialize($a_options)
)
));
$quote->addItem($item);

This is shows option for product in cart and during checkout process, but don't show option in order information. I also tried:

$item->getProduct()->addCustomOption('additional_options', $attr_value );

Try to show them via attributes - didn't help.

$params = array('product' => '1919','qty' => 1,
'options' => array(
                    'glove_size' => $gloves_id,
                  ),);
    $cart->addProduct('1919', $params);  

Magento version is 1.5

Barry
  • 3,303
  • 7
  • 23
  • 42
kafaya
  • 101
  • 2
  • 13

1 Answers1

6

I haven't check that in 1.5 version but the below code will work in 1.7.2 version:

For viewing the custom options you need set options in order items.That can be done through by calling an event sales_convert_quote_item_to_order_item

<sales_convert_quote_item_to_order_item>
    <observers>
        <jrb_setcustomoption_observer>
        <type>singleton</type>
        <class>jrb_setcustomoption/observer</class>
        <method>salesConvertQuoteItemToOrderItem</method>
        </jrb_setcustomoption_observer>
    </observers>
</sales_convert_quote_item_to_order_item>

Set the details options in your observer

public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
    $quoteItem = $observer->getItem();
    if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
        $orderItem = $observer->getOrderItem();
        $options = $orderItem->getProductOptions();
        $options['additional_options'] = unserialize($additionalOptions->getValue());
        $orderItem->setProductOptions($options);
    }
}

For More details you can find in this article: Magento - custom product option don't show in order

Thanks to Vinai

Community
  • 1
  • 1
Jyotiranjan
  • 683
  • 8
  • 20
  • @jyotirajan: my additional_options are in array format like array( 'code' => 'my_code45', 'label' => 'platelet 1', 'value' => array('shape'=>'Shape - Round', 'material'=>'Material- Gold', 'symbol'=>'Symbol ' - image) ); this format is not showing on admin sales order but this format working fine in cart and checkout page – Trliok Jun 21 '16 at 10:01
  • either you can check this in name.phtml (/app/design/adminhtml/default/default/template/sales/items/column/name.phtml) file or check the order object in admin side whether quote custom options are converted to order object or not. that may be set in "info_buyRequest" or "additional_options" column. – Jyotiranjan Jun 30 '16 at 05:38
  • @Jyotiranjan please answer this Question https://magento.stackexchange.com/questions/202036/need-to-display-custom-option-on-admin-sales-order – Pratik Kamani Nov 17 '17 at 11:26
  • @Jyotiranjan I have added "ring_size" as a custom option. Then How can I display on admin->sales->order. – Pratik Kamani Nov 17 '17 at 11:26