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

问题描述

在我的项目中,餐馆属于woocommerce类,他们提供的食物组合就是产品。 在Web设备上,客户的第一个输入是交货地址,然后在第二页中,仅应显示能够在那里交货的餐馆。

在wp_mysql数据库中,我创建了一个专用表,每个餐厅都有自己的地址和交货范围。 借助google maps API和自定义查询,我可以轻松地获取可以在客户指定地址提供的餐厅列表(更确切地说,我可以获取category_ID列表)。

问题是:如何显示来自此WooCommerce产品类别术语Id的产品(因此是餐馆)?

在WooCommerce中完全可行吗?

解决方法

以下代码将基于一系列产品类别术语Ids 更改产品查询(因此,您必须在此函数中包括获得那些产品类别术语Ids的代码)

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文件中。经过测试,可以正常工作。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...