WooCommerce snippet: Restock automatically with WordPress themes

WiseGuys is a big fan of WooCommerce, as we have made a lot of wordpress themes and plugins basing on WordPress and WooCommerce. Recently, we found out that when you select “stock management” for your product, when the order is completed and then customers cancel the order, reduced stock will not restore automatically.

WooCommerce snippet: Restock automatically with WordPress themes
WooCommece auto restock on refunded or cancelled orders snippet

Luckily, WiseGuys has found a way to do that, there’s a lot of plugins out there allowing you to do that, and we do recommend you to use plugins, why? Because our method below will work with your theme, and if you switch to another theme, it will be lost. With plugin, functions will stay there forever till you deactive the plugin.

But, we’re the coders, WiseGuys likes to make things with our own style, WiseGuys style!. Let’s rock it with this snippet below.

The snippet above is re-written by WiseGuys, we’re selling WordPress Themes on Envato market. The original of this snippet is made by Kloon, but it’s the plugin, which you can’t embed in your wordpress themes as ours.

How to use WordPress, WooCommerce snippet?

Basically, for this snippet, and other snippets, you just need to follow this instruction. Either you have snippet on WiseGuys or on other blogs, just do the same thing with your wordpress themes

  1. Go to WP-Admin
  2. Go to Appearance
  3. Choose Theme editor
  4. Select Theme Functions (functions.php) on the file list corresponsing to your current active wordpress theme
  5. Copy/paste snippet to the end of function file.

Problems you might be faced with

1/ I got PHP error

There’s usually a reason that causes the error. The first is snippet starting with function {…}, and go paste it at the end of file, where have enclosing tag ?>. It will cause error, as you have pasted into wrong place, all that kind of snippet should be placed before closing tag.

2/ Conflict and showing errors

Conflicts mean that the author of the themes you’re using has written something, and new snippet just using same technique or function name. It will throw PHP error.

To solve the problem, you might need to ask snippet author or theme author to help you in case you do not know anything about PHP coding.

3/ Blank page, white page

The problem usually appears after you save the file, your WordPress site returns nothing but white death page. It’s more complicated that the other 2 above, as this time, you’re not able to access wp-admin, theme editor or even worst, entire site turns blank.

So, to solve that, you need to use hosting’s editor. If you’re using cPanel, login and go to folder public_html/wp-content/themes/current-wordpress-theme-folder/. Then edit file functions.php, just remove the code you added and save it, problem will be gone. Besides, you can use FTP if you can’t access to hosting, the most popular software like FileZilla or CuteFTP will help you.

In case you still want to use that snippet, you need to enable wp-config.php file located at public_html find the line saying WP_Debug = FALSE, change value FALSE to TRUE, save the file, refresh your site. It might appear some errors, as which file, functions is causing white page and you will have a little more information to fix.

That’s all, WiseGuys thinks it’s enough for today to show you how to use WordPress and WooCommerce snippet with your wordpress theme. You’re understood and have more experience when using snippet to your themes as well as how to debug. Hope that will help you guys in the future.

 

13 Comments

  1. KSO September 17, 2017 at 1:36 am

    Is this working for stock management on variable product level?

    Reply
    1. Andrew Frost September 17, 2017 at 2:42 am

      Hi,

      Yes, it will work as it work base on product in the cart. The variable product is the child product and have unique ID so it should work properly!

      Thanks

      Reply
  2. KSO September 17, 2017 at 2:45 am

    I just tested it, it’s not working if stock management is on variation level and unchecked on product level. Breaks the whole page. And yes, i added it at the right place 😉

    Reply
    1. Andrew Frost September 17, 2017 at 2:51 am

      Can you please replace the old code with new code? I just edited it a little bit!

      Wait for your confirmation!

      Reply
  3. KSO September 17, 2017 at 3:06 am

    Nope, still Error 500 🙁 I’m using WC 3.1.2 and WP 4.8.1

    Reply
    1. Andrew Frost September 17, 2017 at 3:07 am

      Hmm, weird.

      Can you please open the log and paste it here?

      Reply
      1. KSO September 17, 2017 at 3:15 am

        Hmm, even with WP_Debug = TRUE, there are no logs. Just a blank white page.

        Reply
        1. Andrew Frost September 17, 2017 at 3:27 am

          It should have something in PHP Error log, not just wp_debug.txt file

          Reply
          1. KSO September 17, 2017 at 4:15 am

            Unfortunately, it’s not logging an error at all.

  4. miharucosmetics September 17, 2017 at 4:54 pm

    not working just white page

    Reply
  5. KSO September 20, 2017 at 3:51 am

    I just tested this Plugin:

    https://wordpress.org/plugins/woocommerce-auto-restore-stock/

    It has not been updated since 4 years but it’s still working! Tested with WC 3.1.2 and WP 4.8.1 – You have to cancel orders manually, but the restock is working as expected. Also with stock management on variation level.

    Automatic restock is not working, like Pending to Cancel because of Payment Timeout, but that is just fine like that! However, this Plugin is a huge timesaver for my workflow.

    Reply
  6. Daniel Romagnoli February 21, 2018 at 10:35 pm

    Got this error: fatal error on line 14: syntax error, unexpected ‘;’

    Reply

Leave a Reply