I'm looking for some Magento event to add a value(brokerage) to the shipping amount by shown by FedEx.

Why I need it is to achieve the following. We have so far been shipping only within the US, but now want to ship to Canada. We just need to add brokerage to the shipping cost and display the updated shipping amount in the checkout page and everywhere else till the order is placed.I am basically looking for an event to update shipping amount in the checkoutpage so that the updated shipping amount is shown throughout the site.

The event I am trying to use is

Event name: checkout_cart_update_items_after File: vendor/magento/module-checkout/Model/Cart.php

This event works only if customer visits cart and doesn't if he directly goes to the checkout page.


use \Magento\GiftMessage\Api\OrderRepositoryInterface;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
use \Psr\Log\LoggerInterface;

class CheckoutCartSaveAfter implements ObserverInterface
    protected $logger;
    protected $helper;
    protected $order_gift_repository;

     * @desc Construct method which is used to inject dependency of helper class
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Perfectmakeupmirrors\Order\Helper\Data $helper
     * @param \Magento\GiftMessage\Api\OrderRepositoryInterface $order_gift_repository
    public function __construct(LoggerInterface $logger, Data $helper, OrderRepositoryInterface $order_gift_repository){
        $this->logger = $logger;
        $this->helper  = $helper;
        $this->order_gift_repository = $order_gift_repository;

     * @desc Observer execute method which gets control from dispatcher of 'sales_order_place_after' event
     * @param \Magento\Framework\Event\Observer object
    public function execute(Observer $observer) {
        $this->logger->debug("Hi checkout_cart_save_after triggered");
        // 1. Retrieve the Gift Message using dependency injection from the order object.
        // 2. Fetch the record from poms_jobs_queue along with the record ID where the status = "-1".
        // This order object is obtained only after the event "sales_order_save_after" is dispatched.
        $order = $observer->getEvent()->getOrder();

        // Retrieving the Gift Message using dependency injection OrderRepositoryInterface
        // Ensure to use getId() to get the real order ID.
        // Call getMessage() to get the gift message.
        // Exception handling deployed here to address the issue with the empty gift message.
        // Order with empty gift message raises NoSuchEntityException exception.
        // Exception handling is how this situation is dealt with in Magento 2.
        $gift_message = '';         // Initialize to blank.
        try {
            // Magento\GiftMessage\Api\OrderRepositoryInterface $order_gift_repository
            $gift_object  = $this->order_gift_repository->get($order->getId());
            // This line is what throws an exception in case of an empty gift message.
            $gift_message = $gift_object->getMessage();
        } catch (NoSuchEntityException $e) {
            // Valid scenario of No gift message specified. Nothing to be done.
        } catch (\Exception $e) {
            // Error scenario. Since empty gift message throws this error, logging commented
            // $this->logger->debug("Exception: " . $e->getMessage());

        // Call the helper class method to update the order_info array.
        // Pass order object and the gift message as arguments.
        try {
            $this->helper->add_gift_message_to_order_info($order, $gift_message);
        } catch (\Exception $e) {
            $this->logger->debug("***ERROR*** Exception: " . $e->getMessage());


Your Answer

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

Browse other questions tagged or ask your own question.