2

I have a custom HTML on cart page with some checkboxes. I am getting All the selected checkboxes in my observer "checkout_cart_update_items_before" I am able to update cart item prices in this observer.

Now on cart update I want to create these selected checkboxes as Custom option to cart items and display them with all items in cart.

Following is the code that I have tried to create Custom options.

    $item->addOption(
                      array( 
                            'code'  => 'logo_options',
                            'value' => serialize($logo_positions),
                           )
                    );                      
    if ($logoOptions = $item->getOptionByCode('logo_options')) {                            
          $options = $item->getProductOptions();
          $options['logo_options'] = unserialize($logoOptions->getValue());
          item->setProductOptions($options);
    }
    $item->save();

Below is a screenshot of Cart.


enter image description here

9
  • You cannot add multiselect as a custom option to cart items. Mar 8, 2017 at 11:27
  • @JaiminSutariya Then what about custom options as selected checkboxes? Mar 8, 2017 at 11:31
  • You can only show it as custom option title and its value. Mar 8, 2017 at 11:32
  • @JaiminSutariya: What now? I have just added the code for adding custom option but only price in being updated those options are still not getting created to cart item and there are no errors. Mar 9, 2017 at 9:48
  • If you are using Custom options then they should automatically be added to cart if selected. Can you add details about how and where you added checkboxes and what exactly you want to achieve? Will be better if you add screenshots of your cart page. Mar 9, 2017 at 10:06

1 Answer 1

3

Try below code to save your custom options' data.
You need to create arrays for each of the options you want to add to your product. I have used static data but you can do it with your dynamic data.

$logoOption = array(
    'label'                    => 'Logo Options',
    'option_value'             => 'My Logo',
    'value'                    => 'My Logo',
    'print_value'              => 'My Logo',
);
$locationOption = array(
    'label'                    => 'Location',
    'option_value'             => 'Right Breast',
    'value'                    => 'Right Breast',
    'print_value'              => 'Right Breast',
);
$value = array('logo_options'=> $logoOption, 'location' => $locationOption);
$value = serialize($value);

Now to set this data as Product options, Use code,

$item->addOption(array('code'=> 'additional_options', 'product_id'=> $item->getProductId(), 'value'=> $value));

This will add your data to product as options. See screenshot. enter image description here

1
  • Thank You! I just checked it and its working. Now I know what was missing. Mar 9, 2017 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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