Woocommerce - 从商店页面中删除可用的产品库存编号

Woocommerce - Remove the available product inventory number from the shop page

在显示我的电子商务商店中所有产品的商店页面上,它当前在产品名称旁边显示产品数量(库存),如下所示:

我发现并尝试使用此代码:

add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
function woo_remove_category_products_count() {
return;
}

但它不起作用,因为它删除了单个产品页面上的 'only 5 left in stock' 通知,而那不是我需要的。

然后我尝试使用 CSS:

.count {
    display: none !important;
}

但是也不行。

我真的希望有人对此有解决方案。非常欢迎所有建议,并感谢您的提前努力!

@更新1:


试试这个代码片段功能(没有保证,因为未经测试),但逻辑上它应该完成工作(见下面的模板摘录):

remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );

或者:

add_action('init', function(){
    remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
}

先找到那个count涉及的函数:woocommerce_result_count().

然后我找到了相关的钩子:

这是显示挂钩的模板 archive-product.php 的摘录:

<?php
    /**
     * woocommerce_before_shop_loop hook.
     *
     * @hooked woocommerce_result_count - 20       <==== ==== ==== ==== Here @@@ !
     * @hooked woocommerce_catalog_ordering - 30
     */
    do_action( 'woocommerce_before_shop_loop' );
?>

@update2: — 这也有效 (参见更新 3:备选方案)


上次尝试基于 this old thread (see at the end),覆盖商店页面上的原生功能:

add_action('init', function(){
    if(is_shop()){
        function woocommerce_result_count(){
            return;
        }
    }
}

或者:

if(is_shop()){
    function woocommerce_result_count(){
        return;
    }
}

@update3: -- 另一个可行的解决方案 (覆盖模板文件)


函数 woocommerce_result_count() 参考 loop/result-count.php WooCommerce 模板,如您在此源代码摘录中所见:

if ( ! function_exists( 'woocommerce_result_count' ) ) {

    /**
     * Output the result count text (Showing x - x of x results).
     *
     * @subpackage  Loop
     */
    function woocommerce_result_count() {
        wc_get_template( 'loop/result-count.php' );
    }
}

解决方法:

在:loop/result-count.php WooCommerce 模板中,添加:|| is_shop() if语句(第27行),这样:

<?php
/**
 * Result Count
 *
 ... / ...

 * @version     2.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $wp_query;

// @@@ Here we avoid count on shop page <==== ==== ==== ==== ADDING " || is_shop() "…
if ( ! woocommerce_products_will_display() || is_shop() )
    return;
/*
 ... / ...

 */

这次行得通……

参考:Overriding Templates via a Theme (+Template Structure)

好的,所以登录 wordpress 并转到 WooCommerce > 设置 > 库存(选项卡)

然后将股票显示格式设置为从不显示股票信息。