Erwin Manalang
Erwin Manalang

Reputation: 347

Avoid stock reduction on COD orders with proscessing status in Woocommerce

I am having trouble with my COD payment method, when a customer orders... The order becomes processing and it reduces the items stock.

Based on this answer thread: Reduce stock only for specific order statuses and payment method in Woocommerce, I've customized the code and tried this script:

add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_processing_stock', 10, 2 );
function wcs_do_not_reduce_processing_stock( $reduce_stock, $order ) {
    if ( $order->has_status( 'processing' ) && $order->get_payment_method() == 'cod' ) {
        $reduce_stock = false;
    }
    return $reduce_stock;
}

But it doesn't work.

How can I avoid stock reduction on order items for orders using "Cash on delivery" payment method with a "processing" status?

Upvotes: 2

Views: 736

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253949

I have changed a your code a bit and added the missing code that will reduce stock on "completed" order status for COD payment method only:

add_filter( 'woocommerce_can_reduce_order_stock', 'processing_cod_stock_not_reduced', 20, 2 );
function processing_cod_stock_not_reduced( $reduce_stock, $order ) {
    if ( ! $order->has_status( 'completed' ) && $order->get_payment_method() == 'cod' ) {
        return false;
    }
    return $reduce_stock;
}

// Reduce stock on COD orders with completed status
add_action( 'woocommerce_order_status_completed', 'order_stock_reduction_based_on_payment_method', 20, 2 );
function order_stock_reduction_based_on_payment_method( $order_id, $order ){
    if( $order->get_payment_method() == 'cod' && ! get_post_meta( $order_id, '_order_stock_reduced', true ) ){
        wc_reduce_stock_levels($order_id);
    }
}

Code goes in function.php file of the active child theme (or active theme). Tested and works

Upvotes: 2

Related Questions