1

I am trying to get billing address line 1 and line 2 with city and state in order collection.

Here is my code:

$oid = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($oid);
$order->getBillingAddress();

But it is not giving any value of billing address.

When I print $order variable it shows billing_address_id index with the integer value.

See below:

[0] => Array            
    (           
        [entity_id] => 1            
        [state] => processing           
        [status] => processing          
        [coupon_code] =>            
        [protect_code] =>           
        [shipping_description] => My carrier            
        [is_virtual] => 0           
        [store_id] => 1         
        [customer_id] =>            
        [base_discount_amount] => 0.0000            
        [base_discount_canceled] =>             
        [base_discount_invoiced] => 0.0000          
        [base_discount_refunded] =>             
        [base_grand_total] => 625.9800          
        [base_shipping_amount] => 7.9800            
        [base_shipping_canceled] =>             
        [base_shipping_invoiced] => 7.9800          
        [base_shipping_refunded] =>             
        [base_shipping_tax_amount] =>           
        [base_shipping_tax_refunded] =>             
        [base_subtotal] => 516.7200         
        [base_subtotal_canceled] =>             
        [base_subtotal_invoiced] =>             
        [base_subtotal_refunded] =>             
        [base_tax_amount] => 101.2776           
        [base_tax_canceled] =>          
        [base_tax_invoiced] => 101.2776         
        [base_tax_refunded] =>          
        [base_to_global_rate] => 1.0000         
        [base_to_order_rate] => 1.0000          
        [base_total_canceled] =>            
        [base_total_invoiced] => 625.9776           
        [base_total_invoiced_cost] => 625.9776          
        [base_total_offline_refunded] =>            
        [base_total_online_refunded] =>             
        [base_total_paid] => 625.9800           
        [base_total_qty_ordered] =>             
        [base_total_refunded] =>            
        [discount_amount] => 0.0000         
        [discount_canceled] =>          
        [discount_invoiced] => 0.0000           
        [discount_refunded] =>          
        [grand_total] => 625.9800           
        [shipping_amount] => 7.9800         
        [shipping_canceled] =>          
        [shipping_invoiced] => 7.9800           
        [shipping_refunded] =>          
        [shipping_tax_amount] => 0.0000         
        [shipping_tax_refunded] =>          
        [store_to_base_rate] => 1.0000          
        [store_to_order_rate] => 1.0000         
        [subtotal] => 516.7200          
        [subtotal_canceled] =>          
        [subtotal_invoiced] =>          
        [subtotal_refunded] =>          
        [tax_amount] => 101.2776            
        [tax_canceled] =>           
        [tax_invoiced] => 101.2776          
        [tax_refunded] =>           
        [total_canceled] =>             
        [total_invoiced] => 625.9776            
        [total_offline_refunded] =>             
        [total_online_refunded] =>          
        [total_paid] => 625.9800            
        [total_qty_ordered] => 2.0000           
        [total_refunded] =>             
        [can_ship_partially] =>             
        [can_ship_partially_item] =>            
        [customer_is_guest] => 0            
        [customer_note_notify] =>           
        [billing_address_id] => 101         
        [customer_group_id] => 0            
        [edit_increment] =>             
        [email_sent] =>             
        [send_email] =>             
        [forced_shipment_with_invoice] =>           
        [payment_auth_expiration] =>            
        [quote_address_id] =>           
        [quote_id] =>           
        [shipping_address_id] => 102            
        [adjustment_negative] =>            
        [adjustment_positive] =>            
        [base_adjustment_negative] =>           
        [base_adjustment_positive] =>           
        [base_shipping_discount_amount] => 0.0000           
        [base_subtotal_incl_tax] => 617.9976            
        [base_total_due] => 625.9800            
        [payment_authorization_amount] =>           
        [shipping_discount_amount] => 0.0000            
        [subtotal_incl_tax] => 617.9976         
        [total_due] => 625.9800         
        [weight] => 0.0000          
        [customer_dob] =>           
        [increment_id] => 100000115         
        [applied_rule_ids] =>           
        [base_currency_code] => USD         
        [customer_email] => guest_5ad06dd73f2e8@mail.com            
        [customer_firstname] => John            
        [customer_lastname] => DOE          
        [customer_middlename] =>            
        [customer_prefix] =>            
        [customer_suffix] =>            
        [customer_taxvat] =>            
        [discount_description] =>           
        [ext_customer_id] =>            
        [ext_order_id] =>           
        [global_currency_code] => USD           
        [hold_before_state] =>          
        [hold_before_status] =>             
        [order_currency_code] => USD            
        [original_increment_id] =>          
        [relation_child_id] =>          
        [relation_child_real_id] =>             
        [relation_parent_id] =>             
        [relation_parent_real_id] =>            
        [remote_ip] =>          
        [shipping_method] => flatrate_flatrate          
        [store_currency_code] => USD            
        [store_name] =>             
        [x_forwarded_for] =>            
        [customer_note] =>          
        [created_at] => 2012-08-31 01:48:27         
        [updated_at] => 2016-11-11 18:08:44         
        [total_item_count] => 2         
        [customer_gender] =>            
        [discount_tax_compensation_amount] =>           
        [base_discount_tax_compensation_amount] =>          
        [shipping_discount_tax_compensation_amount] =>          
        [base_shipping_discount_tax_compensation_amnt] =>           
        [discount_tax_compensation_invoiced] =>             
        [base_discount_tax_compensation_invoiced] =>            
        [discount_tax_compensation_refunded] =>             
        [base_discount_tax_compensation_refunded] =>            
        [shipping_incl_tax] => 7.9800           
        [base_shipping_incl_tax] => 7.9800          
        [coupon_rule_name] =>           
        [gift_message_id] =>            
        [paypal_ipn_customer_notified] => 0         
    )           

How can I retrieve billing address line 1, line 2, city and state in order collection in Magento 2?

1

The Following code will easily get Customer street address line 1, 2 and state

  $orderId = 1222;
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
  $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);

  /*get street line 1 and line 2 values */

  $billingaddress=$order->getBillingAddress();   
  $billingstreet=$order->getBillingAddress()->getStreet();
  $billingstreet1=$billingstreet[0];
  $billingstreet2=$billingstreet[1];

  /*  get state value */

  $billingstate=$order->getBillingAddress()->getRegion();
  • You have never used first line variable. why you wrote this? – Arshad Hussain Apr 3 at 9:48
  • store street first line in $billingstreet1 variable and street second line in $billingstreet2 variable. please check updated answer – Arunprabakaran M Apr 3 at 9:53
0

You can try getting street and other info from billing like this:

$oid = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($oid);


$city =  $order->getBillingAddress()->getCity();
$street= $order->getBillingAddress()->getStreet();
$region= $order->getBillingAddress()->getRegion();
  • I cannot get "getBillingAddress" in my array index. How can I fetch like yours. See my updated question. – Arshad Hussain Apr 3 at 9:13
  • How can I get billing address line 1 and billing address line 2? – Arshad Hussain Apr 3 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.