问题描述:

I'm working on a plugin that handles the rental of vehicles via WooCommerce. The default WooCommerce behaviour is to decrement the stock of items in an order as soon as payment is made. To cut a long story short, I need to prevent this from occurring (I will be implementing a custom function to only decrease stock on the chosen rental date).

In the WC_Order class I found a function called payment_complete() (class-wc-order.php, line 1278).

In this function is the following:

if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', true, $this->id ) )

$this->reduce_order_stock(); // Payment is complete so reduce stock levels

It would appear to me that I simply need to include

add_filter( 'woocommerce_payment_complete_reduce_order_stock', '_return_false' );

in my plugin in order to prevent stock from decreasing on payment, but unfortunately this does not work. I've also tried wrapping my add_filter() in a function which triggers on init, but still no luck.

Any help greatly appreciated.

网友答案:

This is old, but if you don't want to WooCommerce to manage stock you can tell the entire store not to manage stock in the options. OR, individually on each product.

Studying the reduce_order_stock() method in the WC_Abstract_Order class reveals that stock will not be reduced in those cases.

/**
 * Reduce stock levels
 */
public function reduce_order_stock() {

    if ( 'yes' == get_option('woocommerce_manage_stock') && sizeof( $this->get_items() ) > 0 ) {

        // Reduce stock levels and do any other actions with products in the cart
        foreach ( $this->get_items() as $item ) {

            if ( $item['product_id'] > 0 ) {
                $_product = $this->get_product_from_item( $item );

                if ( $_product && $_product->exists() && $_product->managing_stock() ) {
                    $qty       = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
                    $new_stock = $_product->reduce_stock( $qty );

                    $this->add_order_note( sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $item['product_id'], $new_stock + $qty, $new_stock) );
                    $this->send_stock_notifications( $_product, $new_stock, $item['qty'] );
                }

            }

        }

        do_action( 'woocommerce_reduce_order_stock', $this );

        $this->add_order_note( __( 'Order item stock reduced successfully.', 'woocommerce' ) );
    }
}

However, the OP's observation is correct, but includes a typo. The __return_false() function is preceded by 2 underscores instead of 1, so the correct code would be:

add_filter( 'woocommerce_payment_complete_reduce_order_stock', '__return_false' );

From there I would add your custom stock-decreasing function to the woocommerce_payment_complete hook.

网友答案:
function filter_woocommerce_can_reduce_order_stock( $true, $instance ) { 
return false; 
}; 
add_filter( 'woocommerce_can_reduce_order_stock','filter_woocommerce_can_reduce_order_stock', 10, 2 ); 

This helped me sort out the issue!

网友答案:

Instead of add_filter, You should use remove_filter function.

相关阅读:
Top