0
votes

I have a really weird behavior on Woocommerce with the stock management. On a product with stock management enabled, a stock of 100 items and stock status "available", every time I do an order the product stock go to negative and get out of stock.

For example, if I do an order of 2 items of the product, the stock go to -2 right after the order, even if the stock was at 100 right before.

The product is a simple one, without any attribute. I use the following hooks to alter some label and stuffs, but none seems related to this issue:

add_filter('woocommerce_product_single_add_to_cart_text', array(&$this->wc, 'add_to_cart_text'), 11);
add_filter('woocommerce_add_to_cart', array(&$this->wc, 'add_to_cart'), 10, 1);
add_action('woocommerce_cart_item_removed', array(&$this->wc, 'cart_item_removed'), 10, 1);
add_action('woocommerce_order_status_completed', array(&$this->wc, 'order_status_completed'), 10, 1);
add_action('woocommerce_after_shop_loop_item', array(&$this->wc, 'replace_add_to_cart'));

In short, woocommerce_product_single_add_to_cart_text change the add to cart button label, woocommerce_add_to_cart place some vars in session, woocommerce_cart_item_removed remove those session var on item removal from cart, woocommerce_order_status_completed do some stuffs with the session vars (update a CPT from those session vars - I don't touch the order or the product at all) and woocommerce_after_shop_loop_item display a button on product listing. I tried to disable the woocommerce_order_status_completed hook, it didn't change anything.

I will paste any code of those function if any of you think some could be related to this stock issue.

I'm using latest version of Woocommerce and Wordpress.

1
I don't have an immediate answer, but I would test with all your custom code removed, with all plugins disabled, and with a default theme. Then slowly re-enable all the pieces until you can isolate the culprit. If the problem persists in a default state we can assume a bug in WooCommerce and should report it at their github.helgatheviking
@helgatheviking You were right, some plugin provided by the theme (Shindig) was doing this. I posted an answer explaining this.vard

1 Answers

1
votes

I found out the culprit, as helgatheviking suggested I disabled all the plugins one by one and found out that the plugin Progression One Click Import provided by the theme and marked as "recommended for theme use" was doing this.

My guess is that it is related to this filter in the plugin code:

add_filter( 'add_post_metadata', array( $this, 'check_previous_meta' ), 10, 5 );

Which is doing this:

public function check_previous_meta( $continue, $post_id, $meta_key, $meta_value, $unique ) {
    $old_value = get_metadata( 'post', $post_id, $meta_key );
    if ( count( $old_value ) == 1 ) {
        if ( $old_value[0] === $meta_value ) {
            return false;
        } elseif ( $old_value[0] !== $meta_value ) {
            update_post_meta( $post_id, $meta_key, $meta_value );
            return false;
        }
    }
}

The flaw in this is that it is inserting the stock meta value raw (-2) instead of decrementing the existing meta value, which Woocommerce seems to do with some filter on his end - a behavior that is overwrited by this filter.

I guess this could be fixed by changing the filter priority but just disabling the plugin was good for me as I don't need to import preview data.