使缺货的产品变体变灰 (WooCommerce) [英] Greying out out-of-stock product variations (WooCommerce)

查看:65
本文介绍了使缺货的产品变体变灰 (WooCommerce)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since 2.0 WooCommerce either hides out-of-stock product variations (an obvious problem since clients have then no way to know about their existence) or displays them just as in-stock variations (also a problem because clients are then systematically disappointed to find out the variation is out-of-stock after clicking purchase).

此主题 包含一个将缺货产品变体变灰的解决方案:

This thread includes a solution to grey out the out-of-stock product variations:

大概要添加到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 );

虽然据报道它有效,但在我的情况下,缺货变化与其他变化显示相同,我也有警告:

Yet although it reportedly works, in my case the out-of-stock variations are displayed the same as the others and I also have a warning:

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

我做错了什么?

推荐答案

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

Don't change anything in core.

该错误告诉您,您正在尝试调用 3 个变量作为函数的参数,但动作挂钩仅传递了 2 个,因此缺少第三个.这是因为您已修改 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.

还要注意

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

可以在变量产品类中找到.如果在 WooCommerce 设置中设置了 woocommerce_hide_out_of_stock_items 选项,则应自动处理此问题.

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.

更新

我通读了您引用的 github 问题.由 franticpsyx 所做的提交已经略有修改,因此 franticpsyx 发布的功能 无法正常工作,因为他最初将其发布在那里,以及为什么您会遇到可变编号错误.在 WooCommerce 源代码中,woocommerce_variation_is_active 现在只有 2 个变量传递给它.

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

这对我有用.我很确定在前端,项目通过 Javascript 变灰,因为在后端这样做需要很多 mod 来核心.

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.

这篇关于使缺货的产品变体变灰 (WooCommerce)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆