Question

Our products have custom fields corresponding to weight. How could I add (and update) this weight for the order, so that shipping is calculated with this new weight*?

*Product weight value + custom field weight value.

To clarify my calculation, I've added a custom "weight" field to customizable options with the intention of adding the weight of these selections to the base product weight, so that it's a factor in shipping.

Was it helpful?

Solution

Also I didn't exactly understand what your shipping calculation is, I would suggest the following solution - if you need the calculated value for the weight everywhere in the store:

Create a plugin after the public method Magento\Catalog\Model\Product::getWeight() and overwrite the return value (i.e. the original product weight value) with your calculation. In this way you have to add a single 1-2 lines method and the di.xmlconfiguration and will get your custom weight everywhere you call getWeight() of the product.

If you need the original value of the weight in some places of your store and the calculated value only in the shipping calculation, please provide some more information about that code. There are many different possibilities where you can plugin and add your calculated value to the product object.

For setting the custom weight based on selected custom options you may plug in before \Magento\Quote\Model\Quote\Item::setWeight. The quote item model has the selected custom options and the products original weight, so you might calculate your custom weight there.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top