0

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.

  • Which shipping method? – Sohel Rana Feb 13 at 4:19
  • Any shipping method, actually. – Tryingest Fool Feb 13 at 13:45
2

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.

  • Just clarified my calculation/needs in my post. This is a great start, thank you! Do you think I could check if a Custom Option has been selected in the getWeight() method? – Tryingest Fool Feb 13 at 13:53
  • 1
    I updated my answer, please try that method for plugging in – HelgeB Feb 13 at 14:10
  • 1
    not the setProduct, but the setWeight method. beforeSetWeight should be your plugin method, I've updated my answer. Before becouse you get the weight which should be set (i.e. the products weight) as parameter and you can add your custom value and return the result, which is passed to the setWeight() method of the quote model. – HelgeB Feb 13 at 15:33
  • 1
    Give it a try, if you won't have success with that I'll update my answer with some code tommorow – HelgeB Feb 13 at 18:15
  • 1
    Ok, then you can try also to add there your weight. Just manipulate the weight of the product object passed as input parameter as you need. It will be passed in the setProduct method to setWeight – HelgeB Feb 13 at 18:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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