[英]How can I hide out of stock products on WooCommerce category pages?
My client sells single quantity products.我的客户销售单一数量的产品。 Her site has a dedicated page for displaying those products once they are sold (out of stock).她的网站有一个专门的页面,用于在产品售出(缺货)后展示这些产品。 My challenge is to hide out of stock products from all other pages.我的挑战是从所有其他页面隐藏缺货产品。 So far I have added a number of filters.到目前为止,我已经添加了许多过滤器。 What I am trying to accomplish now is hide the out of stock products on the category pages.我现在想要完成的是在类别页面上隐藏缺货产品。 Is there a filter for that?有过滤器吗?
Here are the filters currently in use:以下是当前使用的过滤器:
/**
* @snippet Display Out of Stock Products via Shortcode - WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 5
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'outofstock',
)
),
'fields' => 'ids',
);
$product_ids = get_posts( $args );
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// Only on shop archive pages
if( is_admin() || is_search() || ! is_shop() ) return $meta_query;
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
);
return $meta_query;
}
/**
* Hide loop read more buttons for out of stock items
*/
if (!function_exists('woocommerce_template_loop_add_to_cart')) {
function woocommerce_template_loop_add_to_cart() {
global $product;
if ( ! $product->is_in_stock() || ! $product->is_purchasable() ) return;
wc_get_template('loop/add-to-cart.php');
}
}
/* Hide out of stock products in related section */
add_filter( 'woocommerce_related_products', 'mysite_filter_related_products', 10, 1 );
function mysite_filter_related_products( $related_product_ids )
{
if (!is_admin()) {
foreach ($related_product_ids as $key => $value) {
$relatedProduct = wc_get_product($value);
if (!$relatedProduct->is_in_stock() ) {
unset($related_product_ids["$key"]);
}
}
return $related_product_ids;
}
}
To hide out of stock products on a product archive page you need to modify the condition inside your woocommerce_product_query_meta_query hook.要在产品存档页面上隐藏缺货产品,您需要修改woocommerce_product_query_meta_query挂钩内的条件。
is_shop() condition only works for shop page , which is defined in WooCommerce > Settings > Products > General > Shop page. is_shop()条件仅适用于商店页面,该页面在 WooCommerce > 设置 > 产品 > 常规 > 商店页面中定义。 In your case you also need to add is_product_category() condition, to check if it's a category page.在您的情况下,您还需要添加is_product_category()条件,以检查它是否是类别页面。
Example:例子:
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// Only on shop archive pages
if( is_admin() || is_search() || ( !is_shop() && !is_product_category() ) ) return $meta_query;
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
);
return $meta_query;
}
Add this code into the theme functions.php file and replace the wooCommerce category slug with your category slug.将此代码添加到主题 functions.php 文件中,并将 wooCommerce 类别 slug 替换为您的类别 slug。
add_filter( 'woocommerce_product_query_meta_query', 'wine_hide_instock_products', 10, 2 );
function wine_hide_instock_products( $meta_query, $query ) {
if( is_product_category('warehouse-deals') ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
);
return $meta_query;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.