在 WooCommerce 中始终显示没有数量的库存状态

Tom*_*999 2 wordpress product stock woocommerce product-quantity

我想显示产品库存状态,而不显示剩余数量。

现在,标准的 WooCommerce 库存显示,以及我目前找到的插件(WooCommerce Booster 等),要么显示数量,要么显示数量和短语“有货”的“一揽子交易”。

换句话说,

我有:“5 个库存”

我想显示:“有货”

这可能吗?如果是这样,如何?

小智 6

如果您对“有货”自定义消息有疑问,请尝试以下代码:

add_filter( 'woocommerce_get_availability', 'custom_override_get_availability', 10, 2);

// The hook in function $availability is passed via the filter!
    function custom_override_get_availability( $availability, $_product ) {
    if ( $_product->is_in_stock() ) $availability['availability'] = __('In Stock', 'woocommerce');
    return $availability;
    }
Run Code Online (Sandbox Code Playgroud)

或这个

//* Add stock status to archive pages
function envy_stock_catalog() {
    global $product;
    if ( $product->is_in_stock() ) {
        echo '<div class="stock" >' . $product->get_stock_quantity() . __( ' in stock', 'envy' ) . '</div>';
    } else {
        echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );
Run Code Online (Sandbox Code Playgroud)


Tom*_*999 5

解决。

Woocommerce -> 设置 -> 产品 -> 库存 -> 库存显示格式。

……不敢相信我以前没看过。