{"id":263,"date":"2017-09-19T17:37:06","date_gmt":"2017-09-19T17:37:06","guid":{"rendered":"http:\/\/www.evermoretechnologies.com\/blog\/?p=263"},"modified":"2017-09-19T17:37:06","modified_gmt":"2017-09-19T17:37:06","slug":"get-address-fields-in-magento-2-from-an-order-object","status":"publish","type":"post","link":"https:\/\/www.evermoretechnologies.com\/blog\/2017\/09\/get-address-fields-in-magento-2-from-an-order-object\/","title":{"rendered":"Get address fields in Magento 2 from an order object."},"content":{"rendered":"<p>There are many times during development that I need to get part of, or all or, an order address in Magento 2 development. \u00a0For mapping rules in integration or passing the state to a tracking script.<\/p>\n<p>When doing this, for US anyways, here is the code framework I start with:<\/p>\n<p>[code]<br \/>\n\/** $order &#8211; \\Magento\\Sales\\Model\\Order *\/<br \/>\n$firstName = $order-&gt;getShippingAddress()-&gt;getFirstName()<br \/>\n$lastName = $order-&gt;getShippingAddress()-&gt;getLastName();<br \/>\n$address  = $order-&gt;getShippingAddress()-&gt;getStreet(); \/** An array of all address lines.  $address[0] = address line 1, etc. *\/<br \/>\n$city         = $order-&gt;getShippingAddress()-&gt;getCity();<br \/>\n$state        = $order-&gt;getShippingAddress()-&gt;getRegionCode();<br \/>\n$zip          = $order-&gt;getShippingAddress()-&gt;getPostCode();<br \/>\n$country      = $order-&gt;getShippingAddress()-&gt;getCountryId();<br \/>\n$telephone    = $order-&gt;getShippingAddress()-&gt;getTelephone();<br \/>\n$total        = $order-&gt;getGrandTotal();<br \/>\n$shippingAmount = $order-&gt;getShippingAmount();<br \/>\n[\/code]<\/p>\n<p>Obviously there are additional fields you can get, but these cover the main ones I usually need.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many times during development that I need to get part of, or all or, an order address in Magento 2 development. \u00a0For mapping rules in integration or passing&#8230; <a href=\"https:\/\/www.evermoretechnologies.com\/blog\/2017\/09\/get-address-fields-in-magento-2-from-an-order-object\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34,35],"tags":[],"_links":{"self":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/263"}],"collection":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":1,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":264,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/263\/revisions\/264"}],"wp:attachment":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}