[英]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.