0

I have been using WP All Import (to varying degrees of success) to import products to my Woo Commerce site. My server keeps timing out and stopping the import which is causing me difficulty and I have followed the official advise from the plugin without much luck. My imports are broken into ~12 CSV files, the largest with ~100 rows.

Does anyone have any other ways of adding ~400 products to the site with the correct variations? One solution I considered was running my site locally, running all my imports and then replacing my live db with the local one but I would rather avoid that...

0

I assume that you want to create your own import plugin here.

The reason that your server timing-out is possible due exporting all products in one request.

So, what I will do for that scenario is by using ajax import. Instead of trying to save all data in one go, you can use ajax to store the product one-by-one.

  • Actually I didn't have this in mind - I currently break my import into appropriate groupings so there are 12 or so to do. One of the largest is a CSV file with ~rows – J4G Mar 2 '19 at 15:52
0

Upload a .csv file onto your server, then iterate over the entries to create the proper arrays for the Woocommerce API.

Something like this.

$path = $_SERVER['DOCUMENT_ROOT'];
require_once( $path.'/wp-load.php' );
$filepath = site_url()."/data/mybatchfile.csv";
$handle = fopen($filepath, "r") or die("Error opening file");

$i = 0;

while(($line = fgetcsv($handle)) !== FALSE) {
    if($i == 0) {
        $c = 0;
        foreach($line as $col) {
            $cols[$c] = $col;
            $c++;
        }
    } else if($i > 0) {
        $c = 0;
        foreach($line as $col) {
            $prod_data[$i][$cols[$c]] = $col;
            $c++;
        }
    }
    $i++;
}
fclose($handle);

foreach ($prod_data as $prod_row){
     $name = $prod_row["product_name"] // these are the column header names
     //get all the rest of your vars here
     $variation1SKU = $prod_row["first_variation_SKU"];
     $variation1name = $prod_row["first_variation_name"];
     $var1slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $variation1name)));
     $variation1price = $prod_row["first_variation_price"];
     $variation1color = $prod_row["first_variation_color"];
     $variation1optname = $prod_row["first_variation_option_name"];

     $variation2SKU = $prod_row["second_variation_SKU"];
     $variation2name = $prod_row["second_variation_name"];
     $var2slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $variation2name)));
     $variation2price = $prod_row["second_variation_price"];
     $variation2color = $prod_row["second_variation_color"];
     $variation1optname = $prod_row["first_variation_option_name"];

$api = new WC_REST_Products_Controller();
$request = new WP_REST_Request ('POST', '', '');
$request->set_body_params( array (
    'name' => $name,
    'slug' => $slug,
    'type' => 'simple',
    'status' => 'publish',
    'regular_price' => $price,
    'price' => $price,
    //'sale_price' => 40,
    'description' => 'This is the description for the '.$name.' product',
    'virtual' => 1,
    'tax_status' => 'none', // or 'taxable'
    'variations' => array(
            array(
                'sku' => $variation1SKU,
                'regular_price' => $variation1price,
                'attributes' => array( 
                    array( 
                        'id' => date('YmdHis', strtotime('+2 seconds')),
                        'slug'=> $var1slug,
                        'name'=> $variation1name,
                        'option'=> $variation1optname, 
                    )
                ) 
            ),
            array(
                'sku' => $variation2SKU,
                'regular_price' => $variation2price,
                'attributes' => array( 
                    array( 
                        'id' => date('YmdHis', strtotime('+2 seconds')),
                        'slug'=> $var2slug,
                        'name'=> $variation2name,
                        'option'=> $variation2optname,
                    )
                ) 
            )
        )
));

$response = $api->create_item( $request );

$id = $response->data['id']; // the post id of the new item
}

This is just untested example code to show you an efficient way to deal with creating products using the APIs of Woocommerce and Wordpress. You'll need to create a CSV file with friendly column headers and then save it as UTF-8 before upload. And if you have some products that are simple and some that are variable then you'll need to introduce a value check, say on the variable of the first variation's name field. If it's blank you set the request body up one way, and if it's populated the request body gets built with the variations. That kind of thing. Hope it helps!

Another thing, the woocommerce API lets you use

$response = $api->update_item( $request );

and then you tell it which item you're updating by putting in the request body

'id' => $id,

Then the other vars in the request body such as a price or a description will get updated. I've found this to be an invaluable tool to modify products on the fly, create an order, add those products to the new order, then return the price back to its original value. That's how you can create custom orders with custom prices and not show a discount line item on the order.

The example code for variations I got from the following thread: https://stackoverflow.com/questions/37823867/woocommerce-api-create-new-product-with-variations

  • thanks - would this work with attributes and variables? it's a little more advanced than what i've done before – J4G Mar 2 '19 at 18:13
  • Yes it will work for variable products. You just set up the request with different variables, namely the 'type' => 'simple' becomes 'type' => 'variable' and then you need to add another key to the request as well. Let me look at an example and amend the answer. – Elkrat Mar 2 '19 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.