禁用缺货属性条款

问题描述 投票:0回答:1

我正在一个艺术网站上工作,该网站销售具有特定印刷编号属性和各种框架选项的印刷艺术品。每个印刷编号只有一件库存,一旦售出,该编号就会变得不可用。

我已经成功实现了产品在售出后被标记为不可用的功能。然而,问题是该选项仍然出现在商店页面上,只有在访问者选择该变体后才会禁用“添加到购物车”按钮。这可能会让用户感到沮丧。

如何使已售完的印刷号码无法点击、模糊或从商店页面完全消失? 显示我的商店页面产品变化的图片 在我附上的图片上,打印号 1 已售出,但在商店页面上仍然可见。

我尝试使用一些代码片段,但没有一个起作用,如果您有插件推荐或自定义代码,我会很高兴。

php wordpress function woocommerce inventory
1个回答
0
投票

您可以通过向主题的functions.php 文件添加自定义代码片段来隐藏 WooCommerce 商店页面中已售完的变体。操作方法如下:

add_filter('woocommerce_variation_is_active', 'hide_sold_out_variations', 10, 2);

function hide_sold_out_variations($is_active, $variation) {
// Check if the variation is in stock
if (!$variation->is_in_stock()) {
    return false; // Make the variation inactive (hidden)
}
return $is_active; // Keep the default behavior for in-stock variations

}

add_action('woocommerce_before_shop_loop_item', 'hide_sold_out_variation_buttons', 10);

function hide_sold_out_variation_buttons() {
global $product;
// Check if the product is variable and has variations
if ($product->is_type('variable')) {
    foreach ($product->get_available_variations() as $variation) {
        // Check if the variation is out of stock
        if (!$variation['is_in_stock']) {
            echo '<style>.variations .variation-' . esc_attr($variation['variation_id']) . ' { display: none; }</style>';
        }
      }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.