gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:24:56 25 4
gpt4 key购买 nike

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

enter image description here

我发现并尝试使用这段代码:

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

但它不起作用,因为它删除了单个产品页面上“仅剩 5 件库存”的通知,而这不是我需要的。

然后我尝试使用 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 );
}

首先找到那个计数涉及的函数:woocommerce_result_count()

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

这是显示 Hook 的模板 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: — 这也有效(参见 update3:备选方案)


上次尝试基于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)

关于php - Woocommerce - 从商店页面中删除可用的产品库存编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38363250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com