0

I picked up this code snippet and have been messing with it. I'm getting some syntax errors…

Here is that code:

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();

Im getting some errors on line 14 and 22... couldn't figure this out. Any ideas?

How to get this Auto restock feature on specific orders status changes in woocommerce 3?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

2 Answers2

3

Sorry, but the code you are using is just completely outdated and full of errors.

Below, based on this working similar code, this class will re-stock Products based on your defined orders status changes:

if ( ! class_exists( 'WC_Auto_Stock_Restore' ) ) {
    class WC_Auto_Stock_Restore {

        function __construct() {
            add_action( 'woocommerce_order_status_processing_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_completed_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_processing_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_completed_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
        }

        public function restore_order_stock( $order_id, $order ) {
            $items = $order->get_items();

            if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! count( $items ) > 0 )
                    return; // We exit

            foreach ( $order->get_items() as $item ) {
                $product_id = $item->get_product_id();

                if ( $product_id > 0 ) {
                    $product = $item->get_product();

                    if ( $product && $product->exists() && $product->managing_stock() ) {

                        // Get the product initial stock quantity (before update)
                        $initial_stock = $product->get_stock_quantity();

                        $item_qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $this, $item );

                        // Update the product stock quantity
                        // Replace DEPRECATED methods: increase_stock() & discrease_stock()
                        wc_update_product_stock( $product, $item_qty, 'increase' );

                        // Get the product updated stock quantity
                        $updated_stock = $initial_stock + $item_qty;

                        do_action( 'woocommerce_auto_stock_restored', $product, $item );

                        // A unique Order note: Store each order note in an array…
                        $order_note[] = sprintf( __( 'Product ID #%s stock incremented from %s to %s.', 'woocommerce' ), $product_id, $initial_stock, $updated_stock);

                        // DEPRECATED & NO LONGER NEEDED - can be removed
                        //$order->send_stock_notifications( $product, $updated_stock, $item_qty );

                    }
                }
            }
            // Adding a unique composite order note (for multiple items)
            $order_notes = count($order_note) > 1 ? implode(' | ', $order_note) : $order_note[0];
            $order->add_order_note( $order_notes );
        }
    }
    $GLOBALS['wc_auto_stock_restore'] = new WC_Auto_Stock_Restore();
}

Code goes in function.php file of your active child theme (or theme).

Tested and works (for WooCommerce 3+ only).


LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • a new problem has turned up. When the order is canceled by the customer or by time-out stock isn't restored – Daniel Romagnoli Feb 27 '18 at 15:45
  • @DanielRomagnoli My answer here is just making work your initial code for Woocommerce 3+, avoiding the errors reported in your initial question… The code just use the same hooks than the ones in your question code. Now this new discovered issue, should be reported **in a new question** with all necessary details and code that you are using for it… – LoicTheAztec Feb 27 '18 at 15:55
  • Hii what about restocking at variation level it will work ? – Parth Shah Sep 14 '18 at 09:52
  • @ParthShah Sure it should, restocking is made at product variations level for variable products. `$item->get_product();` is always the variation product object and never the parent variable product. – LoicTheAztec Sep 14 '18 at 10:13
-3

Try it like this, this should work (I removed the &amp &amp on both lines this is probably causing your error)

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()
  • 2
    Just curious, how the OP will know why your answer will work. All you said was "try it like this", followed by what initially looks to be the same as the OP's original code. Should they run a diff to find what you changed? Did you simply harvest the answers that were posted by others as comments, and post as an answer? If so: Note that the people who originally commented wrote concise answers (albeit in comment form). What you posted leaves it to the reader to figure out why your answer is the answer. – David Makogon Feb 21 '18 at 16:12
  • I'm just posting the solution for him. – Matthijs Otterloo Feb 21 '18 at 16:13
  • Hey, so I added the snippet but I'm still getting the same error. Screenshot: http://prntscr.com/ihw0h8 and on my live site: http://prntscr.com/ihw0yp – Daniel Romagnoli Feb 21 '18 at 16:28
  • Yeah, so the problem was the && on both lines... now got it figured out! – Daniel Romagnoli Feb 21 '18 at 16:31
  • Great! @DanielRomagnoli, upvote/mark as solution so others see it as well. – Matthijs Otterloo Feb 22 '18 at 09:42