簡體   English   中英

WooCommerce 如何顯示特定產品類別的產品

[英]WooCommerce how to display products from specific product categories

在我的項目中,餐廳屬於 woocommerce 類別,他們提供的食物組合是產品。 在 Web 設備上,客戶的第一個輸入是送貨地址,然后在第二頁中應該只顯示能夠送貨到那里的餐廳。

在 wp_mysql db 中,我創建了一個專用表,其中每個餐廳都有自己的地址和送貨范圍。 在谷歌地圖 API 和自定義查詢的幫助下,我輕松地獲得了可以在客戶指定地址提供的餐廳列表(更准確地說,我獲得了 category_ID 列表)。

問題是:如何從這個選定的 WooCommerce 產品類別術語 Id(餐廳)顯示產品?

在 WooCommerce 中完全可行嗎?

以下代碼將根據產品類別術語 Id 數組更改產品查詢(因此您必須在此函數中包含獲取這些產品類別術語 Id 的代碼)

add_filter( 'woocommerce_product_query_tax_query', 'only_specific_product_category_terms', 10, 2 );
function only_specific_product_category_terms( $tax_query, $query ) {
    if( ! is_admin() ) {
        $term_ids = array( 11, 13, 14, 15 ); // <== Here you will set your product category term ids
    
        $tax_query[] = array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id', // Or 'name' or 'slug'
            'terms'    => $term_ids
        );
    }

    return $tax_query;
}

代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM