magento 2 get billing address from quote

Today we talk about how in Magento 2 gets a billing address from a quote. In this tutorial, you will learn how to get data of quote billing address detail example street, city, country id, postcode, region, and telephone. There are two methods of getting the cart detail. One is to inject the class \Magento\Checkout\Model\Session in your block and get data from it. The second is to use the object manager to get data.

Dependency Injection

<?php
namespace QaisarSatti\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
 
    public $_checkoutSession;
 
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,

    ) {
        $this->_checkoutSession = $checkoutSession;
 
    }
 
}

Get cart information in phtml file.

//Get session object in phtml file
$getCurrentQuote = $block->_checkoutSession->getQuote();
$billing = $getCurrentQuote-> getBillingAddress();
$street = $billing->getData('street');
$city = $billing->getData('city');
$countryCode = $billing->getData('country_id');
$postCode = $billing->getData('postcode');
$region = $billing->getData('region');
$telephone = $billing->getData('telephone');

Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$getCurrentQuote = $objectManager->get('\Magento\Checkout\Model\Session');

$billing = $getCurrentQuote-> getBillingAddress();
$street = $billing->getData('street');
$city = $billing->getData('city');
$countryCode = $billing->getData('country_id');
$postCode = $billing->getData('postcode');
$region = $billing->getData('region');
$telephone = $billing->getData('telephone');

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!