将缺货的产品变体变灰(WooCommerce)

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

[编辑:在 functions.php 中添加代码并省略代码 WC 文件中的更改时,它实际上有效。重要提示:它仅在 ONE 属性存在时有效。但是,当有 2 个属性(例如尺寸和颜色)时,它就不起作用了,因为它不再是关于缺货的变体,而是关于变体组合,事实上 WooCommerce 在这种常见情况下是完全无能为力的。请注意,目前似乎也没有可用的插件来解决这个非常明显的问题。太棒了。]

自 2.0 以来,WooCommerce 要么隐藏缺货产品变体(这是一个明显的问题,因为客户无法知道它们的存在),要么将它们显示为有货变体(这也是一个问题,因为客户随后 点击购买后,系统地失望地发现变体缺货)。

This thread 包括将缺货产品变体变灰的解决方案:

估计要加到functions.php:

add_filter( 'woocommerce_variation_is_active', 'grey_out_variations_when_out_of_stock', 10, 3 );

function grey_out_variations_when_out_of_stock( $grey_out, $variation_id, $id ) {

    $variation = get_product( $variation_id );

    if ( ! $variation->is_in_stock() )
        return false;

    return true;
}

plugins/woocommerce/includes/class-wc-product-variation.php完成:

变化:

return apply_filters( 'woocommerce_variation_is_active', true, $this->variation_id, $this->id );

至:

return apply_filters( 'woocommerce_variation_is_active', true, $this );

同时更改:

return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );

至:

return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id, $this );

然而,尽管据报道它有效,但在我的例子中,缺货版本的显示与其他版本相同,我也有一个警告:

Warning: Missing argument 3 for grey_out_variations_when_out_of_stock() in ...\functions.php on line 600

我做错了什么?

不要更改核心中的任何内容。

该错误告诉您您正在尝试调用 3 个变量作为函数的参数,但操作挂钩仅传递 2 个,因此缺少第三个。这是因为您修改了 core 以删除第三个参数。

另请注意

   // 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;
    }

可以在变量产品class中找到。如果 woocommerce_hide_out_of_stock_items 选项在应该自动处理的 WooCommerce 设置中设置。

更新

我通读了您提到的 github 问题。 franticpsyx 所做的提交已被略微修改,因此 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 现在只有 2 个变量传递给它的函数。

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

因此我们需要修改代码以使用正在传递的变量:

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;
}

这对我有用。我很确定在前端项目通过 Javascript 变灰,因为在后端这样做需要大量的 mods 才能核心化。

只是添加我的答案,因为我寻找了很长时间,其中 none 对我有用。不管什么 $variation->is_in_stock() 总是 return true.

我的解决方案是通过将 $variation->is_in_stock() 更改为 $variation->get_stock_quantity() 来修改函数。

$variation->get_stock_quantity() 实际上 return 是一个您可以使用的数字,即剩余库存。因此,根据您喜欢的参数重写该函数,例如,当还剩 5 个时,您希望将其标记为缺货(如果您希望 0 个缺货,则为 0),该函数将如下所示:

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;
    }
}