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.2k
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,281
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

How to add a new address (shipping or billing or another) via Magento 2 REST API?

I was having a similar problem and was digging into the AccountManagement class to find the proper payload. I noticed that the createAccount function checks if a customer ID exists and loads the ...
cdo's user avatar
  • 106
8 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,281
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,593
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( ...
Pushpendra Singh's user avatar
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,692
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,825
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,701
6 votes

How to add a new address (shipping or billing or another) via Magento 2 REST API?

It should be possible to add/update customer addresses using customer repository API: PUT /V1/customers/:id (for admin) PUT /V1/customers/me (for customer)
Alex Paliarush's user avatar
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,620
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
  • 8,969
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,688
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,910
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.2k
5 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,735
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
  • 8,520
4 votes

How do I add a customer's email to the sales invoice?

There is an easier way to add it to all customer configuration emails. Just rewrite Mage_Customer_Helper_Address and it to the customer address attribute list. So open up System > Configuration > ...
Peter Jaap Blaakmeer's user avatar
4 votes

How to check IF getOrigData() has changed using Observer?

Before the after load events are dispatched, _origData and therefore _hasDataChanges are reset. What you can do: Make your checks in an observer for customer_address_save_before, set a flag in your ...
Fabian Schmengler's user avatar
4 votes

Adding button in Magento 2

Sorry that this is not a complete answer but more a pointing in the "right" direction. There are a set of elements that can be called inside the Magento/Ui system. These can be found under app/code/...
David Manners's user avatar

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