在类别页面中显示最低的简单产品价格
Show lowest simple product price in category page
我想在 content-oroduct_cat.php
页面显示最低的 简单产品 价格。 Fancy Squares 中的以下代码用于显示最低价格,但我只想显示 简单产品 ,即省略分组产品。
/* SHOW LOWEST PRICE ON CATEGORY PAGE */
//woocommerce get lowest price in category
function wpq_get_min_price_per_product_cat($term_id)
{
global $wpdb;
$sql = "
SELECT MIN( meta_value+0 ) as minprice
FROM {$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
WHERE
( {$wpdb->term_relationships}.term_taxonomy_id IN (%d) )
AND {$wpdb->posts}.post_type = 'product'
AND {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->postmeta}.meta_key = '_price'
";
return $wpdb->get_var($wpdb->prepare($sql, $term_id));
}
我尝试使用:
AND {$wpdb->posts}.product_type = 'simple'
但这没有用。我如何只显示简单的产品?
You query is not working because product_type
is not stored in
posts
table it is stored in term_taxonomy
table. To get the
desired you have to use Sub query, which will fetch all the simple
product and the main query filter it according to category.
我已将您的 wpq_get_min_price_per_product_cat()
修改如下
function wh_get_min_price_per_product_cat($term_id)
{
global $wpdb;
$sql = "
SELECT MIN( meta_value+0 ) as minprice
FROM {$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
WHERE
( {$wpdb->term_relationships}.term_taxonomy_id IN (%d) )
AND {$wpdb->posts}.post_type = 'product'
AND {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->postmeta}.meta_key = '_price'
AND {$wpdb->posts}.ID IN (SELECT posts.ID
FROM {$wpdb->posts} AS posts
INNER JOIN {$wpdb->term_relationships} AS term_relationships ON posts.ID = term_relationships.object_id
INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy ON term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id
INNER JOIN {$wpdb->terms} AS terms ON term_taxonomy.term_id = terms.term_id
WHERE term_taxonomy.taxonomy = 'product_type'
AND terms.slug = 'simple'
AND posts.post_type = 'product')";
return $wpdb->get_var($wpdb->prepare($sql, $term_id));
}
代码进入您的活动子主题(或主题)的 functions.php
文件。或者在任何插件 php 文件中。
用法
echo wh_get_min_price_per_product_cat($cat_id);
代码已经过测试并且有效。
参考:
希望对您有所帮助!
我想在 content-oroduct_cat.php
页面显示最低的 简单产品 价格。 Fancy Squares 中的以下代码用于显示最低价格,但我只想显示 简单产品 ,即省略分组产品。
/* SHOW LOWEST PRICE ON CATEGORY PAGE */
//woocommerce get lowest price in category
function wpq_get_min_price_per_product_cat($term_id)
{
global $wpdb;
$sql = "
SELECT MIN( meta_value+0 ) as minprice
FROM {$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
WHERE
( {$wpdb->term_relationships}.term_taxonomy_id IN (%d) )
AND {$wpdb->posts}.post_type = 'product'
AND {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->postmeta}.meta_key = '_price'
";
return $wpdb->get_var($wpdb->prepare($sql, $term_id));
}
我尝试使用:
AND {$wpdb->posts}.product_type = 'simple'
但这没有用。我如何只显示简单的产品?
You query is not working because
product_type
is not stored inposts
table it is stored interm_taxonomy
table. To get the desired you have to use Sub query, which will fetch all the simple product and the main query filter it according to category.
我已将您的 wpq_get_min_price_per_product_cat()
修改如下
function wh_get_min_price_per_product_cat($term_id)
{
global $wpdb;
$sql = "
SELECT MIN( meta_value+0 ) as minprice
FROM {$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
WHERE
( {$wpdb->term_relationships}.term_taxonomy_id IN (%d) )
AND {$wpdb->posts}.post_type = 'product'
AND {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->postmeta}.meta_key = '_price'
AND {$wpdb->posts}.ID IN (SELECT posts.ID
FROM {$wpdb->posts} AS posts
INNER JOIN {$wpdb->term_relationships} AS term_relationships ON posts.ID = term_relationships.object_id
INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy ON term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id
INNER JOIN {$wpdb->terms} AS terms ON term_taxonomy.term_id = terms.term_id
WHERE term_taxonomy.taxonomy = 'product_type'
AND terms.slug = 'simple'
AND posts.post_type = 'product')";
return $wpdb->get_var($wpdb->prepare($sql, $term_id));
}
代码进入您的活动子主题(或主题)的 functions.php
文件。或者在任何插件 php 文件中。
用法
echo wh_get_min_price_per_product_cat($cat_id);
代码已经过测试并且有效。
参考:
希望对您有所帮助!