Insert customer address programmatically weird behaviour

by Shell Suite   Last Updated October 04, 2018 06:09 AM

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());


Answers 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: dev.amitbera@gmail.com
 */

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);

    }
}
Amit Bera
Amit Bera
October 04, 2018 05:51 AM

Related Questions