跨境电商
经验交流分享

Magento删除订单

对于Magento程序员来说,经常需要在开发过程中测试订单系统是否达到项目需求,于是后台中就会保存很多测试订单。当程序上线的时候,我们需要将订单全部删除,保证购物车程序的正式运行。然后Magento订单系统完全基于自动化的处理,并且为了安全,更是没有删除订单的功能!那么如何解决该问题呢?网上有许多Magento订单删除插件,可以实现删除订单的功能。

当然,作为程序员的你来说,可能不想使用别人的插件来增加程序的不确定性,更想自己能够完全掌握代码。那么这里提到的解决办法更适合你。另外,此方法只适用于清楚测试订单,并作为Magento删除订单的一种演示方式,它将会删除所有的系统订单。所以不能用于实际代码当中,只作为学习之用。

在Magento根目录中创建PHP文件,任意命名,并复制如下代码到该文件当中,然后在浏览器中运行。

[php]

/**
* @author Dejan Radic <dejan.radic@inchoo.net>
*/

if (version_compare(phpversion(), ‘5.2.0’, ‘<‘)===true) {
echo  ‘<div style="font:12px/1.35em arial, helvetica, sans-serif;"><div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;"><h3 style="margin:0; font-size:1.7em; font-weight:normal; text-transform:none; text-align:left; color:#2f2f2f;">Whoops, it looks like you have an invalid PHP version.</h3></div><p>Magento supports PHP 5.2.0 or newer. <a href="http://www.magentocommerce.com/install" target="">Find out</a> how to install</a> Magento using PHP-CGI as a work-around.</p></div>’;
exit;
}

error_reporting(E_ALL | E_STRICT);
ini_set(‘display_errors’, 1);

$mageFilename = ‘app/Mage.php’;

if (!file_exists($mageFilename)) {
echo $mageFilename." was not found";
exit;
}

require_once $mageFilename;

Mage::app();

$executionPath = null;

/*
* 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);
}

printf(‘Successfully truncated the <i style="color:green;">%s</i> table.<br />’, $table);
} catch(Exception $e) {
printf(‘Error <i style="color:red;">%s</i> occurred truncating the <i style="color:red;">%s</i> table.<br />’, $e->getMessage(), $table);
}
}

next($salesEntitiesConf);
}

exit(‘All done…’);

[/php]

再次提醒,该脚本运行之后将会删除所有的系统订单!以下Magento版本适用:1.6.1.0 CE, 1.6.0.0 CE, 1.5.1.0. CE, 1.11.0.2 EE, 1.10.0.2 EE, 1.9.0.0 EE, 1.11.0.0 PE, 1.10.0.2 PE and 1.9.0.0 PE.

赞(0)
未经允许不得转载:锐想 » Magento删除订单
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

锐想电商 - 跨境电商经验交流分享

锐想无限