0

I need to add some logic to weight package calculation before shipping rates are calculated. So I created a custom module with an observer on sales_quote_address_collect_totals_after event.

The observer looks like this

use Magento\Framework\Event\ObserverInterface;

class AfterCollectTotals implements ObserverInterface{

   public function execute(\Magento\Framework\Event\Observer $observer){


    foreach ($items as $item) {

         // My custom logic here to get the correct weight in $weight

    }
        $observer->getQuote()->getShippingAddress()->setWeight($weight);
    }
}

Weight appears to be set correctly, but when the Shipping Rates are calculated this value is ignored. In the carrier model, $request->getPackageWeight() returns the package weight before my manipulation.

Where am I wrong?

Thank you

2
  • Have you tried sales_quote_address_collect_totals_before instead ..._after?
    – sv3n
    May 18, 2018 at 10:15
  • Yes, but this event is dispatched too late as well May 18, 2018 at 11:13

2 Answers 2

0

I've find the solution.

I didn't have to use the observer but the right way is to override

class Shipping in Magento\Quote\Model\Quote\Address\Total

I added my custom logic to public function collect and my custom weight is used to calculate the shipping rates

1
  • The reason is that the event sales_quote_address_collect_totals_after is dispatched after that the shipping methods collect quote totals to calculate rates May 18, 2018 at 10:13
0

Same problem. Im setting new weight on checkout_cart_product_add_after Event. Weight is set, but the shipping costs are wrong. How to recalculate the shipping costs? Has anyone some code snippes please?

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.