Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to create addresses for customer #1408

Merged
merged 8 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ Deletes customer(s) by given id or a combination of the website id and email or
In addition, you can delete a range of customer ids or delete all customers.


### Add customer address

```sh
n98-magerun2.phar customer:add-address [email] [website] [--firstname=STRING] [--lastname=STRING] [--street=STRING] [--city=STRING] [--country=STRING] [--postcode=STRING] [--telephone=STRING] [--default-billing] [--default-shipping]
```

Examples:

```sh
n98-magerun2.phar customer:add-address [email protected] base --firstname="John" --lastname="Doe" --street="Pariser Platz" --city="Berlin" --country="DE" --postcode="10117" --telephone="1234567890" # add address of brandenburger tor to customer with email "[email protected]" in website "base"
n98-magerun2.phar customer:add-address [email protected] base --firstname="John" --lastname="Doe" --street="Pariser Platz" --city="Berlin" --country="DE" --postcode="10117" --telephone="1234567890" --default-billing --default-shipping # add address of brandenburger tor to customer with email "[email protected]" in website "base" as default billing and shipping
```

Adds a customer address to given customer defined by email and website
---

### Magento Installer
Expand Down
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ commands:
- N98\Magento\Command\Config\Data\MViewCommand
- N98\Magento\Command\Config\Data\IndexerCommand
- N98\Magento\Command\Customer\CreateCommand
- N98\Magento\Command\Customer\AddAddressCommand
- N98\Magento\Command\Customer\DeleteCommand
- N98\Magento\Command\Customer\ChangePasswordCommand
- N98\Magento\Command\Customer\InfoCommand
Expand Down
149 changes: 149 additions & 0 deletions src/N98/Magento/Command/Customer/AddAddressCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace N98\Magento\Command\Customer;

use Exception;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

/**
* Class: AddAddressCommand
*/
class AddAddressCommand extends AbstractCustomerCommand
{
/** @var CustomerRepositoryInterface */
private CustomerRepositoryInterface $customerRepository;

/**
* Method: configure
*
* @return void
*/
protected function configure()
{
$this
->setName('customer:add-address')
->setDescription('Adds an address to a customer')
->addArgument('email', InputArgument::REQUIRED, 'Customer email')
->addArgument('website', InputArgument::REQUIRED, 'Customer website')
->addOption('firstname', null, InputOption::VALUE_REQUIRED, 'First name')
->addOption('lastname', null, InputOption::VALUE_REQUIRED, 'Last name')
->addOption('street', null, InputOption::VALUE_REQUIRED, 'Street address')
->addOption('city', null, InputOption::VALUE_REQUIRED, 'City')
->addOption('country', null, InputOption::VALUE_REQUIRED, 'Country ID, e.g., US')
->addOption('postcode', null, InputOption::VALUE_REQUIRED, 'Postcode')
->addOption('telephone', null, InputOption::VALUE_REQUIRED, 'Telephone number')
->addOption('default-billing', null, InputOption::VALUE_NONE, 'Use as default billing address')
->addOption('default-shipping', null, InputOption::VALUE_NONE, 'Use as default shipping address');
}

/**
* Method: inject
*
* @param CustomerRepositoryInterface $customerRepository
* @return void
*/
public function inject(CustomerRepositoryInterface $customerRepository)
{
$this->customerRepository = $customerRepository;
}

/**
* Method: execute
*
* @param InputInterface $input
* @param OutputInterface $output
* @return mixed
* @throws LocalizedException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->detectMagento($output, true);
if (!$this->initMagento()) {
return Command::FAILURE;
}

$isError = false;

/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelperSet()->get('question');

// Email is a required argument, so we check and ask if it's not provided
$email = $input->getArgument('email');
if (!$email) {
$question = new Question('Please enter the customer\'s email: ');
$email = $questionHelper->ask($input, $output, $question);
}

$website = $this->getHelperSet()->get('parameter')->askWebsite($input, $output);

// Collecting options with possibility of interactive input
$options = ['firstname', 'lastname', 'street', 'city', 'country', 'postcode', 'telephone'];
$data = [];
foreach ($options as $option) {
$value = $input->getOption($option);
if (!$value) {
$question = new Question("Please enter the customer's $option: ");
$value = $questionHelper->ask($input, $output, $question);
}
$data[$option] = $value;
}

// Handling boolean options for default billing and shipping addresses
$defaultOptions = ['default-billing', 'default-shipping'];
foreach ($defaultOptions as $option) {
$value = $input->getOption($option);
if (null === $value) {
$question = new ConfirmationQuestion("Set address as customer's $option? (yes/no): ", false);
$value = $questionHelper->ask($input, $output, $question);
}
$data[$option] = $value;
}

// Now, load the customer by email within the context of the website
try {
$customer = $this->customerRepository->get($email, $website->getId());
} catch (NoSuchEntityException $e) {
$websiteCode = $website->getCode();
$output->writeln("<error>Customer with email '$email' not found in website '$websiteCode'.</error>");
return Command::FAILURE;
}

/** @var AddressInterfaceFactory $addressFactory */
$addressFactory = $this->getObjectManager()->get(AddressInterfaceFactory::class);
$address = $addressFactory->create();
$address->setFirstname($data['firstname'])
->setLastname($data['lastname'])
->setStreet([$data['street']])
->setCity($data['city'])
->setCountryId($data['country'])
->setPostcode($data['postcode'])
->setTelephone($data['telephone'])
->setIsDefaultBilling($data['default-billing'])
->setIsDefaultShipping($data['default-shipping']);

$mergedAddresses = array_merge($customer->getAddresses(), [$address]);

$customer->setAddresses($mergedAddresses);

try {
$this->customerRepository->save($customer);
$output->writeln('<info>Address added successfully to customer ' . $email . '.</info>');
} catch (Exception $e) {
$isError = true;
$output->writeln('<error>Error adding address: ' . $e->getMessage() . '</error>');
}

return $isError ? Command::FAILURE : Command::SUCCESS;
}
}
5 changes: 5 additions & 0 deletions tests/bats/functional_magerun_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ function cleanup_files_in_magento() {
assert_output --partial "Password successfully changed"
}

@test "Command: customer:add-address" {
run $BIN "customer:add-address" [email protected] base --firstname="John" --lastname="Doe" --street="Pariser Platz" --city="Berlin" --country="DE" --postcode="10117" --telephone="1234567890" --default-billing --default-shipping
assert_output --partial "Address added successfully to customer [email protected]"
}

@test "Command: customer:delete" {
run $BIN "customer:delete" --fuzzy --email=foo --force
assert_output --partial "Successfully deleted 1 customer/s"
Expand Down
Loading