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. – brackfost 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.

  • 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
  • 1
    If the weight is saved correctly with the changed value in the quote_item and order_item table I would say we need to look at the calculation process of your shipping costs. Please open a new question since here is a lot of stuff in the comments and it won't get better with more :-). Please add also some details about your shipping calculation and it's dependency to weight. I'm not sure where that happens in Standard Magento – HelgeB Mar 14 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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