Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce csv import custom fields - adjust built-in importer to include custom fields?

I'm in this situation that I have to import values for custom fields for more than 2000 products and I simply cannot find any free plugin or built-in function which enables this. Current I've found a range of premium plugins that has this feature, but the cheapest one costs $50, and that's simply out of budget at this time.

Therefore I would like to ask if anyone in here knows of a way to add custom fields to the built-in csv importer & exporter which later versions of Woocommerce than 3.1 comes with? I've successfully created my own custom fields for product data so far, but they do not automatically show up in the built-in importer (sadly).

Is there any viable way of doing this, or for that matter, do you know any other way such as a free plugin that I haven't found or anything else?

Please note that I have tried the "wp ultimate csv importer" as per this post, but unfortunately the custom fields doesn't show up in this free plugin.

like image 671
Chri.s Avatar asked Oct 31 '17 17:10

Chri.s


2 Answers

Well, it seems that I was a bit too quick posting this question, as I've just figured out an answer - but hopefully this can save some people a lot of time and money (would have saved me a lot of time, if I only found this answer elsewhere).

I Accidentally bumped into the "Column Header Reference" (although apparently I should have been there from beginning) of the Woocommerce document section, and here I actually found that this is completely do-able, just by adjusting the Header Reference in the csv file with a prefix of meta: followed by the id of your custom field.

Requirement for this solution
The only requirement for this solution is that you have Woocommerce version >3.1 installed and activated, and to use the built-in csv importer of the plugin. This can be found in the admin panel under "tools --> import --> Woocommerce products (csv)"

Example:
if you have a custom field for each product that, for example, contains the brand of each product and has an ID of brand, you can import values for each of these custom fields by naming the Header Reference in the csv file the following: meta:brand.

As an other example, if the id of the custom field was _product_brand, values for this custom field can be imported by naming the Header Reference (the 1st row in a csv file in excel, for example) the following: meta:_product_brand

The solution is to prefix the custom field id with meta: inside the csv file in the heading.

Note: in case that it doesn't happen automatically during the second step of the import, make sure to select the option "import as meta" in the column "map to field" for the column you wish to import to your custom field.

Create new custom field
If you just upload csv with meta heading reference with a field id that doesnt exist, this will create a new custom field with the name given in the csv file.

like image 66
Chri.s Avatar answered Nov 08 '22 03:11

Chri.s


I had same need and finally developed my own plugin for this purpose.

First you need to use from PHPExcel in your plugin. After that read your xls file and import product with woocommerce functions.

Read and convert your xls file to to PHP array with following structure:

function fileInitializer($file, $needCells){
     $array_data = array();
     require_once 'libraries/PHPExcel/PHPExcel.php';
     $objPHPExcel = new PHPExcel();
     $target_dir = untrailingslashit( dirname( __FILE__ ) )."/upload-file/".$file;
     $objReader= new PHPExcel_Reader_Excel5();
     $objReader->setReadDataOnly(true);
     $objPHPExcel = $objReader->load( $target_dir);
     $rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
     $rowIndex = 0;
     foreach($rowIterator as $row){
         $cellIterator = $row->getCellIterator();
         $cellIterator->setIterateOnlyExistingCells(false); 
         if(1 == $row->getRowIndex ()) continue;

        foreach ($cellIterator as $cell) {
            foreach($needCells as $needCell){
                if($needCell['cell_name'] == $cell->getColumn()){
                    $array_data[$rowIndex][$needCell['array_name']] = fai_convert_string_to_persian($cell->getCalculatedValue());
                } 
            }
        }
        $rowIndex++;
    }
    return $array_data;
}
$file = 'FILEPATH';
$needCells = array(
     array('cell_name'=>'A', 'array_name'=>'id')
    , array('cell_name'=>'B', 'array_name'=>'full_name')
);
$array_data = fileInitializer($file, $needCells);

and after above process have a while in $array_data and add product like following:

$post = array(
        'post_author' => $user_id,
        'post_content' => '',
        'post_status' => "publish",
        'post_title' => $value['product_name'],
        'post_parent' => '',
        'post_type' => "product",
    );

    $post_id = wp_insert_post( $post, $wp_error );

    //ADDING EXTERA FEATURES
    update_post_meta( $post_id, 'main_code_text_field', $value['main_code'] 
 );

Also these are woocommerce metadatas:

    update_post_meta( $post_id, 'total_sales', '0');
    update_post_meta( $post_id, '_downloadable', 'yes');
    update_post_meta( $post_id, '_virtual', 'yes');
    update_post_meta( $post_id, '_regular_price', "" );
    update_post_meta( $post_id, '_sale_price', "");
    update_post_meta( $post_id, '_purchase_note', "" );
    update_post_meta( $post_id, '_featured', "no" );
    update_post_meta( $post_id, '_weight', "" );
    update_post_meta( $post_id, '_length', "" );
    update_post_meta( $post_id, '_width', "" );
    update_post_meta( $post_id, '_height', "" );
    update_post_meta( $post_id, '_sku', "");
    update_post_meta( $post_id, '_product_attributes', array());
    update_post_meta( $post_id, '_sale_price_dates_from', "" );
    update_post_meta( $post_id, '_sale_price_dates_to', "" );
    update_post_meta( $post_id, '_sold_individually', "" );
    update_post_meta( $post_id, '_manage_stock', "no" );
    update_post_meta( $post_id, '_backorders', "no" );
    update_post_meta( $post_id, '_stock', "" );
like image 23
Majid Abbasi Avatar answered Nov 08 '22 03:11

Majid Abbasi