4

I have succeed create customer programmatically. Now I want to add customer address to the customer, how can i do this programmatically?

  • how did you created a customer programatically please – Developper Magento Oct 31 '18 at 12:58
8

Using Magento\Customer\Api\AddressRepositoryInterface; you can create customer address.

namespace {NameSpace};

/**
 * Description of Address
 */
class Address {

/**
 * @var Magento\Customer\Api\Data\AddressInterfaceFactory
 */
protected $addressDataFactory;

/**
 * @var \Magento\Customer\Api\AddressRepositoryInterface
 */
protected $addressRepository;

public function __construct(
    \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
    \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
) {
    $this->addressRepository = $addressRepository;
    $this->addressDataFactory = $addressDataFactory;
}

public function saveCustomer()
{
    $address = $this->addressDataFactory->create();
    $address->setFirstname($firstName)
            ->setLastname($lastName)
            ->setCountryId($countryId)
            ->setRegionId($regionId)
            ->setRegion($regionName)
            ->setCity($city)
            ->setPostcode($postcode)
            ->setCustomerId($customerId)
            ->setStreet($street)
            ->setTelephone($telephone);

    $this->addressRepository->save($address);
}
  • Great! let's take this next level...according to my requirement...how can i check that if given address is already exist or not?if yes,then i've to create address else i've to skip. – wakar Ahamad May 29 at 15:27
0

Please try with following code.

$customerId = 10

$addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');

$address = $addresss->create();

$address->setCustomerId($customerId)

->setFirstname('test')

->setLastname('last')

->setCountryId('IN')

->setPostcode(10000)

->setCity('Bangalore')

->setTelephone('1234567890')

->setCompany('BNG')

->setStreet('XYZ street')

->setIsDefaultBilling('1')

->setIsDefaultShipping('1')

->setSaveInAddressBook('1')

$address->save();
0

Try it, It works for me.

private function save_customer($JsonParam) {
        echo "<PRE>";
        print_r($JsonParam);
        exit;
        $objectManager = $bootstrap->getObjectManager();
        $url = \Magento\Framework\App\ObjectManager::getInstance();
        $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
        $state = $objectManager->get('\Magento\Framework\App\State');
        $state->setAreaCode('frontend');

        // Customer Factory to Create Customer
        $customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
        $websiteId = $storeManager->getWebsite()->getWebsiteId();
        $store = $storeManager->getStore();  // Get Store ID
        $storeId = $store->getStoreId();

        // Instantiate object (this is the most important part)
        $customer = $customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->loadByEmail($JsonParam->Email);
        if (!$customer->getId()) {
            $customer->setEmail($JsonParam->Email);
            $customer->setFirstname($JsonParam->FirstName);
            $customer->setLastname($JsonParam->LastName);
            $customer->setPassword('Admin@123');
            $customer->save();

            $addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
            $address = $addresss->create();
            $address->setCustomerId($customer->getId())
                    ->setFirstname($JsonParam->FirstName)
                    ->setLastname($JsonParam->LastName)
                    ->setCountryId($JsonParam->Country)
                    ->setPostcode($JsonParam->Zip)
                    ->setCity($JsonParam->City)
                    ->setState($JsonParam->State)
                    ->setTelephone($JsonParam->Phone)
                    ->setCompany('Arhamsoft Pvt. Ltd.')
                    ->setStreet($JsonParam->Address1 . " / " . $JsonParam->Address2)
                    ->setIsDefaultBilling(false)
                    ->setIsDefaultShipping('1')
                    ->setSaveInAddressBook('1');
            $address->save();

            $addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
            $address = $addresss->create();
            $address->setCustomerId($customer->getId())
                    ->setFirstname($JsonParam->FirstName)
                    ->setLastname($JsonParam->LastName)
                    ->setCountryId($JsonParam->BillingCountry)
                    ->setPostcode($JsonParam->BillingZip)
                    ->setCity($JsonParam->BillingCity)
                    ->setState($JsonParam->BillingState)
                    ->setTelephone($JsonParam->Phone)
                    ->setCompany('Arhamsoft Pvt. Ltd.')
                    ->setStreet($JsonParam->BillingAddress1 . " / " . $JsonParam->BillingAddress2)
                    ->setIsDefaultBilling('1')
                    ->setIsDefaultShipping(false)
                    ->setSaveInAddressBook('1');
            $address->save();

            echo 'Create customer successfully' . $customer->getId(); exit; 
        } else {
            //email already exists 
            echo 'Email already exists'; exit; 
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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