WooCommerce

how to update your e-Commerce catalog

from a csv file or a remote url

WooCommerce is a fantastic open source solution and the right choice if you're looking for a quick and easy way to open a new e-commerce in a couple of days of work. All of that with just a little or no coding experience.

Setting up your e-commerce with WordPress and WooCommerce can be really smooth, just selecting a preferred layout from tons of free WordPress themes and plugins designed for WooCommerce. The hard work will starts when you need to load thousands of products with their descriptions, prices, sizes and stock quantities. Fortunately, also in this case, WooCommerce can help with its FREE built-in tool: "Import product from CSV"

WooCommerce import tool

WooCommerce > Product > Import

This import tool allows you to map fields in a csv file (one product per row) with internal WooCommerce variables and then load or update data if product with same SKU (Stock Keeping Unit) already exists in the catalog. The "mapping" process is even stored into the database, so it can be reused for next import job.

Unfortunately, there is no way to schedule the loading job, at least for free, so all the task has to be managed and run manually every time you need to update your catalog or add additional products in a batch mode.

Of course, there are several plugins you can find and purchase to solve this problem, but this will be yet another plugin where you won't have a complete control of your process.

Here we have the solution with the following PHP code snippet that you could easily adapt to your needs.


1) Our simple PHP script has to be created in a subdirectory under your Wordpress base directory (for example /var/www/html/wordpress/digitalBooster-scripts).


2) First of all we need to to initialize the Wordpress environment calling wp-load.php . In order to do that insert next code snippet at the top of your file.

The code will automatically search for WordPress Base directory and initialize the environment.

<?php

if( php_sapi_name() !== 'cli' ) {

die("Meant to be run from command line");

}

// Initialize Wordpress environment -------------

function find_wordpress_base_path() {

$dir = dirname(__FILE__);

do {

//it is possible to check for other files here

if( file_exists($dir."/wp-config.php") ) {

return $dir;

}

} while( $dir = realpath("$dir/..") );

return null;

}

define( 'BASE_PATH', find_wordpress_base_path()."/" );

define('WP_USE_THEMES', false);

global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;

require(BASE_PATH . 'wp-load.php');

// END Initialize Wordpress environment -------------

3) Here we suppose to feed our e-commerce catalog from a remote csv file.

$data = file_get_contents("http://your-feeding-source.csv");

Our file is a csv with a semicolon as field separator and the product's SKU at first column, price at 17th column ( $price = $s[16]), etc., but of course this must be adapted to your file format.



The following WooCommerce core functions allow you to retrieve product by SKU.

$id=wc_get_product_id_by_sku($sku);

$product = wc_get_product( $id );

In our really common scenario a Stock Keeping Unit (SKU) has to be stored and used as unique key for any product in your catalog.



Then, the updatate of any product property, like: price, sale price and quantity is performed calling the related WooCommerce function:

$product->set_regular_price($price);

$product->set_sale_price(null);

$product->save();

Data is then saved and the database transaction completed calling $product->save();


/* Your Business logic here */

$sku = "1";

$quantity = 0;


/* download your csv file */

$data = file_get_contents("http://your-feeding-source.csv");

$rows = explode("\n",$data);

foreach($rows as $row) {

$s = str_getcsv($row, ";");

// skip the header at first line

if ($s[0] == 'Sku') continue;

$sku = $s[0]; // in our example SKU field is at first position

$quantity = $s[15];

$price = $s[16];


// The Next 2 WooCommerce core functions retrieve a product by SKU

$id=wc_get_product_id_by_sku($sku);

$product = wc_get_product( $id );

if ($product) {

$product->set_stock_quantity($quantity);

$product->set_regular_price($price);

$product->set_sale_price(null);

$product->save();


// Some logging

echo "ID: ". $product->id . " SKU:". $product->get_sku() ."\n";

echo "Name: ". $product->get_name() ."\n";

echo "Stock Qty.:". $product->get_stock_quantity() ."\n";

echo "Product Saved!\n";

} else

echo "SKU: ". $sku ." does not exists!\n";

}

return;

?>

That's all folks ! Hope did you enjoy and we proved you that WordPress and WooCommerce can be customized and adapted to your online business with just a few lines of PHP code.

Please don't hesitate to contact Digital Booster as for our professional services to set up or re-design in a few hours your e-Commerce starting from our e-Commerce Toolbox designed for SME and Local Business that need to open quickly and smoothly their new online shop.

Sample of implementations e-Commerce Toolbox could be found here:

Toys, Games and a lot of Fun!


The Easy eCommerce designed for local Shops and Resellers