【问题标题】:How can I hide out of stock products on WooCommerce category pages?如何在 WooCommerce 类别页面上隐藏缺货产品?
【发布时间】:2021-11-28 13:09:58
【问题描述】:

我的客户销售单一数量的产品。她的网站有一个专门的页面,用于在产品售出(缺货)后展示这些产品。我的挑战是从所有其他页面隐藏缺货产品。到目前为止,我已经添加了许多过滤器。我现在想要完成的是在类别页面上隐藏缺货产品。有过滤器吗?

以下是当前使用的过滤器:

/**
 * @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;
  }
}

【问题讨论】:

    标签: php wordpress woocommerce filter


    【解决方案1】:

    要在产品存档页面上隐藏缺货产品,您需要修改 woocommerce_product_query_meta_query 挂钩内的条件。

    is_shop() 条件仅适用于 shop page,在 WooCommerce > Settings > Products > General > Shop page 中定义。在您的情况下,您还需要添加 is_product_category() 条件,以检查它是否是类别页面。

    例子:

    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;
    }
    

    【讨论】:

    • 太棒了!!!这非常有效。
    猜你喜欢
    • 2021-08-31
    • 1970-01-01
    • 2019-09-27
    • 2014-08-20
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    相关资源
    最近更新 更多