WooCommerce Stock Update & Caching

5 replies
Hi,

I've been writing some code to update my stock quantities based on a CSV file that is automatically retrieved from my drop ship supplier. Objective is to sync stock every X minutes.

The code sits in a PHP in the root of my website. WooCommerce is installed as you would expect.

I have managed to update the database fine using update statements within my PHP file and have confirmed that the stock is being updated in the database. However, it is not being reflected when viewed on the website. Going to WooCommerce -> System Status and clicking Clear Transients sorts it all out and shows the database modified prices on the website.

Can anyone suggest a way for me to be able to call this functionality ($woocommerce->clear_product_transients() from a standard PHP file outside the scope of the WooCommerce installation?

I don't really want to move the file as it should run from a cronjob automatically say every 10 minutes.

Thanks,
Michael
#caching #stock #update #woocommerce
  • Profile picture of the author Mkj
    Are you clearing a cache directory when you do this:

    WooCommerce -> System Status and clicking Clear Transients

    or are you updating the database instead?

    If the above is a cache directory you could use the code on this page (untested) or something similar:

    PHP clear cache directory | Codes Tips

    and run the file every update.

    If the database needs updating then that shouldn't be too difficult either. Just need to write a file to access the database and empty or update the field.
    {{ DiscussionBoard.errors[7561205].message }}
  • Profile picture of the author Michael Westgate
    Thanks. Just had a look and WooCommerce doesn't use cache directories.

    The Clear Transients function is listed here in the apidocs on line 1716.

    I come from a .NET background so am unsure just by looking at this code which option would be better:
    1. Try to include this PHP file in my PHP file (and fake any authentication too?!?) and then call the function (basically imitating the 'Clear Transients' button click from my PHP file outside of the system)
    2. Rewrite what the code is doing so that it works the same but from my PHP file outside of WooCommerce
    {{ DiscussionBoard.errors[7561230].message }}
    • Profile picture of the author Mkj
      Try to include this PHP file in my PHP file (and fake any authentication too?!?) and then call the function (basically imitating the 'Clear Transients' button click from my PHP file outside of the system)
      This is what I did with a similar problem. In fact I removed the required authentication which thinking about it was probably a mistake. But I did protect the directory the file was in from any external access by using htaccess (only my ip was allowed in). I also stopped crawlers from indexing the directory it was in.

      Rewrite what the code is doing so that it works the same but from my PHP file outside of WooCommerce
      More or less what my solution was isn't it?
      {{ DiscussionBoard.errors[7561477].message }}
  • Profile picture of the author Michael Westgate
    Can of worms. I've spent another day on this but finally have something that I can run with.

    Thinking about it I didn't really want to play with removing or bypassing authentication nor did I want write alternative WooCommerce cache clearing code (in case WooCommerce changes in the future as it expands). So that left me back at square one.

    What I have now is a Wordpress plugin that first checks to see if WooCommerce is loaded and if it is, it calls the necessary WooCommerce function. Very simple stuff but for the very reason this plugin seems to be being executed on every single page request (so is a bit overkill), it works.

    It would be nice if I could get the plugin to only work on my page and none others!
    {{ DiscussionBoard.errors[7565209].message }}
    • Profile picture of the author cmauro
      Hi Michael,

      I just want to aks you something....

      I have a Visual Basic App installed on a Desktop computer conected to Internet. This App manage all products of my store and uses a MS-Access Database.
      I just want to know if I can update any changes of quantity of a product (MS-Access) and then update this into Woocommerce product stock table.

      Can I do that? Do you know how to do it?

      Thanks in advance.

      Christian
      {{ DiscussionBoard.errors[8426408].message }}

Trending Topics