删除Magento中的测试订单

redmaomail 2024-07-24 18:12 阅读数 62 #Magento

红帽云邮外贸主机

当你开发一个Magento项目,你会创建一个Magento主题,放置初始产品和类别,还放置一些订单来测试送货方式和支付方法的正常运行。一切都很好,客户也想要看看网站。可是运行中,你将看到一些本应该删除的测试订单。

当你想在后台删除订单的时候你会发现,你只能将订单状态改为“取消”,而订单仍然在那里。Magento不允许我们通过后台来删除这些,所以你看不到任何“删除订单”按钮。这对商家和开发人员来说都是让人沮丧的。来自SAP的人发现不能删除订单有些好处,但是应该有一个状态来从报告中消除销售、库存等信息的销售计数。

那么,怎么做呢?

你可以在你的Magento根目录下创建一个php脚本,粘贴下面的代码:

<?php
/**
* @copyright Alwayly
*/

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...');

代码执行后,数据库里再也不会有测试订单了。记住,这将删除所有数据库中订单。

电商网站开发服务


红帽云邮外贸主机

分享到:
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。
    红帽云邮外贸主机
热门
    红帽云邮外贸主机
    红帽云邮外贸主机