How to Get Billing & Shipping Details Programmatically From Quote in Magento 2
The post gives the solution to get billing & shipping details programmatically from quote in Magento 2.
The developers can use this solution in a number of ways.
I’ll share how I put it to use.
One of the clients was troubled with the high rate of shopping cart abandonment. His marketing team asked to use the abandon cart details and send a series of quote emails to those potential customers who quit his store, convincing him for purchase.
For sending such Emails, he required to get the shipping and billing details that were to be included in the Email so they can directly check out the order without the hassle of adding the required shipping details!! (You know I always talk about crazy client requirements, this being one of them 😄)
So, I had to get that data programmatically as to not waste time and it being the only feasible way to do, I came up with the below solution.
Method to Get Billing & Shipping Details Programmatically From Quote in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php namespace [Vendor]\[Module]\Controller\Getquote; use Magento\Checkout\Model\Cart; use Magento\Framework\App\Action\Context; class Index extends Action { private $cart; public function __construct(Context $context, Cart $cart) { $this->cart = $cart; parent::__construct($context); } public function execute() { $billingAddress = $this->cart->getQuote()->getBillingAddress(); $street = $billingAddress->getData('street'); $city = $billingAddress->getData('city'); $countryCode = $billingAddress->getData('country_id'); $postCode = $billingAddress->getData('postcode'); $region = $billingAddress->getData('region'); $telephone = $billingAddress->getData('telephone'); $shippingAddress = $this->cart->getQuote()->getShippingAddress(); $street = $shippingAddress->getData('street'); $city = $shippingAddress->getData('city'); $countryCode = $shippingAddress->getData('country_id'); $postCode = $shippingAddress->getData('postcode'); $region = $shippingAddress->getData('region'); $telephone = $shippingAddress->getData('telephone'); // you can also get Shipping Method from shipping address $shippingMethod = $shippingAddress->getShippingMethod(); } } |
Still having any doubts in the implementation?
Feel free to mention them in the Comments section below.
I’d gladly help you 🙂
Please share the above solution with fellow developers on social media.
Thank you
Also Read: How to Dynamically Change Billing Address in Magento 2
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
I have a problem in billing address same as a shipping address checkbox. When I put coupon code on checkout cart page then payment page billing address only get pin code or country code in payment page is there any solution for this?
Hello Jitendra,
Are you using any third party payment or shipping extension?
Also, do check the log or browser console when you apply the coupon code.
Thank you.