我必须添加此功能,并且我想在编写之前知道是否存在这样的插件。
5 回答
我创建了一个插件来禁用不在 CSV 中的文件。我更喜欢禁用这些项目,而不是在出现问题时实际删除它们(它不会擦除我的数据库)。
创建插件文件
magmi/plugins/extra/general/itemdisabler/magmi_itemdisabler_plugin.php
在文件中,粘贴以下内容并保存:
插件代码:
<?php
class Magmi_ItemdisablerPlugin extends Magmi_ItemProcessor
{
protected $datasource_skus = array();
public function getPluginInfo()
{
return array("name"=>"Magmi Magento Item Disabler",
"author"=>"Axel Norvell (axelnorvell.com)",
"version"=>"1.0.6");
}
public function afterImport()
{
$this->log("Running Item Disabler Plugin","info");
$this->disableItems();
return true;
}
public function getPluginParams($params)
{
return array();
}
public function isRunnable()
{
return array(true,"");
}
public function initialize($params)
{
}
public function processItemAfterId(&$item,$params=null)
{
if(isset($item['sku']))
{
$this->datasource_skus[] = $item['sku'];
}
}
public function disableItems()
{
if(count($this->datasource_skus) <= 0)
{
$this->log('No items were found in datasource. Item Disabler will not run.', "info");
return false; /* Nothing to disable */
}
//Setup tables
$ea = $prefix!=""?$prefix."eav_attribute":"eav_attribute";
$eet = $prefix!=""?$prefix."eav_entity_type":"eav_entity_type";
$cpe = $prefix!=""?$prefix."catalog_product_entity":"catalog_product_entity";
$cpei = $prefix!=""?$prefix."catalog_product_entity_int":"catalog_product_entity_int";
//Get "status" attribute_id
$status_attr_id = "
SELECT ea.attribute_id FROM $ea ea
LEFT JOIN $eet eet ON ea.entity_type_id = eet.entity_type_id
WHERE ea.attribute_code = 'status'
AND eet.entity_type_code = 'catalog_product'";
$result = $this->selectAll($status_attr_id);
if (count($result) == 1) {
$attribute_id = $result[0]['attribute_id'];
}
unset($result);
//Get all active items
$sql = "SELECT e.sku, e.entity_id FROM $cpei i
INNER JOIN $cpe e ON
e.entity_id = i.entity_id
WHERE attribute_id=?
AND i.value = 1";
$all_magento_items = $this->selectAll($sql, array($attribute_id));
//Setup the magento_skus array for easy processing.
$magento_skus = array();
foreach($all_magento_items as $item)
{
$this->log("{$item['sku']} found in Mage", "info");
$magento_skus[$item['sku']] = $item['entity_id'];
}
//process the array, move anything thats in the datasource.
foreach($this->datasource_skus as $sku)
{
if(isset($magento_skus[$sku]))
{
unset($magento_skus[$sku]);
}
}
if(!empty($magento_skus))
{
foreach($magento_skus as $sku => $id)
{
$this->log("Disabling Item Id $id with SKU: $sku", "info");
$this->update("
UPDATE $cpei i
INNER JOIN $cpe e ON
e.entity_id = i.entity_id
SET VALUE = '2'
WHERE attribute_id = ?
AND i.value = 1
AND e.sku=?", array($attribute_id, $sku));
}
}
else
{
//If the Datasource contains all Magento's items.
$this->log('All items present in datasource. No items to disable.', "info");
}
}
}
然后登录 Magmi,启用插件并运行导入。此插件将在导入完成后执行。它打开数据源,记录所有 SKU,然后将它们与 Magento 数据库进行比较。在数据源中找不到的任何 sku 都将被禁用。这个插件可以优化一点,但它现在可以正常工作。
I would think it much better to affirmatively delete rather than delete by omission. It should be trivial to adapt the convert adapter to parse a column (e.g. "deleted") and set the _isDeleted
property to true
. This will cause the product to be deleted on save.
Ref Mage_Core_Model_Abstract
and Varien_Object
.
我想我会提到,如果人们有一个非常大的产品列表并且他们试图运行这个禁用程序,他们可能会遇到问题。我对 magmi 不是很熟悉,但是在使用 cli 时似乎会显示更多错误,因此如果您发现禁用不起作用,请尝试使用它进行测试。它对我不起作用,我将其缩小到 php 内存限制。我不得不更改脚本以包含 ini_set('memory_limit','512M'); 在 disableItems 函数中。
所以,我的现在看起来像这样......
public function disableItems()
{
ini_set('max_execution_time', 0);
set_time_limit(0);
ini_set('memory_limit','512M');
umask(0);
如果他们对此有任何问题,我希望这对任何人都有帮助。我还必须设置我的运行,只要它需要和一切。最好在您的配置文件中设置它,但这是一个快速的解决方案,尤其是如果您在共享主机上。
我已经尝试使用内置的产品删除器,但它并没有像预期的那样工作。它会删除旧产品,但当它再次添加它们时会增加商品编号,因此在几次导入后,您的商品编号为 1xxxxx。
如果我只有从 CSV 导入的产品而没有“始终”的产品,我实际上在 magmi 之前使用 php 脚本来删除所有产品:
<?php
$mysqli = new mysqli("localhost", "dbuser", "dbpassword", "db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE `catalog_product_bundle_price_index`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_bundle_selection_price`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_group_price`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_flat_1`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_category_product_index`;
TRUNCATE TABLE `catalog_product_index_price`;
TRUNCATE TABLE `catalog_product_index_eav`;
TRUNCATE TABLE `catalog_category_product`;
TRUNCATE TABLE `catalog_product_index_eav_idx`;
TRUNCATE TABLE `catalog_product_entity`;
TRUNCATE TABLE `catalog_product_relation`;
TRUNCATE TABLE `catalog_product_index_price_idx`;
TRUNCATE TABLE `catalog_product_index_website`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;
TRUNCATE TABLE `core_url_rewrite`;
SET FOREIGN_KEY_CHECKS = 1;
";
$results = mysqli_multi_query($mysqli,$query);
$mysqli->close();
?>
希望有帮助。
这是一个 Magmi 插件,它完全符合您对 imo 的要求:http ://www.emvee-solutions.com/blog/magmi-delete-disable-products-missing-csv-source/