Now is your last chance to buy a lifetime license before we switch to annual pricing. Existing licenses will be unaffected.
read more
Posted on December 14, 2023

How to Import WooCommerce Products via Cron Jobs

hello world!

The need to automate WooCommerce product imports usually results from one of the following scenarios:

  • Your suppliers send you regular updates of their product lists or catalogs. These updates can include changes to product descriptions, variations, prices, etc.
  • You use drop-shipping arrangements for some of your products, and the drop-shippers provide you with regular updates on their stock levels.
  • You have multiple stores that all access the same inventory, but they're not connected online, so you have to send regular updates to your own stores.

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:

  1. An Automatic Scheduling service, which costs $9 per month.
  2. Placing manual cron jobs on your server, which is free.

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.

Table of Contents

Understanding How Cron Jobs Work

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:

  • URL
  • FTP/SFTP
  • A file that has been uploaded to /wp-content/uploads/wpallimport/files/ folder on your server. You specify this method with the Use existing file option.

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.

Creating the Cron Jobs on Your Server

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.

Asking Your Host for Help with Cron Jobs

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.

Calling Cron URLs from the Command Line

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Oxygen Today

Lifetime, unlimited site license

Copyright © 2024 Soflyy
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram