Skip to content

Instantly share code, notes, and snippets.

@the-nerdery-dot-info
Forked from Zifius/cleanup.php
Created June 23, 2016 09:45
Show Gist options
  • Save the-nerdery-dot-info/4729fb931947a6b681e32e36a6cc754e to your computer and use it in GitHub Desktop.
Save the-nerdery-dot-info/4729fb931947a6b681e32e36a6cc754e to your computer and use it in GitHub Desktop.
Shell script to remove unassigned simple products
<?php
/**
* This script is used to clean up unassigned simple products
*
* @author Alexander Turiak <[email protected]>
* @copyright Copyright (c) 2014 HexBrain (http://www.hexbrain.com)
*/
require_once 'abstract.php';
class CleanUp extends Mage_Shell_Abstract
{
public function run()
{
$dryRun = $this->getArg('dry-run');
// Get all the simple product ids
$allSimples = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => Mage_Catalog_Model_Product_Type::TYPE_SIMPLE))
->getAllIds();
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
// Get all the assigned product ids
$select = $readConnection->select()
->from($resource->getTableName('catalog/product_super_link'), array('product_id'))
->where('product_id IN (?)', $allSimples);
$allAssigned = $readConnection->fetchCol($select);
// All unassigned products
$allUnAssigned = array_diff($allSimples, $allAssigned);
if (count($allUnAssigned) == 0) {
echo 'Nothing to do here: no unassigned products to remove' . PHP_EOL;
return;
}
if ($dryRun == 'no') {
echo 'This will actually remove products. WARNING: There is no rollback' . PHP_EOL . PHP_EOL;
}
$product = Mage::getModel('catalog/product');
foreach ($allUnAssigned as $productId) {
$product->load($productId);
if ($dryRun == 'no') {
echo 'Removing unassigned product ' . $product->getSku() . PHP_EOL;
$product->delete();
} else {
echo 'Product ' . $product->getSku() . ' will be removed' . PHP_EOL;
}
}
if ($dryRun == 'no') {
echo PHP_EOL . 'Done.' . PHP_EOL;
} else {
echo PHP_EOL . $this->usageHelp();
}
}
public function usageHelp()
{
return <<<USAGE
Usage: php -f cleanup.php -- [options]
--dry-run Dry run, doesn't actually delete products (default mode)
Pass "no" as value to bypass
USAGE;
}
}
$cleanup = new CleanUp();
$cleanup->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment