

By Andrzej
June 26, 2023
How to get shipping rates by quote id in Magento 2
In this blog post we will be discussing how to get shipping rates by quote ID in Magento 2. When developing an e-commerce website, shipping is an integral part of ensuring customer satisfaction. Magento 2 provides a robust shipping system that allows store owners to set up different shipping methods tailored to their specific needs. One important feature of this system is the ability to retrieve shipping rates for a specific quote ID. In this post we will walk you through the process of retrieving shipping rates by quote I, and show you how to implement this functionality in your Magento 2 store. Whether you're a beginner or an experienced developer, this post will provide valuable insights on how to get shipping rates in Magento 2.
Firstly, create a module skeleton in Magento 2.
Create a directory for your module in the app/code
directory of your Magento 2 installation. For example, if your module is called MyModule
, create a directory called MyModule
under app/code
.
Create the module configuration file app/code/MyModule/etc/module.xml
with the following content:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="MyModule_Checkout" setup_version="1.0.0"> </module> </config>
Replace MyModule
with the name of your module.
Create the registration.php
file in the app/code/MyModule
directory with the following content:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MyModule_Checkout', __DIR__ );
Once you have finished creating the skeleton, you can enable the module by running the bin/magento module:enable MyModule_Checkout
command in your Magento 2 installation directory. You could also install the module by running the bin/magento setup:upgrade
command.
Now let's define what a quote ID is. In Magento, a quote is created when a customer adds items to their cart but has not yet checked out. Each quote has a unique ID, which can be used to identify the quote.
To retrieve the shipping rates for a specific quote ID, we need to use the Magento\Quote\Api\CartRepositoryInterface
interface. We can create an instance of this class and load the quote by its ID. To demonstrate this, let's create the new Block class Quote
in the example directory app/code/MyModule/Checkout/Block/Cart
:
<?php namespace MyModule\Checkout\Block\Cart; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\QuoteIdMaskFactory; use Psr\Log\LoggerInterface; class Quote { protected $logger; private QuoteIdMaskFactory $quoteIdMaskFactory; private CartRepositoryInterface $cartRepository; public function __construct( LoggerInterface $logger, QuoteIdMaskFactory $quoteIdMaskFactory, CartRepositoryInterface $cartRepository ) { $this->logger = $logger; $this->quoteIdMaskFactory = $quoteIdMaskFactory; $this->cartRepository = $cartRepository; } public function getShippingRates() { $this->logger->debug("Shipping Rates: ", $this->getShippingRatesByQuoteId("en0iGNEDbGcoX8KIgtjxkxHMgEPNrhvA")); } public function getShippingRatesByQuoteId($maskedId) { $quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedId, 'masked_id'); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->cartRepository->get($quoteIdMask->getQuoteId()); $shippingAddress = $quote->getShippingAddress(); $shippingAddress->collectShippingRates(); return $shippingAddress->getGroupedAllShippingRates(); } }
The output will be shown in the file var/log/debug.log
.
The example output when the getShippingRates
method is run is:
[2023-05-25T14:37:35.769726+00:00] report.DEBUG: Shipping Rates: {"flatrate":[{"Magento\\Quote\\Model\\Quote\\Address\\Rate\\Interceptor":{"carrier_sort_order":null}}]} []
First, we load the \Magento\Quote\Model\QuoteIdMask
object using a factory to get the entity_id
of the quote using a masked ID.
Then we load the quote by its ID from the cart repository.
Once we have loaded the quote from the cart repository, we can get the shipping rates by calling the getShippingAddress()
method to retrieve the quote's shipping address, and then calling the collectShippingRates()
method to calculate the available shipping methods and their rates.
Once we have loaded the quote, we can get the shipping rates by calling the getShippingAddress()
method to retrieve the quote's shipping address, and then calling the collectShippingRates()
method to calculate the available shipping methods and their rates.
We can then loop through the available shipping methods and retrieve their names and rates. In this code, the getShippingRatesByQuoteId
method will return an array of available shipping methods, grouped by carrier.
Each shipping method object has methods to retrieve its name and rate.
And that's it! With these few lines of code, we can easily get the shipping rates for a specific quote ID in Magento 2. This is a useful feature for custom shipping calculations or for displaying shipping rates to customers before they check out.
Summary
Shipping is a crucial aspect of any e-commerce website and Magento offers a robust shipping system to cater to diverse business needs. In this blog post we explored how to retrieve shipping rates by quote ID. By following a few simple steps you can implement this functionality and enhance the customer experience on your Magento store.
The aforementioned functionality opens up possibilities for customized shipping calculations and empowers you to display accurate shipping rates to your customers before they proceed to checkout. By providing transparency and control over shipping costs, you can significantly improve customer satisfaction and drive conversions on your store.
Whether you're a beginner or an experienced developer, implementing this feature will undoubtedly enhance the shipping process and contribute to the success of your e-commerce business.