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:
<?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(); } }
That was all.
Please share the above solution with fellow developers on social media.
Thank you
Also Read: How to Dynamically Change Billing Address in Magento 2