0

Currently am using magento 2.2.5,

while adding the product to the cart the custom options values are stored in the quote_item_option table correctly.

while updating the custom options programmatically it has not changed in the quote_item_option table and doesn't throw any exception.

Please guide me with the proper solutions.

4
  • Can you please share the code you are using to set the custom options and to change them programatically?
    – HelgeB
    Apr 15, 2019 at 15:22
  • @HelgeB I have posted the codes into the github. Please find the link below github.com/thilaRathin/magento2 Apr 16, 2019 at 5:55
  • @HelgeB this link is not active now. can you please share it again, the code.
    – surbhi agr
    Dec 3, 2019 at 10:07
  • @HelgeB link is not active. Can you share it again? Nov 10, 2020 at 4:50

2 Answers 2

1

Without understanding what exactly you want to achieve with your change of option value: If you just want to modify the value of an already set option, I guess you can simplify your code in _saveCartItemEngravingOptionValue.

Remove the line $cartItem->setOptions($options)->save(); (the option is already in the cart item) and just call save on the specific option object:

$option->setValue(...)->save();

That should save the changes if you get into that part of the code (I would suggest to add some logging to your code, to see if that is the case).

2
  • Thanks you so much man.. its working fine Apr 16, 2019 at 9:15
  • how can i get $option for cart item?
    – Khushbu
    Jun 7, 2019 at 12:02
2

create an observer using checkout_cart_product_add_after event into Vender/CustomModule/etc/frontend/events.xml

 <event name="checkout_cart_product_add_after">
    <observer name="change_custom_option" instance="Vender\CustomModule\Observer\ChangeCustomOption" />
 </event>

then create observer into Vender\CustomModule\Observer/ChangeCustomOption.php

  use Magento\Framework\Event\ObserverInterface;
  use Magento\Framework\Event\Observer;

  class ChangeCustomOption implements ObserverInterface
   {
     public function execute(Observer $observer) {

       $item  = $observer->getEvent()->getData('quote_item');          
       $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

       $_product = $item->getProduct();

       $options  = $_product->getTypeInstance(true)->getOrderOptions($_product);

       $customOptions = $options['options'];

       $customValue  = 10.6; //fill value here

       foreach($customOptions as $key=>$option): 

          if($option[label]==Size):
               $item->getOptionByCode('option_'.$option['option_id'])->setValue($customValue);
          endif;

       endforeach;     
    }
 }

hope it will help you.

Happy Coding!

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.