Having custom fields as columns in CSVI importer

CSVI is a great data importing tool

You can load many products at a time into your VM database through it. There is a slight problem with the custom fields import but

You need to specify all custom fields in a single column called: custom_title

It is a bit tedious for the person who is creating the import sheet. Best solution would have been that all custom fields also come into the column name drop downs.

So, here is a great trick that does this (One of my favorite tricks I ever wrote )

  1. First, add the required custom fields in the available fields dropdown. You can do this by simply firing the following sql query on your database
    INSERT INTO #__csvi_available_fields (csvi_name,component_name,component_table,component)
    select custom_title,custom_title,'productimport','com_virtuemart' from #__virtuemart_customs where published = 1
  2. After the above step, you can now see all your custom fields in the available fields dropdown when u are configuring your import sheet structure. Configure the import sheet as per your need
  3. Now, comes the code changes part, open the file: /administrator/components/com_csvi/models/com_virtuemart/import/productimport.php
  4. Firstly add a class variable called: public $additional_customs = null; //added by convergence team
  5. In function getStart(), add the following code after the code: $this->loadData()
    /*added by convergecne team, get all the valid custom fields in an array*/
    			$vm_custom_fields = JFactory::getDBO()->setQuery("select virtuemart_custom_id, LOWER(custom_title) as  'custom_title'	from #__virtuemart_customs where published = 1")->loadObjectList('custom_title');

    Now in the switch case, add the following in its default clause:

    default:
    	if(array_key_exists($name,$vm_custom_fields)){
    		$this->additional_customs[$vm_custom_fields[$name]->custom_title] = $value;
    		$this->customtitles[$vm_custom_fields[$name]->custom_title] = $vm_custom_fields[$name]->virtuemart_custom_id;
    	}
    	$this->$name = $value;
    	break;

    Now in function getProcessRecord(), find the following code

    if (isset($this->custom_title) && !empty($this->custom_title)) $this->_processCustomFields();

    replace it by

    if ((isset($this->custom_title) && !empty($this->custom_title))||count($this->additional_customs)>0) $this->_processCustomFields();
  6. Finally go to the function _processCustomFields() and add the following after $titles = explode("~",$this->custom_title);
    //add the newly added customfieds as well
    if(count($this->additional_customs)>0)
    foreach ($this->additional_customs as $key=>$value){
    	$titles[] = $key;
    	$values[] = $value;
    }

Enjoy