2013-05-09 43 views
0

Magmi是否有一個選項可以禁用或將數量設爲0,以導入不在csv文件中的導入產品? 因爲我的供應商從csv文件中刪除所有缺貨產品。 如果有人可以幫助我或找到另一種解決方案,請。 謝謝你提前Magmi導入更新選項

回答

4

我創建了一個插件,禁用文件不在CSV中。我更喜歡禁用這些項目,而不是實際刪除它們以防發生錯誤(它不會擦除我的數據庫)。

  1. 在下面創建插件文件magmi/plugins/extra/general/itemdisabler/magmi_itemdisabler_plugin.php

  2. 在該文件中,粘貼並保存:

插件代碼:

<?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都被禁用。這個插件可以優化得更好一些,但它現在可以正常工作。

+0

像魅力一樣工作完美。非常感謝你 – user2367751 2013-05-10 01:22:01