E-Commerce Stock-Updater

This was an enjoyable piece of functionality to develop. A footwear retailer recently migrated their online e-commerce shop from NopCommerce to WordPress/WooCommerce. I was asked to re-create an import feature that they had been using to update stock levels. On a regular basis, the retailer staff would generate a spreadsheet of products with the latest stock levels and needed to be able to click a button to have that file processed and all the products updated.

I have worked on many projects that required a source file to be submitted regularly by the website owner for processing, as well as many one-off custom imports. So-much-so that in my spare time I’ve been working on an import-management plugin that can be easily extended for individual cases. That work is not yet complete, but this project was another opportunity to work on some of the features it might need.

Most interesting was a problem with server timeouts – where the server cannot do all the work required within the limit set by the hosting company. That limit is there to ensure that websites are not going to tie up server resources as well as to ensure that if something is never going to complete because of a bug in the code, the end user will eventually see an error that indicates something has gone wrong, rather than waiting around forever!

In the case of this project, the server limit was not allowing the typical number of stock items to be updated in one pass. A number of solutions were discussed, including having the staff break up the file into smaller ones, or just having the plugin report the point at which it stopped processing, so that the staff could remove those rows from the spreadsheet before re-submitting. Those would have been less costly options, but we decided to automate the whole process by using an ajax script.

The ajax script is informed of the row-number at which the importer stopped (the import script monitors itself to determine how long it’s taking per row and stops if it knows that it will not be able to finish in time). If there are more rows to process, the ajax script re-submits the request to import but only supplying that row number. The import script, having saved the import data temporarily, knows where to pick up and simply starts again from that point with a newly-reset time limit. This process repeats until the ajax script is informed there are no more rows remaining. It was very satisfying to finish this up and see it working!

Dev Notes

One notable frustration was working with WooCommerce. In this case, the function used to update stock quantities does not do all the work required to have the desired effect, and the solution is not officially documented. Various post_meta values must be updated , such as ‘_stock’, ‘_stock_status’ and ‘product_visibility’ (I wonder why the function in question does not do this).

In addition, you can run into trouble if you initialise the product as an object because it will set various defaults that may cause unexpected results, such as items appearing for selection on the frontend even though they’re out of stock! That can happen because the default product setting has ‘backorders’ turned on.

WooCommerce is a fantastic piece of software, especially considering its cost (free!), but it can certainly be hard to work with at times!

Client

Big Top Multimedia