Importing Manufactures & Option Attribute Sets In Magento

Import Manufacturers and option attribute sets in Magento

If you are migrating to Magento having thousands of products, affiliates, manufacturers and lots of custom attributes, things may not be easy for you. Magento itself provides an easy set of features to import lots of useful data but the pain area still lies when you try to import custom attributes, option attributes etc. For an example let’s say you have hundreds of manufacturers details which needs to be added to your Magento store you have to manually add those which is quite time consuming and human errors are possible.

Following is a way to add option attributes to your Magento store using command line interface (CLI). Here is how you should proceed

1. Create a customimport file under your Magento root i.e. /root/shell/Magento/customimport.php
2. Add the following code in your customimport.php file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
    require_once 'abstract.php';
    class Mage_Shell_Attributeimport extends Mage_Shell_Abstract
    {
        protected $_attribute_with_option_value;
        protected $_attribute;
        protected function _getAttribute()
        {
            if (is_null($this->_attribute_with_option_value)) {
                $this->_log = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $this->_attribute_with_option_value);;
            }
            return $this->_attribute_with_option_value;
        }
 
        public function run()
        {
            if ($this->getArg('attribute') && $this->getArg('file')) {
                try{
                    // need the product typeid to filter the results
                    $product = Mage::getModel('catalog/product');
                    // build the "query" to get only the attribute we want
                    $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
                        ->setEntityTypeFilter($product->getResource()->getTypeId())
                        ->addFieldToFilter('attribute_code', $this->getArg('attribute')) // This can be changed to any attribute code
                        ->load(false);
                    // get the first item in the result set.
                    $attribute = $attributes->getFirstItem()->setEntity($product->getResource());
                    unset($product,$attributes);
                    /* @var $attribute Mage_Eav_Model_Entity_Attribute */
                    $eoptions = $attribute->getSource()->getAllOptions(false);
                    // build existing options array so we can not duplicate the options
                    $existing_options=array();
                    foreach($eoptions as $opt){
                        $existing_options[trim($opt['label'])] = $opt['value'];
                    }
                    unset($eoptions);
 
                    $_new_options = file($this->getArg('file'));
                    $options = array('value' => array(), 'order' => array(), 'delete' => array());
                    $i = 0;
                    foreach($_new_options as $option){
                        $option = trim($option);
                        if(!isset($existing_options[$option])){
                            $i++;
                            $options['value']['option_' . $i] = array($option);
                        }
                    }
                    unset($_new_options,$existing_options);
 
                    if(count($options['value'])>0){
                        $_attribute =  Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $this->getArg('attribute'));
                        $_attribute->setOption($options);
                        $_attribute->save();
                        echo "Options successfully imported.n";
                        echo "attribute: ".$this->getArg('attribute')."n";
                        echo "count: ".count($options['value'])."n";
                        unset($_attribute,$options);
                    } else {
                        echo "No NEW options to import.n";
                    }
                }catch(Exception $e){
                    echo "Import Error::".$e->getMessage()."n";
                }
            } else {
                echo $this->usageHelp();
            }
        }
 
        public function usageHelp()
        {
            return <<<USAGE
    Usage:  php -f customimport.php -- [options]
            php -f customimport.php -- --attribute manufacturer --file ../manufacturers.txt
 
      --attribute <attribute>       name of the attribute to update
      --file <file>                 file path to import from, one value per line
      help                          This help
    USAGE;
        }
    }
 
    $shell = new Mage_Shell_Attributeimport();
    $shell->run();

Having the code file in proper place we can simply run this from browser: SSH into your box. Or setup a CRON job with the following line

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

Before you run this file simply ensure that you have the appropriate data and path etc. are correct i.e.

  1. Path to magento install is correct
  2. Attribute to import into is already there
  3. Import file exists and contains manufacturer of option attribute sets well defined

I hope this will help in saving many hours and you don’t have to manually add manufacturer or other option attribute sets anymore. I would love to hear your thoughts on importing manufacturer or option attribute sets in your Magento store. Please leave me a comment and let me know. Subscribe our RSS to receive latest Magento updates, tips and tricks.

  • Robots

    PHP Parse error:  syntax error, unexpected T_STRING, expecting T_FUNCTION … line 14
     

  • Magnus_A

    syntax error, unexpected $end in ****/shell/customimport.php on line 84

  • Computerhersteller

    Had this error too. Removing the public function usageHelp solved this for me.