Skip to content

Feature import product with category id, instead of category name #425

Description

@zhhank

I noticed that import categories is kind of slow, not much faster than magento build-in category model save() in my case. (I am not sure why that happen, maybe the event trigger the reindex processing... )
So I prefer to import products with category id, to avoid loading whole categories from the database.
Here is my quick and dirty fix, hope this can help someone:

--- a/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category/Product.php
+++ b/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category/Product.php
@@ -62,6 +62,12 @@
     protected $_skuEntityIds = array();
 
     /**
+     * 
+     * use category ids.
+    **/
+    protected $_isCatgoryids = true;
+    
+    /**
      * Checks if a category has already been cleaned out (delete from catalog_category_product where category_id=X
      *
      * @var array
@@ -232,6 +238,9 @@
      */
     protected function _initCategories()
     {
+        if ($this->_isCatgoryids){
+            return $this;
+        }
         $collection = Mage::getResourceModel('catalog/category_collection')->addNameToResult();
         /* @var $collection Mage_Catalog_Model_Resource_Category_Collection */
 
@@ -328,9 +337,15 @@
                 $rowData = $this->_prepareRowForDb($rowData);
 
                 // entity table data
+                if ($this->_isCatgoryids) {
+                    //customized for catgorids.
+                     $catId = empty($rowData[self::COL_CATEGORY]) ? '' : $rowData[self::COL_CATEGORY];
+                    
+                } else {
                 $catId     = isset($this->_categoriesWithRoots[$rowData[self::COL_ROOT]][$rowData[self::COL_CATEGORY]])
                     ? (int)$this->_categoriesWithRoots[$rowData[self::COL_ROOT]][$rowData[self::COL_CATEGORY]]['entity_id']
                     : 0;
+                }
                 $prodId    = isset($this->_skuEntityIds[$rowData[self::COL_SKU]])
                     ? (int)$this->_skuEntityIds[$rowData[self::COL_SKU]]
                     : 0;
@@ -441,6 +456,10 @@
      */
     public function validateRow(array $rowData, $rowNum)
     {
+        if ($this->_isCatgoryids){
+                return true;
+        }
+        
         static $root = NULL;
         static $category = NULL;
--- a/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php
+++ b/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php
@@ -90,6 +90,12 @@
     protected $_mediaValueTableName;
 
     /**
+     * 
+     * use category ids.
+    **/
+    protected $_isCatgoryids = true;
+
+    /**
      * Attributes with index (not label) value.
      *
      * @var array
@@ -503,6 +509,10 @@
      */
     protected function _initCategories()
     {
+        if ($this->_isCatgoryids){
+            return $this;
+        }
+                
         $transportObject = new Varien_Object();
         Mage::dispatchEvent( 'avs_fastsimpleimport_entity_product_init_categories', array('transport' => $transportObject) );
 
@@ -1117,15 +1127,17 @@
                 }
 
                 // 3. Categories phase
+                if ($this->_isCatgoryids) {
+                    $categories[$rowSku][$rowData[self::COL_CATEGORY]] = true;
+                } else {
                 $categoryPath = empty($rowData[self::COL_CATEGORY]) ? '' : $rowData[self::COL_CATEGORY];
                 if (!empty($rowData[self::COL_ROOT_CATEGORY])) {
-                    $categoryId = $this->_categoriesWithRoots[$rowData[self::COL_ROOT_CATEGORY]][$categoryPath];
+                        $categoryId = $this->_categoriesWithRoots[$rowData[self::COL_ROOT_CATEGORY]][self::COL_CATEGORY];
                     $categories[$rowSku][$categoryId] = true;
                 } elseif (!empty($categoryPath)) {
                     $categories[$rowSku][$this->_categories[$categoryPath]] = true;
-                } elseif (array_key_exists(self::COL_CATEGORY, $rowData)) {
-                    $categories[$rowSku] = array();
                 }
+                }
 
                 if (!empty($rowData['_tier_price_website'])) { // 4.1. Tier prices phase
                     $tierPrices[$rowSku][] = array(
@@ -1624,7 +1636,11 @@
     protected function _validate($rowData, $rowNum, $sku)
     {
         $this->_isProductWebsiteValid($rowData, $rowNum);
+        if ($this->_isCatgoryids){
+                
+        } else {
         $this->_isProductCategoryValid($rowData, $rowNum);
+        }
         $this->_isTierPriceValid($rowData, $rowNum);
         $this->_isGroupPriceValid($rowData, $rowNum);
         $this->_isSuperProductsSkuValid($rowData, $rowNum);
@@ -1804,4 +1820,5 @@
         }
         return false;
     }
+
 }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions