magento - Magmi 有没有插件可以删除 CSV 文件中没有的产品?

标签 magento magmi

我必须添加这个功能,我想在编写一个插件之前知道是否存在这样的插件。

最佳答案

我创建了一个插件来禁用不在 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 数据库进行比较。在数据源中找不到的任何 skus 都将被禁用。这个插件可以优化一点,但它现在可以正常工作。

    关于magento - Magmi 有没有插件可以删除 CSV 文件中没有的产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8669688/

    相关文章:

    magento - Magmi安装后授权

    mysql - 使用 Magmi 导入 Magento 产品 - 访问数据库被拒绝

    php - Magento 中过滤器搜索时的自定义网格中未返回产品名称

    magento - 通过 Magmi 在 Magento 中配置 super 产品属性

    image - Magmi 无法找到多个图像

    magento - 使用 magmi 导入可配置产品

    magento - 检索属性代码

    php - 如何编辑Magento的API函数 "items()"获取商品的ImageUrl

    Magento 只读和隐藏产品属性

    Magento 支付工作流程和事件订单已支付