0

Using Create new customer using CLI like admin approved method as a base I have been pulling my hair out on how to extend it so I can also add an address to the created user via CLI. any assistance is appreciated.

1 Answer 1

0

After a bit of time. customer.php

class Customer extends AbstractHelper
{
    const KEY_EMAIL = 'customer-email';
    const KEY_FIRSTNAME = 'customer-firstname';
    const KEY_LASTNAME = 'customer-lastname';
    const KEY_PASSWORD = 'customer-password';
    const KEY_WEBSITE = 'website';
    const KEY_SENDEMAIL = 'send-email';
    const KEY_CITY = 'city';
    const KEY_TELEPHONE = 'telephone';
    const KEY_STREET = 'street';
    const KEY_POSTCODE = 'postcode';
    const KEY_REGION = 'region';

_construct

        EmailNotificationInterface $emailNotificationInterface,
        AddressInterfaceFactory $dataAddressFactory,
        AddressRepositoryInterface $addressRepository,
        CollectionFactory $collectionFactory,
        \Magento\Customer\Model\Customer $customer

        $this->dataAddressFactory = $dataAddressFactory;
        $this->addressRepository = $addressRepository;
        $this->collectionFactory = $collectionFactory;
        $this->customer = $customer;

createcustomer()
        $city = $this->data->getOption(self::KEY_CITY);
        $telephone = $this->data->getOption(self::KEY_TELEPHONE);
        $street = $this->data->getOption(self::KEY_STREET);
        $postcode = $this->data->getOption(self::KEY_POSTCODE);
        $region = $this->data->getOption(self::KEY_REGION);

    public function saveAddress($customerId, $firstname, $lastname, $street_1, $city, $postcode, $telephone, $region)
    {
        try {
            $address = $this->dataAddressFactory->create();
            $address->setFirstname($firstname)
            ->setLastname($lastname);
            $address->setTelephone($telephone);
            $street[] = $street_1;
            $address->setStreet($street);

            $regionData = $this->getRegionCode($region);
            $address->setCity($city);
            $address->setCountryId($regionData['country_id']);
            $address->setPostcode($postcode);
            $address->setRegionId($regionData['region_id']);
            $address->setIsDefaultShipping(1);
            $address->setIsDefaultBilling(1);
            $address->setCustomerId($customerId);
            $this->addressRepository->save($address);
            $this->exception = false;
        } catch (\Exception $e) {
            $this->logger->critical($e);
            $this->exception = $e;
            return false;
        }
    }

basically details what is required. Hope this helps someone else out.

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.