Skip to content

Instantly share code, notes, and snippets.

@az-iar
Created August 13, 2015 06:19
Show Gist options
  • Save az-iar/5f14bad5a138b6e46157 to your computer and use it in GitHub Desktop.
Save az-iar/5f14bad5a138b6e46157 to your computer and use it in GitHub Desktop.
Magento - Shell Scripts
<?php
require_once 'abstract.php';
class Mage_Shell_Assign_Product_Thumbnails extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
if (!$product->hasImage()) continue;
if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage());
if (!$product->hasThumbnail()) $product->setThumbnail($product->getImage());
$product->save();
}
echo 'Done!'."\n";
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f assign_product_thumbnails.php -- [options]
help This help
USAGE;
}
}
$shell = new Mage_Shell_Assign_Product_Thumbnails();
$shell->run();
<?php
require_once 'abstract.php';
class Mage_Shell_Delete_Orders extends Mage_Shell_Abstract
{
protected $_argname = array();
public function __construct()
{
parent::__construct();
// Time limit to infinity
set_time_limit(0);
// Get command line argument named "argname"
// Accepts multiple values (comma separated)
if ($this->getArg('argname')) {
$this->_argname = array_merge(
$this->_argname,
array_map(
'trim',
explode(',', $this->getArg('argname'))
)
);
}
}
// Shell script point of entry
public function run()
{
// Determine magento edition
if (file_exists('LICENSE_EE.txt')) {
$edition = 'EE';
} elseif (file_exists('LICENSE_PRO.html')) {
$edition = 'PE';
} else {
$edition = 'CE';
}
if (($edition == 'EE' && version_compare(Mage::getVersion(), '1.11.0.0.', '<') === true)
|| ($edition == 'PE' && version_compare(Mage::getVersion(), '1.11.0.0.', '<') === true)
|| ($edition == 'CE' && version_compare(Mage::getVersion(), '1.6.0.0.', '<') === true)
) {
$executionPath = 'old';
} else {
$executionPath = 'new';
}
$xpathEntity = 'global/models/sales_entity/entities//table';
if ($executionPath == 'old') {
$xpathResource = 'global/models/sales_mysql4/entities//table';
} else {
$xpathResource = 'global/models/sales_resource/entities//table';
}
$salesEntitiesConf = array_merge(
Mage::getSingleton('core/config')->init()->getXpath($xpathEntity),
Mage::getSingleton('core/config')->init()->getXpath($xpathResource)
);
$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_write');
/*
* If you want delete System/Order Statuses (Status and State) you
* should comments below lines (46-51)
*/
$skipTables = array(
$resource->getTableName('sales_order_status'),
$resource->getTableName('sales_order_status_state'),
$resource->getTableName('sales_order_status_label')
);
$salesEntitiesConf = array_diff($salesEntitiesConf, $skipTables);
/*
Multiple RDBMS Support in Magento CE 1.6+ / EE 1.11+
http://www.magentocommerce.com/images/uploads/RDBMS_Guide2.pdf
2.2. Adapters:
... The new Varien_DB_Adapter_Interface was added to sign a contract that all
developed adapters must execute in order to get Magento working on an actual
database. The interface describes the list of methods and constants that can be used by resource models...
Used below in the loop:
* If $executionPath == 'old'
* Varien_Db_Adapter_Pdo_Mysql::showTableStatus()
* Varien_Db_Adapter_Pdo_Mysql::truncate()
* Else
* Varien_Db_Adapter_Interface::isTableExists()
* Varien_Db_Adapter_Interface::truncateTable()
*/
while ($table = current($salesEntitiesConf)) {
$table = $resource->getTableName($table);
if ($executionPath == 'old') {
$isTableExists = $connection->showTableStatus($table);
} else {
$isTableExists = $connection->isTableExists($table);
}
if ($isTableExists) {
try {
if ($executionPath == 'old') {
$connection->truncate($table);
} else {
$connection->truncateTable($table);
}
echo "Successfully truncated the $table table. \n";
} catch (Exception $e) {
echo "Error {$e->getMessage()} occurred truncating the $table table. \n";
}
}
next($salesEntitiesConf);
}
echo "All done";
}
// Usage instructions
public function usageHelp()
{
return <<<USAGE
Usage: php -f delete_orders.php -- [options]
help This help
USAGE;
}
}
// Instantiate
$shell = new Mage_Shell_Delete_Orders();
// Initiate script
$shell->run();
<?php
require_once 'abstract.php';
class Mage_Shell_Delete_Products extends Mage_Shell_Abstract
{
protected $_argname = array();
/**
* Mage_Shell_Delete_Products constructor.
*/
public function __construct()
{
parent::__construct();
set_time_limit(0);
// Get command line argument named "argname"
// Accepts multiple values (comma separated)
if($this->getArg('argname')) {
$this->_argname = array_merge(
$this->_argname,
array_map(
'trim',
explode(',', $this->getArg('argname'))
)
);
}
}
/**
* Run script
*
*/
public function run()
{
set_time_limit(0);
$productCollection = Mage::getResourceModel('catalog/product_collection');
/** @var Mage_Catalog_Model_Product_Attribute_Media_Api $mediaApi */
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
foreach ($productCollection as $product) {
echo 'Deleting product : ' . $product->getId() . "\n";
$prodID = $product->getId();
/** @var Mage_Catalog_Model_Product $_product */
$_product = Mage::getModel('catalog/product')->load($prodID);
$items = $mediaApi->items($_product->getId());
if (count($items) > 0) {
foreach ($items as $item) {
$mediaApi->remove($_product->getId(), $item['file']);
}
echo $product->getId() . " done \n";
} else {
echo $product->getId() . " has no images \n";
}
$_product->delete();
echo 'Product ' . $prodID . ' has been deleted'. "\n";
}
echo 'Done!' . "\n";
}
// Usage instructions
public function usageHelp()
{
return <<<USAGE
Usage: php -f delete_products.php -- [options]
help This help
USAGE;
}
}
$shell = new Mage_Shell_Delete_Products();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment