问题
On the shop page that displays all the products in my e-commerce store, it's currently displaying the product count (inventory) number beside the name of the product as so:
I found and tried to use this code:
add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
function woo_remove_category_products_count() {
return;
}
But it doesn't work because it removes the 'only 5 left in stock' notice on the individual product page and thats not what I need.
Then I have tried to use CSS:
.count {
display: none !important;
}
But doesn't work either.
I really hope someone has a solution for this. All suggestions very welcome and thanks for your efforts in advance!
回答1:
@Update1:
Try this snippet code function (without guaranty, because untested), but logically it should do the job (see the extract of the template below):
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
Or alternatively:
add_action('init', function(){
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
}
First find the function involved in that count: woocommerce_result_count()
.
Then I have Find the related hook:
Here is the extract of the template archive-product.php
that shows the hook:
<?php
/**
* woocommerce_before_shop_loop hook.
*
* @hooked woocommerce_result_count - 20 <==== ==== ==== ==== Here @@@ !
* @hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
?>
@update2: — This is working too (see update3: the alternative)
Last try based on this old thread (see at the end), overriding the native function on shop page:
add_action('init', function(){
if(is_shop()){
function woocommerce_result_count(){
return;
}
}
}
Or alternatively:
if(is_shop()){
function woocommerce_result_count(){
return;
}
}
@update3: -- The other working solution (overriding template file)
The function woocommerce_result_count() refer to loop/result-count.php
WooCommerce template, as you can see in this source extract:
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' );
}
}
The solution:
is in: loop/result-count.php
WooCommerce template, adding: || is_shop()
to the if
statement (on line 27), this way:
<?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;
/*
... / ...
*/
That works this time…
Reference: Overriding Templates via a Theme (+Template Structure)
回答2:
Ok so log into wordpress and go to WooCommerce > Settings > Inventory (tab)
Then set Stock display format to never display stock information.
来源:https://stackoverflow.com/questions/38363250/woocommerce-remove-the-available-product-inventory-number-from-the-shop-page