{"id":175061,"date":"2019-05-17T09:39:31","date_gmt":"2019-05-17T09:39:31","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=175061"},"modified":"2023-03-06T07:30:19","modified_gmt":"2023-03-06T07:30:19","slug":"how-to-add-the-customer-default-billing-address-to-the-quote-programatically","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/","title":{"rendered":"How to Add the Customer Default Billing Address to the Quote Programmatically?"},"content":{"rendered":"<p>In this blog, we will see how to add the default customer billing address to the quote programmatically in Magento 2.<\/p>\n<p>For setting the data in the quote we need the Quote object.<br \/>\nBy using the customer id we can get the customer&#8217;s default billing address.<\/p>\n<p>And Address class object in the quote contains the setter and getter methods to set and get the address data. Using this we are going to put the address in quotes.<\/p>\n<p>Let me show you how&#8230;..<\/p>\n<pre class=\"brush:php\">    public function __construct(\n        \\Magento\\Quote\\Api\\CartRepositoryInterface $quoteRepository,\n        \\Magento\\Customer\\Api\\CustomerRepositoryInterface $customerRepository,\n        \\Magento\\Customer\\Model\\AddressFactory $addressFactory,\n        \\Magento\\Customer\\Model\\SessionFactory $customerSessionFactory,\n    ) {\n        $this-&gt;quoteRepository = $quoteRepository;\n        $this-&gt;customerRepository = $customerRepository;\n        $this-&gt;addressFactory  = $addressFactory;\n        $this-&gt;_customerSessionFactory = $customerSessionFactory;\n    }\n\n    public function setBillingAddressToQuote($cartId) {\n        $quote = $this-&gt;quoteRepository-&gt;getActive($cartId);\n        \n        $customerId = $this-&gt;_customerSessionFactory-&gt;create()-&gt;getCustomerId();\n\n        \/\/get customer default billing address using customerId.\n        $customer = $this-&gt;customerRepository-&gt;getById($customerId);\n        $billingAddressId = $customer-&gt;getDefaultBilling();\n        $billingAddress = $this-&gt;addressFactory-&gt;create()-&gt;load($billingAddressId);\n        $address = $billingAddress-&gt;getData();\n\n        \/\/now setting the address as the quote billing address\n        $quote-&gt;getBillingAddress()-&gt;setFirstname($address['firstname']);\n        $quote-&gt;getBillingAddress()-&gt;setLastname($address['lastname']);\n        $quote-&gt;getBillingAddress()-&gt;setStreet($address['street']);\n        $quote-&gt;getBillingAddress()-&gt;setCity($address['city']);\n        $quote-&gt;getBillingAddress()-&gt;setTelephone($address['telephone']);\n        $quote-&gt;getBillingAddress()-&gt;setPostcode($address['postcode']);\n        $quote-&gt;getBillingAddress()-&gt;setCountryId($address['country_id']);\n    }<\/pre>\n<p>Default magento class <strong>module-quote\/Model\/Quote\/Address.php<\/strong>. Here, in this class, we can see all the methods to set data in the quote.<\/p>\n<p>Similarly, using this technique we can not only set the customer&#8217;s default billing address but also can use the data from any source we are getting.<\/p>\n<p>Thank you for checking this blog. Please let me know if you find any issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will see how to add the default customer billing address to the quote programmatically in Magento 2. For setting the data in the quote we need the Quote object. By using the customer id we can get the customer&#8217;s default billing address. And Address class object in the quote contains the <a href=\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":263,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,302,13],"tags":[8553],"class_list":["post-175061","post","type-post","status-publish","format-standard","hentry","category-magento","category-magento2","category-php","tag-quote-address"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Add the Customer Default Billing Address to the Quote 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-add-the-customer-default-billing-address-to-the-quote-programatically\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Add the Customer Default Billing Address to the Quote Programmatically? - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In this blog, we will see how to add the default customer billing address to the quote programmatically in Magento 2. For setting the data in the quote we need the Quote object. By using the customer id we can get the customer&#8217;s default billing address. And Address class object in the quote contains the [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/\" \/>\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=\"2019-05-17T09:39:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-06T07:30:19+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=\"Rajat Verma\" \/>\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=\"Rajat Verma\" \/>\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-add-the-customer-default-billing-address-to-the-quote-programatically\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/\"},\"author\":{\"name\":\"Rajat Verma\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/6cdb78fdfd18ba990da43f117619a858\"},\"headline\":\"How to Add the Customer Default Billing Address to the Quote Programmatically?\",\"datePublished\":\"2019-05-17T09:39:31+00:00\",\"dateModified\":\"2023-03-06T07:30:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/\"},\"wordCount\":158,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"quote address\"],\"articleSection\":[\"magento\",\"Magento2\",\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/\",\"name\":\"How to Add the Customer Default Billing Address to the Quote Programmatically? - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2019-05-17T09:39:31+00:00\",\"dateModified\":\"2023-03-06T07:30:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Add the Customer Default Billing Address to the Quote 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\/6cdb78fdfd18ba990da43f117619a858\",\"name\":\"Rajat Verma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ecad6f740eef36b76420bb9efc0e8e5633bf47af4c05127d527800f481098ed9?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\/ecad6f740eef36b76420bb9efc0e8e5633bf47af4c05127d527800f481098ed9?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Rajat Verma\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/rajat-verma351\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Add the Customer Default Billing Address to the Quote 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-add-the-customer-default-billing-address-to-the-quote-programatically\/","og_locale":"en_US","og_type":"article","og_title":"How to Add the Customer Default Billing Address to the Quote Programmatically? - Webkul Blog","og_description":"In this blog, we will see how to add the default customer billing address to the quote programmatically in Magento 2. For setting the data in the quote we need the Quote object. By using the customer id we can get the customer&#8217;s default billing address. And Address class object in the quote contains the [...]","og_url":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2019-05-17T09:39:31+00:00","article_modified_time":"2023-03-06T07:30:19+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":"Rajat Verma","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Rajat Verma","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/"},"author":{"name":"Rajat Verma","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/6cdb78fdfd18ba990da43f117619a858"},"headline":"How to Add the Customer Default Billing Address to the Quote Programmatically?","datePublished":"2019-05-17T09:39:31+00:00","dateModified":"2023-03-06T07:30:19+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/"},"wordCount":158,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["quote address"],"articleSection":["magento","Magento2","php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/","url":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/","name":"How to Add the Customer Default Billing Address to the Quote Programmatically? - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2019-05-17T09:39:31+00:00","dateModified":"2023-03-06T07:30:19+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-add-the-customer-default-billing-address-to-the-quote-programatically\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Add the Customer Default Billing Address to the Quote 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\/6cdb78fdfd18ba990da43f117619a858","name":"Rajat Verma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ecad6f740eef36b76420bb9efc0e8e5633bf47af4c05127d527800f481098ed9?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\/ecad6f740eef36b76420bb9efc0e8e5633bf47af4c05127d527800f481098ed9?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Rajat Verma"},"url":"https:\/\/webkul.com\/blog\/author\/rajat-verma351\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/175061","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\/263"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=175061"}],"version-history":[{"count":12,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/175061\/revisions"}],"predecessor-version":[{"id":371694,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/175061\/revisions\/371694"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=175061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=175061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=175061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}