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

  • Have you tried sales_quote_address_collect_totals_before instead ..._after? – sv3n May 18 '18 at 10:15
  • Yes, but this event is dispatched too late as well – Daniele Rovatti May 18 '18 at 11:13
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

  • 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 – Daniele Rovatti May 18 '18 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, privacy policy and cookie policy

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