Magento 2 (v2.3) - Cart Page - Is it possible to get mage-cache-storage stored shipping amount in custom module plugin?

Magento 2 (v2.3) - Cart Page - Is it possible to get mage-cache-storage stored shipping amount in custom module plugin?

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 (v2.3) - Cart Page - Is it possible to get mage-cache-storage stored shipping amount in custom module plugin?

Accessing Magento 2 Cart Shipping Amount: A Developer’s Guide

As a senior developer working on Magento 2 (v2.3), you’ve likely encountered situations where you need to access the cart shipping amount in your custom module plugin. This is particularly challenging when dealing with guests or registered users on the cart page, as the shipping amount may not be readily available through typical Magento objects.

The Challenge: Accessing Shipping Amount

Attempts to retrieve the quote shipping amount from common Magento classes such as \Magento\Checkout\Model\Session, \Magento\Quote\Model\Cart\CartTotalRepository, and \Magento\Quote\Model\QuoteFactory often result in a value of 0. This suggests that at this stage, the selected or estimated shipping amount is not yet stored in the quote table or checkout session.

The Solution: localStorage[‘mage-cache-storage’]

Upon closer inspection, you’ll find that Magento stores some cart-related data in the browser’s localStorage. Specifically, the object localStorage['mage-cache-storage'] contains valuable information about the current state of the shopping cart. To access this data:

  1. Open your web browser’s developer tools.
  2. Go to the “Application” tab and select the “Storage” sub-tab.
  3. Locate localStorage, and click on it.

You should see an object called 'mage-cache-storage'. When you parse this JSON object using JSON.parse(localStorage['mage-cache-storage']) in your browser’s console, you’ll find that it contains information about the cart, including shipping details (see example below).

Retrieving Shipping Data: A Step-by-Step Guide

To retrieve the shipping data from localStorage in a Magento 2 custom plugin:

  1. Inject the \Magento\Framework\Stdlib\CookieManagerInterface into your plugin.
  2. Use this to access the mage-cache-storage cookie and parse it.

Here’s an example of how you can achieve this:

public function afterGetQuote(\Magento\Checkout\Model\Session $subject, $result)
{
    try {
        // Get mage-cache-storage data from cookie
        $cookieManager = \Magento\Framework\App\ObjectManager::getInstance()->get(
            \Magento\Framework\Stdlib\CookieManagerInterface::class
        );
        $cacheStorageData = json_decode($cookieManager->getPublicCookie('mage-cache-storage'), true);

        // Access shipping details
        if (isset($cacheStorageData['quote']['items']) && is_array($cacheStorageData['quote']['items'])) {
            foreach ($cacheStorageData['quote']['items'] as $item) {
                if (!empty($item['additional']['shipping_amount'])) {
                    // Process the shipping amount here
                    break;
                }
            }
        }
    } catch (\Exception $e) {
        // Handle exceptions gracefully
    }

    return $result;
}

Conclusion

Retrieving the cart shipping amount in a Magento 2 plugin requires a keen eye for where Magento stores its data. While the quote object and session may not directly contain the desired information, navigating to the localStorage['mage-cache-storage'] object provides the necessary details.

By following the steps outlined above and properly parsing the JSON data within your custom plugin, you can successfully access the selected or estimated shipping amount for both guest users and registered customers. This approach ensures that your module remains compatible with Magento’s core functionality while providing the specific data points required for your custom logic.

Remember to always test your code thoroughly in various scenarios to ensure it behaves as expected when dealing with different user types and cart configurations.

Magento 2 Checkout Cache Database

Our network