{"id":374741,"date":"2023-03-30T07:53:03","date_gmt":"2023-03-30T07:53:03","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=374741"},"modified":"2023-03-30T07:53:11","modified_gmt":"2023-03-30T07:53:11","slug":"how-to-get-a-customer-address-by-customer-id-programmatically","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/","title":{"rendered":"How to get a customer address by customer ID programmatically"},"content":{"rendered":"\n<p>In Magento 2 most of the time we required customer addresses to manage the shipping for tracking etc.<br>In this blog, We will learn about how to get the customer&#8217;s address in API using the customer id or customer token.<\/p>\n\n\n\n<p>The below solution may help you to get the address:<\/p>\n\n\n\n<p>You can use the below code in your API.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">    public function __construct(\n        \\Magento\\Store\\Model\\StoreManagerInterface $storeManager,\n        \\Magento\\Customer\\Model\\CustomerFactory $customerFactory,\n        \\Magento\\Framework\\App\\Request\\Http $request\n    ) {\n        $this-&gt;storeManager            = $storeManager;\n        $this-&gt;customerFactory         = $customerFactory;\n        $this-&gt;_request                = $request;\n    }\n\n    public function execute() {\n        $customerId = &quot;12&quot;;\n        $customerFactory = $this-&gt;customerFactory-&gt;create()-&gt;load($customerId);\n\n        if ($customerFactory-&gt;getAddresses() != null)\n        {\n            foreach ($customerFactory-&gt;getAddresses() as $address) {\n                $allCustomerAddress&#091;] = $address-&gt;toArray();\n            }\n        }\n\n        if ($allCustomerAddress != null)\n        {\n            foreach ($allCustomerAddress as $customerAddres)\n            {\n                $returnArray&#091;&quot;success&quot;] = true;\n                $returnArray&#091;&quot;message&quot;] = &quot;&quot;;\n                $returnArray&#091;&quot;street&quot;] = $customerAddres&#091;&#039;street&#039;];\n                $returnArray&#091;&quot;city&quot;] = $customerAddres&#091;&#039;city&#039;];\n                $returnArray&#091;&quot;region&quot;] = $customerAddres&#091;&#039;region&#039;];\n                $returnArray&#091;&quot;country&quot;] = $customerAddres&#091;&#039;country_id&#039;];\n                $returnArray&#091;&quot;postcode&quot;] = $customerAddres&#091;&#039;postcode&#039;];\n            }\n        }\n        return $returnArray;\n    }<\/pre>\n\n\n\n<p>After running this code you will get a response in postman like mentioned below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">{\n    &quot;success&quot;: true,\n    &quot;message&quot;: &quot;&quot;,\n    &quot;street&quot;: &quot;h 12 , set 10\\nsector 59&quot;,\n    &quot;city&quot;: &quot;1&quot;,\n    &quot;region&quot;: null,\n    &quot;country&quot;: &quot;KW&quot;,\n    &quot;postcode&quot;: null\n}<\/pre>\n\n\n\n<p>Note: Using class \\Magento\\Customer\\Model\\CustomerFactory you can get customer-related data that is required as per your requirement.<\/p>\n\n\n\n<p>That&#8217;s it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Magento 2 most of the time we required customer addresses to manage the shipping for tracking etc.In this blog, We will learn about how to get the customer&#8217;s address in API using the customer id or customer token. The below solution may help you to get the address: You can use the below code <a href=\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":439,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121,1],"tags":[13900,2070,2954],"class_list":["post-374741","post","type-post","status-publish","format-standard","hentry","category-magento-2","category-uncategorized","tag-customer-address","tag-magento2","tag-magento2-api"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to get a customer address by customer ID programmatically - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to get a customer address by customer ID programmatically - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In Magento 2 most of the time we required customer addresses to manage the shipping for tracking etc.In this blog, We will learn about how to get the customer&#8217;s address in API using the customer id or customer token. The below solution may help you to get the address: You can use the below code [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-30T07:53:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-30T07:53:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ajay Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajay Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/\"},\"author\":{\"name\":\"Ajay Singh\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/a34da891d20d341126f02e0f2716b63f\"},\"headline\":\"How to get a customer address by customer ID programmatically\",\"datePublished\":\"2023-03-30T07:53:03+00:00\",\"dateModified\":\"2023-03-30T07:53:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/\"},\"wordCount\":103,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"Customer address\",\"Magento2\",\"magento2 api\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/\",\"name\":\"How to get a customer address by customer ID programmatically - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2023-03-30T07:53:03+00:00\",\"dateModified\":\"2023-03-30T07:53:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to get a customer address by customer ID programmatically\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/a34da891d20d341126f02e0f2716b63f\",\"name\":\"Ajay Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eb0e5d7a768feaf55e3b627fb8a98b0d994cf89381c4ca02db24fefe188aec5d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eb0e5d7a768feaf55e3b627fb8a98b0d994cf89381c4ca02db24fefe188aec5d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ajay Singh\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/ajaysingh-tomar648\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to get a customer address by customer ID programmatically - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/","og_locale":"en_US","og_type":"article","og_title":"How to get a customer address by customer ID programmatically - Webkul Blog","og_description":"In Magento 2 most of the time we required customer addresses to manage the shipping for tracking etc.In this blog, We will learn about how to get the customer&#8217;s address in API using the customer id or customer token. The below solution may help you to get the address: You can use the below code [...]","og_url":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2023-03-30T07:53:03+00:00","article_modified_time":"2023-03-30T07:53:11+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ajay Singh","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ajay Singh","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/"},"author":{"name":"Ajay Singh","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/a34da891d20d341126f02e0f2716b63f"},"headline":"How to get a customer address by customer ID programmatically","datePublished":"2023-03-30T07:53:03+00:00","dateModified":"2023-03-30T07:53:11+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/"},"wordCount":103,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["Customer address","Magento2","magento2 api"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/","url":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/","name":"How to get a customer address by customer ID programmatically - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2023-03-30T07:53:03+00:00","dateModified":"2023-03-30T07:53:11+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-get-a-customer-address-by-customer-id-programmatically\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to get a customer address by customer ID programmatically"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/a34da891d20d341126f02e0f2716b63f","name":"Ajay Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eb0e5d7a768feaf55e3b627fb8a98b0d994cf89381c4ca02db24fefe188aec5d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb0e5d7a768feaf55e3b627fb8a98b0d994cf89381c4ca02db24fefe188aec5d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ajay Singh"},"url":"https:\/\/webkul.com\/blog\/author\/ajaysingh-tomar648\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/374741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/439"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=374741"}],"version-history":[{"count":6,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/374741\/revisions"}],"predecessor-version":[{"id":374771,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/374741\/revisions\/374771"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=374741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=374741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=374741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}