I have two custom options on the product view page: width and length. When adding a product to the cart, I want to update both the weight and the price based on these options.
Currently, the price update works correctly. However, the weight update behaves unexpectedly. Here’s the issue:
-
When a product is added to the cart for the first time, the weight is updated correctly based on the custom options.
-
If I select a different set of custom options and add the same or another product to the cart, the new product’s weight is updated correctly, but the previous product’s weight resets to its original value.
My Approach
I’ve used several events and plugins to achieve this functionality:
Events Used:
checkout_cart_product_add_after
quote_item_save_before
checkout_cart_add_product_complete
sales_quote_item_set_product
Plugins Used:
Target Classes:
MagentoQuoteModelQuoteItemAbstractItem
MagentoQuoteModelQuoteItem
Methods Intercepted:
aroundAddProduct
beforeSetQty
beforeAddProduct
aroundSetProduct
Please provide the proper way to update the weight of a product dynamically based on custom options when added to the cart, ensuring that:
- The weight is updated correctly for the newly added product.
- The previously added items in the cart retain their updated weight without reverting.
Thank you!