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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class Comfythemes_Woocommerce_Auto_Stock_Restore { function __construct() { add_action( 'woocommerce_order_status_processing_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 ); add_action( 'woocommerce_order_status_completed_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 ); add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 ); add_action( 'woocommerce_order_status_processing_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 ); add_action( 'woocommerce_order_status_completed_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 ); add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 ); } public function restore_order_stock( $order_id ) { $order = new WC_Order( $order_id ); if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! sizeof( $order->get_items() ) > 0 ) { return; } foreach ( $order->get_items() as $item ) { if ( $item['product_id'] > 0 ) { $_product = $order->get_product_from_item( $item ); if ( $_product && $_product->exists() && $_product->managing_stock() ) { $old_stock = $_product->stock; $qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item ); $new_quantity = $_product->increase_stock( $qty ); do_action( 'woocommerce_auto_stock_restored', $_product, $item ); $order->add_order_note( sprintf( __( 'Item #%s stock incremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) ); $order->send_stock_notifications( $_product, $new_quantity, $item['qty'] ); } } } } } new Comfythemes_Woocommerce_Auto_Stock_Restore(); |
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
- Go to WP-Admin
- Go to Appearance
- Choose Theme editor
- Select Theme Functions (functions.php) on the file list corresponsing to your current active wordpress theme
- 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.