Support » Plugin: WooCommerce Auto Restore Stock » Decrementing Stock
Decrementing Stock
-
Have you thought about adding some extra code to adjust the stock levels if an order is changed from cancelled back to complete?
Viewing 3 replies - 1 through 3 (of 3 total)
-
I’ve made a fairly straight forward modification to your code if you want me to send it over. It will decrement stock when orders move from cancelled to a variety of active statuses.
I’d like to see what you’ve done! Would you mind sharing?
Here is the entire code, copy and paste if you like 🙂
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! class_exists( 'WC_Auto_Stock_Restore' ) ) { class WC_Auto_Stock_Restore { public 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 ); add_action( 'woocommerce_order_status_cancelled_to_completed', array( $this, 'remove_order_stock' ), 10, 1 ); add_action( 'woocommerce_order_status_cancelled_to_processing', array( $this, 'remove_order_stock' ), 10, 1 ); add_action( 'woocommerce_order_status_cancelled_to_on-hold', array( $this, 'remove_order_stock' ), 10, 1 ); //add_action( 'woocommerce_order_status_changed', array( $this, 'adjust_order_stock' ), 10, 3 ); } // End __construct() //public function adjust_order_stock( $order_id, $old_status, $new_status ) { //$valid_new_statuses = array( 'complete' ); //if ( $old_status == cancelled AND $new_status ) //} public function remove_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->reduce_stock( $qty ); do_action( 'woocommerce_auto_stock_restored', $_product, $item ); $order->add_order_note( sprintf( __( 'Item #%s stock decremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) ); $order->send_stock_notifications( $_product, $new_quantity, $item['qty'] ); } } } } 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'] ); } } } } // End restore_order_stock() } $GLOBALS['wc_auto_stock_restore'] = new WC_Auto_Stock_Restore(); }
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Decrementing Stock’ is closed to new replies.