Last active
April 16, 2019 15:18
-
-
Save MahmoudDolah/c83fa89b60017f6707f34dfe1be9eaf5 to your computer and use it in GitHub Desktop.
A Magento shell script to mass delete customer accounts created by spam
This file contains hidden or 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
<?php | |
require_once 'abstract.php'; | |
class Mage_Shell_Customer extends Mage_Shell_Abstract | |
{ | |
/** | |
* Run script | |
* | |
*/ | |
public function run() | |
{ | |
$collection = Mage::getModel('customer/customer')->getCollection() | |
->addAttributeToSelect('firstname') | |
->addAttributeToSelect('lastname') | |
->addAttributeToSelect('email'); | |
if (!$this->getArg('delete') && !$this->getArg('print')) { | |
printf("ERROR: Either --delete or --print must be specified\n"); | |
exit(1); | |
} | |
if ($name = $this->getArg('firstname')) | |
$collection->addAttributeToFilter('firstname', array('like' => $name)); | |
if ($name = $this->getArg('lastname')) | |
$collection->addAttributeToFilter('lastname', array('like' => $name)); | |
if ($name = $this->getArg('email')) | |
$collection->addAttributeToFilter('email', array('like' => $name)); | |
foreach ($collection as $item) { | |
if ($this->getArg('delete')) { | |
printf("Deleting ID:%s\n", $item->getId()); | |
$item->delete(); | |
} else { | |
printf("%s\t%s\n", | |
str_pad($item->getFirstname() . $item->getLastname(), 50), | |
$item->getEmail() | |
); | |
} | |
} | |
} | |
/** | |
* Retrieve Usage Help Message | |
* | |
*/ | |
public function usageHelp() | |
{ | |
return <<<USAGE | |
Usage: php -f customer.php -- [options] | |
--firstname Filter by first name | |
--lastname Filter by last name | |
--email Filter by email | |
--delete Delete matching users | |
--print Display matching users | |
Examples: | |
php shell/customer.php --lastname "%qq.com%" --list | |
php shell/customer.php --email "%.ru" --delete | |
USAGE; | |
} | |
} | |
$customer = new Mage_Shell_Customer(); | |
$customer->run(); |
There is an mistake in the script. When you use the option email, it still searches for lastname.
Updated
if ($name = $this->getArg('email'))
$collection->addAttributeToFilter('email', array('like' => $name));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is an mistake in the script. When you use the option email, it still searches for lastname.