user2367751
user2367751

Reputation: 1

Magmi import update option

does Magmi has a option to disable or make quantity to 0 for products not in csv file in import?. Because my supplier remove all out of stock products from csv file. If someone can help me on this or finding another solution please. Thank you in advance

Upvotes: 0

Views: 1246

Answers (1)

Axel
Axel

Reputation: 10772

I've created a plugin that disables files not in the CSV. I prefer disabling the items, instead of actually deleting them in case something goes wrong (it won't wipe my database).

  1. Create the plugin file magmi/plugins/extra/general/itemdisabler/magmi_itemdisabler_plugin.php

  2. In the file, paste in the following and save:

Plugin Code:

<?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");       
        }

    }
}

Then login to Magmi, enable the plugin and run the import. This plugin will execute after the import has completed. It opens the datasource, logs all of the SKUs, then compares them against the Magento database. Any skus that aren't found in the datasource are disabled. This plugin could be optimized a bit better but it works as it is right now.

Upvotes: 4

Related Questions