-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a couple of customer related commands
- Loading branch information
Showing
4 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace MahoCLI\Commands; | ||
|
||
use Mage; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\QuestionHelper; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
#[AsCommand( | ||
name: 'customer:change-password', | ||
description: 'Change password of a customers' | ||
)] | ||
class CustomerChangepassword extends BaseMahoCommand | ||
{ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$this->initMaho(); | ||
|
||
/** @var QuestionHelper $questionHelper */ | ||
$questionHelper = $this->getHelper('question'); | ||
|
||
$question = new Question('Email: ', ''); | ||
$email = $questionHelper->ask($input, $output, $question); | ||
$email = trim($email); | ||
if (!strlen($email)) { | ||
$output->writeln('<error>Email cannot be empty</error>'); | ||
return Command::INVALID; | ||
} | ||
|
||
$question = new Question('New password: ', ''); | ||
$password = $questionHelper->ask($input, $output, $question); | ||
$password = trim($password); | ||
if (!strlen($password)) { | ||
$output->writeln('<error>New password cannot be empty</error>'); | ||
return Command::INVALID; | ||
} | ||
|
||
$customerAttributes = ['entity_id', 'website_id', 'email', 'firstname', 'lastname']; | ||
$customers = Mage::getResourceModel('customer/customer_collection') | ||
->addAttributeToSelect($customerAttributes) | ||
->addAttributeToFilter('email', ['eq' => $email]); | ||
|
||
if ($customers->getSize() == 0) { | ||
$output->writeln("<error>Customer not found.</error>"); | ||
return Command::FAILURE; | ||
} | ||
|
||
if ($customers->getSize() == 1) { | ||
foreach ($customers as $customer) { | ||
$customer->setPassword($password); | ||
$customer->save(); | ||
$output->writeln("<info>Customer $email deleted.</info>"); | ||
return Command::SUCCESS; | ||
} | ||
} | ||
|
||
$output->writeln(''); | ||
$output->writeln("<comment>More than one customer matches your search:</comment>"); | ||
$table = new Table($output); | ||
$table->setHeaders($customerAttributes); | ||
|
||
foreach ($customers as $customer) { | ||
$table->addRow([ | ||
$customer->getEntityId(), | ||
$customer->getWebsiteId(), | ||
$customer->getEmail(), | ||
$customer->getFirstname(), | ||
$customer->getLastname() | ||
]); | ||
} | ||
$table->render(); | ||
|
||
$question = new Question('Type the ID of the customer you wish to change the password for: ', ''); | ||
$customerId = $questionHelper->ask($input, $output, $question); | ||
$customerId = trim($customerId); | ||
|
||
$customer = Mage::getModel('customer/customer') | ||
->load($customerId); | ||
if (!$customer->getId()) { | ||
$output->writeln("<error>Customer not found.</error>"); | ||
return Command::FAILURE; | ||
} | ||
|
||
$customer->setPassword($password); | ||
$customer->save(); | ||
$output->writeln("<info>Customer $email deleted.</info>"); | ||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace MahoCLI\Commands; | ||
|
||
use Mage; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\QuestionHelper; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ChoiceQuestion; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
#[AsCommand( | ||
name: 'customer:create', | ||
description: 'Create a customer' | ||
)] | ||
class CustomerCreate extends BaseMahoCommand | ||
{ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$this->initMaho(); | ||
|
||
/** @var QuestionHelper $questionHelper */ | ||
$questionHelper = $this->getHelper('question'); | ||
|
||
$question = new Question('Email: ', ''); | ||
$email = $questionHelper->ask($input, $output, $question); | ||
$email = trim($email); | ||
if (!strlen($email)) { | ||
$output->writeln('<error>Email cannot be empty</error>'); | ||
return Command::INVALID; | ||
} | ||
|
||
$question = new Question('Password: ', ''); | ||
$password = $questionHelper->ask($input, $output, $question); | ||
$password = trim($password); | ||
if (!strlen($password)) { | ||
$output->writeln('<error>Password cannot be empty</error>'); | ||
return Command::INVALID; | ||
} | ||
|
||
$question = new Question('Firstname: ', ''); | ||
$firstname = $questionHelper->ask($input, $output, $question); | ||
$firstname = trim($firstname); | ||
if (!strlen($firstname)) { | ||
$output->writeln('<error>Firstname cannot be empty</error>'); | ||
return Command::INVALID; | ||
} | ||
|
||
$question = new Question('Lastname: ', ''); | ||
$lastname = $questionHelper->ask($input, $output, $question); | ||
$lastname = trim($lastname); | ||
if (!strlen($lastname)) { | ||
$output->writeln('<error>Lastname cannot be empty</error>'); | ||
return Command::INVALID; | ||
} | ||
|
||
$websites = []; | ||
$websitesCollection = Mage::getResourceModel('core/website_collection'); | ||
foreach ($websitesCollection as $website) { | ||
$websites[$website->getId()] = "{$website->getCode()} - {$website->getName()}"; | ||
} | ||
$question = new ChoiceQuestion('Website: ', $websites); | ||
$website = $questionHelper->ask($input, $output, $question); | ||
$website = explode(" - ", $website)[0]; | ||
$website = Mage::getModel('core/website')->load($website, 'code'); | ||
if (!$website->getId()) { | ||
$output->writeln('<error>Website not found</error>'); | ||
return Command::INVALID; | ||
} | ||
|
||
$customer = Mage::getModel('customer/customer') | ||
->setEmail($email) | ||
->setPassword($password) | ||
->setFirstname($firstname) | ||
->setLastname($lastname) | ||
->setWebsiteId($website->getId()) | ||
->save(); | ||
|
||
if (!$customer->getId()) { | ||
$output->writeln('<error>Unable to create customer</error>'); | ||
return Command::FAILURE; | ||
} | ||
|
||
$output->writeln("<info>Customer $email create successfully.</info>"); | ||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters