How To - Import manufacturers or any other Option Attribute Set

Last modified by artistan on Fri, May 27, 2011 16:49
Source|Old Revisions  

Sometime when you need to deal with import of thousands of Manufacturers, then manual data entry is really a overhead and tedious.

In such case you can follow the following simple steps for the import of manufacturers Via CLI (command line interface):

Based On This Article....

http://www.magentocommerce.com/wiki/import-export_and_data_manipulation/import_manufacturers Import Manufacturers

This one will also check for existing options on that attribute and NOT duplicate them!!

Steps

Follow the following steps:

1> Create a file in the root/shell/ of magento: attributeimport.php

2> Write the following code there:

  1. <?php
  2.         //////////////////////////////////// RESOURCES TO LEARN HOW THIS CAN/COULD/SHOULD WORK ////////////////////////////////////////////
  3.         /// http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/
  4.         /// http://stackoverflow.com/questions/3275009/magento-getting-attributes-from-an-attribute-set-without-a-product
  5.         /// http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections/
  6.         /// ** http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/get_manufacturers_and_any_attribute_list
  7.         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. /**
  9. * Magento
  10. *
  11. * NOTICE OF LICENSE
  12. *
  13. * This source file is subject to the Open Software License (OSL 3.0)
  14. * that is bundled with this package in the file LICENSE.txt.
  15. * It is also available through the world-wide-web at this URL:
  16. * http://opensource.org/licenses/osl-3.0.php
  17. *
  18. * DISCLAIMER
  19. * Provided AS IS, no warranty.
  20. *
  21. * @category    Mage
  22. * @package     Mage_Shell
  23. * @copyright   Copyright (c) 2011 Charles Peterson, BrokerBin.com
  24. * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  25. */
  26.  
  27. require_once 'abstract.php';
  28.  
  29. /**
  30. * Magento Attribute Import Shell Script
  31. *
  32. * @category    Mage
  33. * @package     Mage_Shell
  34. * @author      Charles Peterson <cpeterson@brokerbin.com>
  35. */
  36. class Mage_Shell_Attributeimport extends Mage_Shell_Abstract
  37. {
  38.     /**
  39.      * Attribute name with "option" value
  40.      *
  41.      * @var string
  42.      */
  43.     protected $_attribute_with_option_value;
  44.     /**
  45.      * Attribute Object
  46.      *
  47.      * @var eav/entity_attribute
  48.      */
  49.     protected $_attribute;
  50.  
  51.     /**
  52.      * Retrieve Attribute Object
  53.      *
  54.      * @return eav/entity_attribute
  55.      */
  56.     protected function _getAttribute()
  57.     {
  58.         if (is_null($this->_attribute_with_option_value)) {
  59.             $this->_log = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $this->_attribute_with_option_value);;
  60.         }
  61.         return $this->_attribute_with_option_value;
  62.     }
  63.  
  64.     /**
  65.      * Run script
  66.      *
  67.      */
  68.     public function run()
  69.     {
  70.         if ($this->getArg('attribute') && $this->getArg('file')) {
  71.             try{
  72.                 // need the product typeid to filter the results
  73.                 $product = Mage::getModel('catalog/product');
  74.                 // build the "query" to get only the attribute we want
  75.                 $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
  76.                     ->setEntityTypeFilter($product->getResource()->getTypeId())
  77.                     ->addFieldToFilter('attribute_code', $this->getArg('attribute')) // This can be changed to any attribute code
  78.                     ->load(false);
  79.                 // get the first item in the result set.
  80.                 $attribute = $attributes->getFirstItem()->setEntity($product->getResource());
  81.                 unset($product,$attributes);
  82.                 /* @var $attribute Mage_Eav_Model_Entity_Attribute */
  83.                 $eoptions = $attribute->getSource()->getAllOptions(false);
  84.                 // build existing options array so we can not duplicate the options
  85.                 $existing_options=array();
  86.                 foreach($eoptions as $opt){
  87.                     $existing_options[trim($opt['label'])] = $opt['value'];
  88.                 }
  89.                 unset($eoptions);
  90.  
  91.                 $_new_options = file($this->getArg('file'));
  92.                 $options = array('value' => array(), 'order' => array(), 'delete' => array());
  93.                 $i = 0;
  94.                 foreach($_new_options as $option){
  95.                     $option = trim($option);
  96.                     if(!isset($existing_options[$option])){
  97.                         $i++;
  98.                         $options['value']['option_' . $i] = array($option);
  99.                     }
  100.                 }
  101.                 unset($_new_options,$existing_options);
  102.  
  103.                 if(count($options['value'])>0){
  104.                     $_attribute =  Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $this->getArg('attribute'));
  105.                     $_attribute->setOption($options);
  106.                     $_attribute->save();
  107.                     echo "Options successfully imported.n";
  108.                     echo "attribute: ".$this->getArg('attribute')."n";
  109.                     echo "count: ".count($options['value'])."n";
  110.                     unset($_attribute,$options);
  111.                 } else {
  112.                     echo "No NEW options to import.n";
  113.                 }
  114.             }catch(Exception $e){
  115.                 echo "Import Error::".$e->getMessage()."n";
  116.             }
  117.         } else {
  118.             echo $this->usageHelp();
  119.         }
  120.     }
  121.  
  122.     /**
  123.      * Retrieve Usage Help Message
  124.      *
  125.      */
  126.     public function usageHelp()
  127.     {
  128.         return <<<USAGE
  129. Usage:  php -f attributeimport.php -- [options]
  130.         php -f attributeimport.php -- --attribute manufacturer --file ../manufacturers.txt
  131.  
  132.   --attribute <attribute>       name of the attribute to update
  133.   --file <file>                 file path to import from, one value per line
  134.   help                          This help
  135. USAGE;
  136.     }
  137. }
  138.  
  139. $shell = new Mage_Shell_Attributeimport();
  140. $shell->run();

3> Note i have used manufacturers.txt in the root dir with the values of manufacturers in newline format. For example:

Dell
Toshiba
Sony
Fujitsu
... and so on

4> Simply run from browser: SSH into your box. Or setup a crontab job with the following line...

  1. cd /{my/magento/install}/shell/; php -f attributeimport.php -- --attribute manufacturer --file ../manufacturers.txt;

make sure you put in the appropriate data

  • path to magento install
  • attribute to import into
  • file to import from

5> You are done.

Note: Don’t repeat 4 unless you delete those values from backend manually.

Todo: Skip the import for existing values.

This will also check for existing options on that attribute and NOT duplicate them!!

Conclusion

You can use above snippet for any attribute with dropdown options like manufacturer

Good Luck!




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs