• Resolved lukasjan123

    (@lukasjan123)


    Hello,

    I changed how woocommerce reduces stock with following code snippet to fix a problem where customers abandoned orders, they turned to status “On-Hold” and stock was reduced. This code snippet worked perfectly until I updated to 3.5.0 – 3.5.2.

    Code snippet:

    // Woocommerce do not reduce stock on hold
    // 
    add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
    function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
        if ( $order->has_status( 'on-hold' ) && $order->get_payment_method() == 'paysera' ) {
            $reduce_stock = false;
        }
        return $reduce_stock;
    }
    
    add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
    function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
        // Only for 'processing' and 'completed' order statuses change
        if ( $new_status == 'processing' || $new_status == 'completed' ){
        $stock_reduced = get_post_meta( $order_id, '_order_stock_reduced', true );
            if( empty($stock_reduced) && $order->get_payment_method() == 'paysera' ){
                wc_reduce_stock_levels($order_id);
            }
        }
    }

    After updating to 3.5.0 and newer woocommerce. Now some orders, which go in following scheme do not reduce stock at all:
    Pending payment -> On-Hold -> Processing

    Did anything change in 3.5.0 – 3.5.2 updates, which made my code snippet incorrect or this is some problem with my payment gateway(Paysera)?

    Any help is appreciated,

    Thanks,

    Lukas

Viewing 4 replies - 1 through 4 (of 4 total)
  • hi Lukas, how did you solve this problem?

    Thank you.

    Best Regards Phil

    Thread Starter lukasjan123

    (@lukasjan123)

    This was probably a problem with my payment gateway and order statuses changing from on-hold to processing.

    Solved this by making all orders go to failed status and change to processing when payment is received. Also changed the code above to

    
    // Woocommerce do not reduce stock on hold
    // 
    add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
    function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
        if ( $order->has_status( 'failed' ) && $order->get_payment_method() == 'paysera' ) {
            $reduce_stock = false;
        }
        return $reduce_stock;
    }
    
    add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
    function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
        // Only for 'processing' and 'completed' order statuses change
        if ( $new_status == 'processing' || $new_status == 'completed' ){
        $stock_reduced = get_post_meta( $order_id, '_order_stock_reduced', true );
            if( empty($stock_reduced) && $order->get_payment_method() == 'paysera' ){
                wc_reduce_stock_levels($order_id);
            }
        }
    }
    

    Thank You! That helped me!

    Thread Starter lukasjan123

    (@lukasjan123)

    Glad to hear that.

    However, I want to notify you that with this solution we have recently had a couple of issues. Some clients were redirected from payment gateway to our wordpress store unsuccessful payment page, though payment was successfully completed and we have received the money. I think it is like this because of payment status going to “failed” because we did not have this issue when all payment statuses first went to “on-hold” status.

    I have made changes for all orders to go back to “on-hold” status and will try to figure out another way to fix this issue.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Woocommerce stock reduction problem’ is closed to new replies.