隐藏Woocommerce中库存商品的产品价格

时间:2018-08-05 16:13:54

标签: php wordpress woocommerce product price

当物品缺货时,我有以下代码显示红点而不是价格:

add_filter( 'woocommerce_get_price_html', 'theatre_child_woocommerce_get_price_html', 10, 2 );

function theatre_child_woocommerce_get_price_html( $price, $_product ) {

    if( ! $_product->is_in_stock() ) {
        $price = '<div class="theatre_child_red_circle"></div>';
    }

    return $price;
}

我的客户现在想隐藏库存商品的价格。显然,如果我使用CSS隐藏span.price,它也会隐藏红色圆圈。

我如何修改上面的代码以在缺货的商品上显示一个红色圆圈,并隐藏有货的商品的价格?

1 个答案:

答案 0 :(得分:0)

以下内容将使您的红色圆圈代表“缺货”商品,并隐藏“有货”商品的价格:

add_filter( 'woocommerce_get_price_html', 'theatre_child_woocommerce_get_price_html', 10, 2 );
function theatre_child_woocommerce_get_price_html( $price, $product ) {
    if( ! $product->is_in_stock() ) {
        $price = '<div class="theatre_child_red_circle"></div>';
    } else {
       $price = '';
    }
    return $price;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

相关问题