简体   繁体   English

WooCommerce 如何显示特定产品类别的产品

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

In my project, restaurants are woocommerce categories, the food combos they deliver are products.在我的项目中,餐厅属于 woocommerce 类别,他们提供的食物组合是产品。 At the web appliance, the first input of the customer is the delivery address, then in the second page only the restaurants able to deliver there should be displayed.在 Web 设备上,客户的第一个输入是送货地址,然后在第二页中应该只显示能够送货到那里的餐厅。

In the wp_mysql db I created a dedicated table where each restaurant has its own address and delivery range.在 wp_mysql db 中,我创建了一个专用表,其中每个餐厅都有自己的地址和送货范围。 With the help of google maps API and a custom query I easily manage to get the list of restaurants can deliver at the customer specified address (more precisely I get the list of category_IDs).在谷歌地图 API 和自定义查询的帮助下,我轻松地获得了可以在客户指定地址提供的餐厅列表(更准确地说,我获得了 category_ID 列表)。

The question is: How can I display products from this selected WooCommerce product category term Ids (so the restaurants)?问题是:如何从这个选定的 WooCommerce 产品类别术语 Id(餐厅)显示产品?

Is it feasible at all within WooCommerce?在 WooCommerce 中完全可行吗?

The following code will alter the product query, based on an array of product category term Ids (so you will have to include inside this function, the code to get those product category term Ids) :以下代码将根据产品类别术语 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;
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示Woocommerce中特定产品类别的产品 - Display products from specific product category in Woocommerce Woocommerce:产品页面其他分类的产品(交叉产品) - Woocommerce: products from other categories on the product page (cross products) 显示 WooCommerce 产品循环中特定类别的产品属性值 - Display product attributes values for specific categories in WooCommerce product loops 如何将特定类别添加到 woocommerce 产品变体 - How to add specific categories to woocommerce product variations 避免在WooCommerce中添加来自不同产品类别的购物车产品 - Avoid add to cart products from different product categories in WooCommerce 将 WooCommerce 订单中按产品类别订购的产品数量相加 - Sum the quantities of products ordered by product categories from a WooCommerce order Woocommerce如何按产品SKU展示产品 - Woocommerce how to display products by product SKU Woocommerce 按特定类别获取产品 - Woocommerce get products by specific categories Magento - 在产品页面中显示自定义文本,仅适用于特定类别的产品 - Magento - Display a custom text in product page, only for products from specific categories WooCommerce单品有子品类时按品类展示内容 - Display content on WooCommerce single products based on product categories when they have child categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM