Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
25 votes

Magento 2: How to add customer address programmatically?

Using Magento\Customer\Api\AddressRepositoryInterface; you can create customer address. namespace {NameSpace}; /** * Description of Address */ class Address { /** * @var Magento\Customer\Api\...
Amit Bera's user avatar
  • 77.5k
20 votes

“We can't save the customer” Alert on Magento 2

You most probably have an error somewhere. In order to find out what the problem is you can add this code. $this->messageManager->addException($e, $e->getMessage()); Instead of the line $...
Marius's user avatar
  • 198k
13 votes
Accepted

Programatically load default billing and shipping address

This is an example of getting default customer's billing and shipping address if you have the customerId and using the customerRepository Magento\Customer\Api\CustomerRepositoryInterface and the ...
Nikolas's user avatar
  • 2,291
12 votes
Accepted

How to get customer address by customer ID?

You cannot retrieve an address based on the customer id so this code will never work: $address = $this->_addressRepository->getByCustomerId($customerId);//error Because the getByCustomerId ...
Raphael at Digital Pianism's user avatar
10 votes

How to add a customer custom-attribute in the customer address edit form?

Custom customer attributes will never just 'appear' on the frontend like they do in the backend. The code that displays them on the frontend is located in a custom phtml file. Magento EE has this ...
CarComp's user avatar
  • 1,246
10 votes
Accepted

Limit country drop down input field Magento 2

Please allow only that need from Admin > Stores > Configuration > General > Country Options > Allow Countries
Bhavin iFlair's user avatar
9 votes
Accepted

Update customer address using address id in magento 2

If you know the address id ($addressId) you can use the address repository to load and save updated addresses: /** * @var \Magento\Customer\Api\AddressRepositoryInterface */ protected $...
Siarhey Uchukhlebau's user avatar
8 votes
Accepted

Magento 2 Customer Listing action: get customer's telephone

The telephone number is related to the customer's addresses and not to the customer's account. So a solution will be to get the customer's default billing or shipping and get the telephone number ...
Nikolas's user avatar
  • 2,291
8 votes
Accepted

Multiselect attribute not saving in Magento 2.3.0

When creating the customer attribute, please take care about following things: Attribute is added to the attribute set, group customer_eav_attribute eav_entity_attribute Attribute is assigned ...
Ketan Borada's user avatar
  • 2,613
7 votes

Programatically update customer address by its entity id

Replace this $address->setData($addressCode, $addressData[$addressValue]); with $address->setData($addressCode, $addressValue); <?php $addressId = '5'; $addressData = array( ...
Banna Online's user avatar
  • 1,069
7 votes

How to get customer address by customer ID?

For get customer adderess using order id in .phtml file $customerId = 3; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerObj = $objectManager->create('Magento\...
Abhinav Singh's user avatar
7 votes
Accepted

how to display the address fields in the registration form

Create customer_account_create.xml file your current active theme For eg. app/design/frontend/{YOUR_PACKAGE}/{YOUR_THEME}/Magento_Customer/layout/customer_account_create.xml After add below ...
Abdul's user avatar
  • 9,701
7 votes
Accepted

Magento 2.X - "Invalid Customer Address Id" comes randomly for the customers on checkout

I've checked it further and fixed this issue by overriding Magento's validateForCart function. /** * Validate address to be used for cart. * * @param CartInterface $cart * @param AddressInterface $...
Sumit's user avatar
  • 4,875
6 votes

Check if customer has address?

$customer->getAddresses() should return all customer addresses so count($customer->getAddresses()) should return quantity of addresses of customer. you can check like if(count($customer->...
Mujahidh's user avatar
  • 2,793
6 votes

Magento 2.X - "Invalid Customer Address Id" comes randomly for the customers on checkout

I had same issue, used plugin to solve this. In di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation=&...
Amit Singh's user avatar
  • 1,775
6 votes

Magento 2 checkout address buttons are missing and the developer console shows a customAttributes error

For this kind of issue, it's really hard to find the root cause. Magento is hard, Js is hard. My suggestion is to use Debugger. Chrome Devtools: https://developers.google.com/web/tools/chrome-...
Khoa TruongDinh's user avatar
5 votes

Magento 2 Get shipping and billing address of customer after order placed?

You can add this piece of code in the file: <?php /** @var $_order \Magento\Sales\Model\Order */ ?> <?php $_order = $block->getOrder() ?> <?php $billingAddress = $_order->...
osrecio's user avatar
  • 1,628
5 votes
Accepted

How to Make First and Last Name Optional for addresses?

Here's a simple method that can be used to make any required address field optional. First create a plugin for Magento\Checkout\Block\Checkout\LayoutProcessor. Define the class as follow: <?php ...
Aaron Allen's user avatar
  • 9,009
5 votes
Accepted

Customer Address custom attribute not showing front end in Magento 2

Customer Address edit form won't automatically generate custom attribute. You have to override template file vendor/magento/module-customer/view/frontend/templates/address/edit.phtml to show it in ...
Quan Le's user avatar
  • 1,698
5 votes

Magento 2 : From where and how does knockout retrieve data on checkout

Here are answers to your query: What/Where is this persistence storage: If we look at dependancy injection of current model, "storage" is an alias of "Magento_Customer/js/customer-data" js model. By ...
PROGOSTECH's user avatar
5 votes

How to update customer address using custom Api in magento2?

You need to call below api for customer address update API : xyz.com/rest/V1/customers/2 Header: Authorization :"Bearer ishamls4xu7pnwhlvup141502tjm3cl2" Content-Type :"application/json" Method : ...
Prashant Valanda's user avatar
5 votes

Magento 2: How to add customer address programmatically?

Please try with following code. $customerId = 10 $addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory'); $address = $addresss->create(); $address->setCustomerId($...
Vishnunath's user avatar
5 votes
Accepted

How to get Region text from address id (Magento 2)

You can try following code to get the region from the address id. $address = $this->addressRepository->getById($addressId); $address->getRegion()->getRegion(); where $this->...
Anshu Mishra's user avatar
  • 8,970
5 votes
Accepted

Magento 2 How to get Customer address after registration?

As $customer is an object of service contact data interface \Magento\Customer\Api\Data\CustomerInterface .So you cannot get default address using $customer->getDefaultShippingAddress(). This ...
Amit Bera's user avatar
  • 77.5k
5 votes
Accepted

How to get Customer Address in formated way and print it - Magento2.3

create Model app/code/VendoreName/ModuleName/Model CustomerAddress.php <?php namespace VendoreName\ModuleName\Model; use Magento\Framework\Model\AbstractModel; use VendoreName\ModuleName\Model\...
Msquare's user avatar
  • 9,103
4 votes

Magento Checkout Street Line Autofill Bug

Using x-autocompletetype="address-line2" does not appear to work with the latest Chrome (65.0.3325.106), it will still duplicate the first street address into both inputs. However, autocomplete="...
Code Commander's user avatar
4 votes
Accepted

Magento 2 - Event after customer edit account and address

You can use below events for 3rd point. Edit Address customer_address_save_after <event name="customer_address_save_after"> <observer name="customer_addres_after_save_viv_observer" ...
Krishna ijjada's user avatar
4 votes
Accepted

How to get list billing address and list shipping address based on customer id

Try following way: $customerId = 2; $customer = Mage::getModel('customer/customer')->load($customerId); $defaultBilling = $customer->getDefaultBilling(); $defaultShipping = $customer->...
Sohel Rana's user avatar
  • 35.9k
4 votes
Accepted

Trying to understand EAV attributes and extension attributes in Magento 2

I had to do something similar in a Twilio module I wrote, https://github.com/pmclain/module-twilio. I've included the relevant code for adding the custom address attribute sms_alert to the frontend ...
Pmclain's user avatar
  • 2,608
4 votes

Magento 2 : "Invalid address id" when adding product to cart

This may be due to having multiple active quotes for a logged-in customer. Note: Multiple Quote creation may happen in different kinds of scenarios and my case happened due to custom extension/module. ...
Aarief Mohideen's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible