Last active
October 25, 2019 12:29
-
-
Save Marko-M/3684d3838d0565b8af3f to your computer and use it in GitHub Desktop.
Script to re-save Magento store products per store/product type
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 Mmartinovic_Shell_Resave extends Mage_Shell_Abstract | |
{ | |
protected $_stores = []; | |
protected $_types = []; | |
public function __construct() { | |
parent::__construct(); | |
ini_set('memory_limit', '-1'); | |
$stores = Mage::app()->getStores(true, true); | |
if($this->getArg('stores')) { | |
$s = array_flip(explode(',', $this->getArg('stores'))); | |
$this->_stores = array_intersect_key( | |
$stores, | |
array_flip(explode(',', $this->getArg('stores'))) | |
); | |
} | |
if($this->getArg('types')) { | |
$this->_types = array_intersect( | |
explode(',', $this->getArg('types')), | |
[ | |
Mage_Catalog_Model_Product_Type::TYPE_BUNDLE, | |
Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, | |
Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, | |
Mage_Catalog_Model_Product_Type::TYPE_GROUPED, | |
Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL | |
] | |
); | |
} | |
} | |
// Shell script point of entry | |
public function run() { | |
/** @var Zeeandco_Custom_Model_Core_Store $store */ | |
foreach($this->_stores as $store) { | |
printf('Processing "%s" store'.PHP_EOL, $store->getCode()); | |
/** @var Mage_Catalog_Model_Resource_Product_Collection $productCollection */ | |
$productCollection = Mage::getResourceModel('catalog/product_collection'); | |
$productCollection | |
->addAttributeToFilter( | |
[ | |
[ | |
'attribute' => Zeeandco_Custom_Model_Core_Store::STORE_ATTRIBUTE, | |
'eq' => $store->getZeeStoreOptionId() | |
] | |
] | |
) | |
->addAttributeToFilter( | |
[ | |
[ | |
'attribute' => 'type_id', | |
'in' => $this->_types | |
] | |
] | |
); | |
if(!($productIds = $productCollection->getAllIds())) { | |
continue; | |
}; | |
$total = $productCollection->getSize(); | |
$count = 0; | |
foreach($productIds as $productId) { | |
printf('[%d/%d]'.PHP_EOL, ++$count, $total); | |
try { | |
/** @var Mage_Catalog_Model_Product $product */ | |
$product = Mage::getModel('catalog/product') | |
->load($productId); | |
$product->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID) | |
// Schedule the partial reindex regardless to current indexer settings | |
->setIsMassupdate(true) | |
// Do not mess with url rewrites cause nothing changed | |
->setExcludeUrlRewrite(true) | |
->save(); | |
} catch (Exception $e) { | |
printf( | |
'Error while saving product ID "%d" on store code "%d"'.PHP_EOL, | |
$productId, | |
$store->getCode() | |
); | |
} | |
} | |
} | |
} | |
// Usage instructions | |
public function usageHelp() | |
{ | |
$file = basename(__FILE__); | |
return <<<USAGE | |
Usage: php -f $file -- [options] | |
This script sets memory limit to -1. Beware of the memory leak within product model! | |
--stores <store_code_1,store_code2> CSV of store codes to process | |
--types <simple,configurable> CSV of product types to process | |
help This help | |
USAGE; | |
} | |
} | |
// Instantiate | |
$shell = new Mmartinovic_Shell_Resave(); | |
// Initiate script | |
$shell->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment