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;
}
}
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');
$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');
$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');