1

I'm using magento v2.2.5 , i successfully insert customer programmatically and after i finished insert the customer , i tried to insert customer address like this:

//load
$customer = $this->insertCustomer();
$data['customer_id'] = $customer->getId();

//create address
/* \Magento\Customer\Model\AddressFactory */
$address = $this->addressFactory->create();
$address = $this->addressDataFactory->setCustomerId($data['customer_id'])
      ->setFirstname($data['firstname'])
      ->setLastname($data['lastname'])
      ->setCountryId($data['country'])
      ->setRegion($data['region'])
      ->setPostcode($data['postcode'])
      ->setCity($data['city'])
      ->setTelephone($data['main_telephone'])
      ->setStreet($data['street'])
      ->setIsDefaultBilling('1')
      ->setIsDefaultShipping('1')
      ->setSaveInAddressBook('1');
$address->save();

if i run this code the address is not being be saved in database, but if i add this code die($address->getId()); after $address->save(); the address is being saved in the database table. for example:

...
  ->setIsDefaultShipping('1')
  ->setSaveInAddressBook('1');
$address->save();
die($address->getId());
2
  • get reference from here or here
    – Hitesh
    Commented Oct 4, 2018 at 5:27
  • @Hit's i got reference from here Commented Oct 4, 2018 at 5:28

1 Answer 1

1

You have to use

$address->setCustomerId($data['customer_id'])

Instead of

$this->addressDataFactory->setCustomerId($data['customer_id'])

Code WIll be like

address = $this->addressFactory->create();
$address->setCustomerId($data['customer_id'])
      ->setFirstname($data['firstname'])
      ->setLastname($data['lastname'])
      ->setCountryId($data['country'])
      ->setRegion($data['region'])
      ->setPostcode($data['postcode'])
      ->setCity($data['city'])
      ->setTelephone($data['main_telephone'])
      ->setStreet($data['street'])
      ->setIsDefaultBilling('1')
      ->setIsDefaultShipping('1')
      ->setSaveInAddressBook('1');
$address->save();

Or you can use repository:

<?php
/**
 * User: Amit Bera
 * Email: [email protected]
 */

namespace Devbera\BugreportRepcaptcha;


class Test
{
    /**
     * @var \Magento\Customer\Api\AddressRepositoryInterface
     */
    protected $addressRepository;
    /**
     * @var \Magento\Customer\Api\Data\AddressInterface
     */
    protected $address;

    public function __construct(
        \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
        \Magento\Customer\Api\Data\AddressInterface $address
    )
    {
        $this->addressRepository = $addressRepository;
        $this->address = $address;
    }
    public function createAddress($data)
    {

        $address = $this->address->setCustomerId($data['customer_id'])
            ->setFirstname($data['firstname'])
            ->setLastname($data['lastname'])
            ->setCountryId($data['country'])
            ->setRegion($data['region'])
            ->setPostcode($data['postcode'])
            ->setCity($data['city'])
            ->setTelephone($data['main_telephone'])
            ->setStreet($data['street'])
            ->setIsDefaultBilling(1)
            ->setIsDefaultShipping(1);
        $this->addressRepository->save($address);

    }
}
2
  • 1
    Region is not saved in magento 2.3 Commented Jun 14, 2019 at 12:25
  • It is not working Commented Sep 5, 2019 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.