The need to automate WooCommerce product imports usually results from one of the following scenarios:
To automate these processes, you need to build a product import routine and then run that routine on a schedule.
This article assumes that you have already built the import routine using WP All Import. If you need help with this, see How to Update the Product Price in WooCommerce as a quick example.
The remaining question is how to run this routine on a schedule. When using WP All Import, you have two options:
For more information on the Automatic Scheduling service, see Run WordPress Import on a Schedule.
In this article, we're going to examine the use of manual cron jobs on your server.
Cron is a scheduler that runs on Unix-like operating systems. It allows you to automate tasks.
A cron job is a job that can be run by that scheduler tool.
To set up a recurring WooCommerce product import using cron jobs, you must first create the import with WP All Import using one of the following import sources:
The reason for this requirement is that import files must be accessible to the routine that is trying to import them without the need for manual intervention.
Before scheduling an import routine, you should first test it manually. When you do this, WP All Import will record all the details of that import, which you can see by clicking All Import › Manage Imports on the WordPress main menu. This will display the following page:
To set up scheduling, click the Scheduling Options link for the import you are trying to schedule. This will bring up the following screen:
To see information on the URLs you will schedule with your cron jobs, click Manual Scheduling.
You don't actually have to do anything on this screen. For example, you don't have to click the Save button. The information shown in each of the fields displayed above is meant to help you set up cron jobs on your server, which we discuss in the next section. However, some explanation of them is required.
Each import has two cron URLs – a trigger URL and a processing URL.
The trigger URL will look something like this:
http://YOUR-WEBSITE.com/wp-load.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=trigger
The processing URL will look something like this:
http://YOUR-WEBSITE.com/wp-load.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=processing
The website, secret key, and import ID are already populated in the preceding image. You can get the import ID from the All Import › Manage Imports screen (first image). The secret key is stored in the Secret Key field on the All Import › Settings screen.
The process of setting up cron jobs can differ from one host to another and can often be done in the web hosting control panel. Commands such as wget, curl, and lynx are typically used:
wget --spider "http://YOUR-WEBSITE.com/wp-load.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=trigger&rand="$RANDOM
wget -q -O - "http://YOUR-WEBSITE.com/wp-load.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=trigger&rand="$RANDOM
curl "http://YOUR-WEBSITE.com/wp-load.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=trigger&rand="$RANDOM
lynx "http://YOUR-WEBSITE.com/wp-load.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=trigger&rand="$RANDOM
As seen in these examples, you should use a $RANDOM parameter as a cache-busting technique. This is especially handy in servers where heavy caching doesn't permit the cron jobs to function correctly.
Note, however, that not all options work for all hosts, so you should contact your host for guidance.
To run your import every 24 hours, run the trigger URL every 24 hours. To run the import once per week, run the trigger URL every week.
The processing URL should be run every two minutes regardless because it may not complete your product import in one run. This is because many hosts impose maximum execution times, meaning your processing script may time out after completing only a small portion of the import.
When the processing URL is run again after two minutes, it will check whether the import is finished. If not, it will resume processing until it times out again, and so on, until the import is complete.
At that point, the processing script will “untrigger” the import, which means that the processing script will have no effect until the trigger script triggers another import.
To get help from your host to set up the product import cron jobs, you can use this email template:
Hi Support,
Please set up two cron jobs.
CRON JOB 1
Fetch this URL every 24 hours: http://YOUR-WEBSITE.com/wp-load.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=trigger
CRON JOB 2
Fetch this URL every 2 minutes: http://YOUR-WEBSITE.com/wp-load.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=processing
Thanks,
Your Name
If your host doesn’t have a cron feature, try an external service like EasyCron.
If your host doesn't allow inbound calls to the regular cron URLs, you may have to call the URLs from the command line. These calls look like this:
/path/to/your/php-cgi /path/to/yourwordpressinstall/wp-load.php import_key=ABC123 import_id=5 action=trigger
/path/to/your/php-cgi /path/to/yourwordpressinstall/wp-load.php import_key=ABC123 import_id=5 action=processing
Note that you have to use the php-cgi binary when doing this, or the parameters won't be passed correctly.