在 Woocommerce 中按产品类别、产品标签和价格查询
Query by product category, product tags and price in Woocommerce
我正在尝试制作一个简单的表单来根据表单输入字段查询 woocommerce 产品 wine,如下所示:
Selected 类别(葡萄品种,例如红酒、白酒等)- 输入类型下拉列表
Select 酒厂标签 1 下拉菜单
Select 葡萄酒分类标签 2 下拉菜单
Select 葡萄酒产区标签 3 下拉菜单
价格范围下拉菜单
按类别和价格过滤有效,但标签给出的结果好坏参半,我不明白为什么。
这是我的表单提供一些上下文的样子:
这是我的代码:
$custom_query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'order' => 'DESC',
'posts_per_page' => 3,
'product_tag' => array($tag1, $tag2, tag3),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
'operator' => 'OR'
)),
//Price
'meta_query' => array(
array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
)
);
在输入字段中,我有 3 个产品标签变量(如 $tag1
、$tag2
、$tag3
),1 个产品类别变量(如 $category
) 和 2 个价格范围变量(如 $clow
和 $chigh
),它们是从到的价格。
有人知道为什么会这样吗?
你的代码有一些小错误:
tag3
、 中缺少 $
OR
不是 operator
值(不需要),
- 每个 产品标签 都需要在一个 分隔的
tax_query
数组中 才能使您的过滤器正常工作。
因此请尝试使用以下修改后的代码:
$custom_query_args = array(
'posts_per_page' => 3,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'order' => 'DESC',
'tax_query' => array(
// Product category filter
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
),
// Product tag 1 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag1),
'field' => 'slug',
),
// Product tag 2 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag2),
'field' => 'slug',
),
// Product tag 3 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag3),
'field' => 'slug',
),
),
// Price filter
'meta_query' => array( array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
) ),
);
经过测试并有效。
我找到了更简单的方法,而且效果更好,因为当没有过滤器时,它将显示所有产品并且查询也更小,如下所示:
$category = 'category-slug-here'; //String, but can accept array?
$tags = 'comma,delimeted,tags,here'; //I build string with tags needed no array.
$clow = 0; //min price int
$chigh = 200; //max price int
$custom_query_args = array(
'posts_per_page' => '12',
'product_cat' => $category,
'post_type' => 'product',
'product_tag' => $tags,
// Price filter
'meta_query' => array( array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
) ),
);
这样做有什么不好的方面吗,因为我已经使用不同的过滤器标签对其进行了测试并且它像我计划的那样工作?
我正在尝试制作一个简单的表单来根据表单输入字段查询 woocommerce 产品 wine,如下所示:
Selected 类别(葡萄品种,例如红酒、白酒等)- 输入类型下拉列表
Select 酒厂标签 1 下拉菜单
Select 葡萄酒分类标签 2 下拉菜单
Select 葡萄酒产区标签 3 下拉菜单
价格范围下拉菜单
按类别和价格过滤有效,但标签给出的结果好坏参半,我不明白为什么。
这是我的表单提供一些上下文的样子:
这是我的代码:
$custom_query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'order' => 'DESC',
'posts_per_page' => 3,
'product_tag' => array($tag1, $tag2, tag3),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
'operator' => 'OR'
)),
//Price
'meta_query' => array(
array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
)
);
在输入字段中,我有 3 个产品标签变量(如 $tag1
、$tag2
、$tag3
),1 个产品类别变量(如 $category
) 和 2 个价格范围变量(如 $clow
和 $chigh
),它们是从到的价格。
有人知道为什么会这样吗?
你的代码有一些小错误:
tag3
、 中缺少 OR
不是operator
值(不需要),- 每个 产品标签 都需要在一个 分隔的
tax_query
数组中 才能使您的过滤器正常工作。
$
因此请尝试使用以下修改后的代码:
$custom_query_args = array(
'posts_per_page' => 3,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'order' => 'DESC',
'tax_query' => array(
// Product category filter
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
),
// Product tag 1 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag1),
'field' => 'slug',
),
// Product tag 2 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag2),
'field' => 'slug',
),
// Product tag 3 filter
array(
'taxonomy' => 'product_tag',
'terms' => array($tag3),
'field' => 'slug',
),
),
// Price filter
'meta_query' => array( array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
) ),
);
经过测试并有效。
我找到了更简单的方法,而且效果更好,因为当没有过滤器时,它将显示所有产品并且查询也更小,如下所示:
$category = 'category-slug-here'; //String, but can accept array?
$tags = 'comma,delimeted,tags,here'; //I build string with tags needed no array.
$clow = 0; //min price int
$chigh = 200; //max price int
$custom_query_args = array(
'posts_per_page' => '12',
'product_cat' => $category,
'post_type' => 'product',
'product_tag' => $tags,
// Price filter
'meta_query' => array( array(
'key' => '_price',
'value' => array($clow, $chigh),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
) ),
);
这样做有什么不好的方面吗,因为我已经使用不同的过滤器标签对其进行了测试并且它像我计划的那样工作?