Greying out out-of-stock product variations (WooCommerce)

前端 未结 2 574
太阳男子
太阳男子 2020-12-10 23:16

[EDIT: it actually works when adding the code in functions.php and omitting the changes in code WC files. Important: it only works when ONE attribute exists. However when th

相关标签:
2条回答
  • 2020-12-10 23:57

    Just adding my answer as I hunted a long time for it and none of these were working for me. no matter what $variation->is_in_stock() would ALWAYS return true.

    My solution was to modify the function by changing $variation->is_in_stock() to $variation->get_stock_quantity().

    $variation->get_stock_quantity() actually returns a number that you can use which is the remaining stock. so rewrite the function based on the parameters you like, say when there are 5 left you want it marked out of stock (or 0 if you want 0 to be out of stock) the function would look like so:

    add_filter( 'woocommerce_variation_is_active', 'my_jazzy_function', 10, 2 );
    
    function my_jazzy_function( $active, $variation ) {
    
        $var_stock_count = $variation->get_stock_quantity();
    
        // if there are 5 or less, disable the variant, could always just set to 0.
        if( $var_stock_count <= 6 ) {
           return false;
        }
        else {
           return true;
        }
    }
    
    0 讨论(0)
  • 2020-12-11 00:03

    Don't change anything in core.

    That error tells you that you are trying to call 3 variables as parameters for your function, but the action hook is only passing 2, hence the third one is missing. This is because you have modified core to remove the 3rd argument.

    Also note that

       // Hide out of stock variations if 'Hide out of stock items from the catalog' is checked
        if ( empty( $variation->variation_id ) || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
            continue;
        }
    

    can be found in the variable product class. If the woocommerce_hide_out_of_stock_items option is set in the WooCommerce settings that should handle this automatically.

    UPDATE

    I read through the github issue that you reference. The commits made by franticpsyx have since been modified slightly, so the function that franticpsyx posted isn't working as he originally posted it there and why you are running into the variable number error. In the WooCommerce source the woocommerce_variation_is_active now only has 2 variables passed to it.

    public function variation_is_active() {
        return apply_filters( 'woocommerce_variation_is_active', true, $this );
    }
    

    So we need to modify the code to use the variables that are being passed:

    add_filter( 'woocommerce_variation_is_active', 'grey_out_variations_when_out_of_stock', 10, 2 );
    
    function grey_out_variations_when_out_of_stock( $grey_out, $variation ) {
    
        if ( ! $variation->is_in_stock() )
            return false;
    
        return true;
    }
    

    This worked for me. I'm pretty sure that on the front-end the items are greyed out via Javascript, because to do so on the back-end would require a lot of mods to core.

    0 讨论(0)
提交回复
热议问题