我正在尝试制作一个woocommerce滑盖旋转木马。但是meta_query
不起作用,默认滑块正常工作。例如,如果我改为featured=yes
它没有显示我已经设置了一些特色产品的任何东西。请帮我解决这个问题。 Woo-commerce版本是3 +。
我的短代码
[product_carousel title="LATEST PRODUCTS" per_page="6" featured="no" latest="yes" best_sellers="no" on_sale="no" orderby="menu_order" order="desc"]
和我的功能是
<?php
function product_slider_carousel($atts){
global $wpdb, $woocommerce;
$arg_s = shortcode_atts(
array(
'title'=>'Product Slider',
'latest'=>'yes',
'per_page'=>'12',
'featured' => 'no',
'sale' => 'no',
'best_sellers'=>'no',
'on_sale'=>'no',
'orderby'=>'menu_order',
'order'=>'desc',
), $atts, 'product_carousel' );
//getting the values from shortcode
$title = $arg_s['title'];
$latest = $arg_s['latest'];
$featured = $arg_s['featured'];
$best_sellers = $arg_s['best_sellers'];
$on_sale = $arg_s['on_sale'];
$per_page = $arg_s['per_page'];
$orderby = $arg_s['orderby'];
$order = $arg_s['order'];
$args = array(
'post_type' => array( 'product', 'product_variation' ),
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1,
'meta_query' => '',
'fields' => 'id=>parent'
);
if(isset( $featured) && $featured == 'yes' ){
$args['meta_query'][] = array(
'key' => '_featured',
'value' => 'yes'
);
}
if(isset( $best_sellers) && $best_sellers == 'yes' ){
$args['meta_key'] = 'total_sales';
$args['orderby'] = 'meta_value';
$args['order'] = 'desc';
}
if(isset( $on_sale) && $on_sale == 'yes' ){
$args['meta_key'] = '_sale_price';
$args['meta_compare'] = '>=';
$args['meta_value'] = 0;
$sale_products = get_posts( $args );
$product_ids = array_keys( $sale_products );
$parent_ids = array_values( $sale_products );
// Check for scheduled sales which have not started
foreach ( $product_ids as $key => $id ) {
if ( get_post_meta( $id, '_sale_price_dates_from', true ) > current_time('timestamp') ) {
unset( $product_ids[ $key ] );
}
}
$product_ids_on_sale = array_unique( array_merge( $product_ids, $parent_ids ) );
set_transient( 'wc_products_onsale', $product_ids_on_sale );
}
$query_args = array(
'posts_per_page'=> $per_page,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'order' => $order,
'meta_query' => $args['meta_query'],
);
if(isset($atts['skus'])){
$skus = explode(',', $atts['skus']);
$skus = array_map('trim', $skus);
$query_args['meta_query'][] = array(
'key' => '_sku',
'value' => $skus,
'compare' => 'IN'
);
}
if(isset($atts['ids'])){
$ids = explode(',', $atts['ids']);
$ids = array_map('trim', $ids);
$query_args['post__in'] = $ids;
}
if ( isset( $category ) && $category!= 'null' && $category != 'a:0:{}' && $category != '0' && $category!="0, ") {
$query_args['product_cat'] = $category;
}
if (strcmp($on_sale, 'yes') == 0 ) {
if( empty( $product_ids_on_sale ) )
{ return; }
$query_args['post__in'] = $product_ids_on_sale;
}
if ( isset( $latest ) && $latest == 'yes' ) {
$orderby = 'date';
$order = 'desc';
}
switch( $orderby ) {
case 'rand':
$query_args['orderby'] = 'rand';
break;
case 'date':
$query_args['orderby'] = 'date';
break;
case 'price' :
$query_args['meta_key'] = '_price';
$query_args['orderby'] = 'meta_value_num';
break;
case 'sales' :
$query_args['meta_key'] = 'total_sales';
$query_args['orderby'] = 'meta_value_num';
break;
case 'title' :
$query_args['orderby'] = 'title';
break;
}
$the_query = new WP_Query( $query_args );
ob_start();
?>
<div class="row">
<div class="col-md-12">
<div class="product_wrap">
<div class="woocommerce">
<?php
if (isset($title)&&$title!=''){
echo '<h4>'.$title.'</h4>';
}else{
echo '<h4> </h4>';
}
?>
<ul class="products vpm-product-slider">
<?php
if($the_query->have_posts()) :
while($the_query->have_posts()) : $the_query->the_post();
// Product Details
get_template_part( "/templates/content", "product-shortcode" );
// Product Details
endwhile;
endif;
?>
</ul>
</div>
</div>
<!-- Query in Query-->
</div>
</div>
<?php
wp_reset_query();
return ob_get_clean();
}
add_shortcode('product_carousel','product_slider_carousel');
?>
答案 0 :(得分:3)
在特色产品的代码中,您应该使用税务查询。您可以在WC_Shortcode_Products
私有函数的set_visibility_featured_query_args()
源代码中轻松查看。
QMAKE_CXXFLAGS *= -wd4100
自Woocommerce 3以来, if(isset( $featured) && $featured == 'yes' ){
$args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'terms' => 'featured',
'field' => 'name',
'operator' => 'IN',
'include_children' => false, // optional
);
}
产品属性不再作为postmeta数据处理,但现在存储为 {{1}下的帖子"featured"
分类,以获得更好的表现。
答案 1 :(得分:0)
您使用旧的查询产品的方式,WooCommerce 3引入了querying products的标准方式。以下是获取所有特色产品的示例。它使用起来更容易,并且您可以编写未来的证据,因此您应该重写您所做的并将其转换为新方法。
// Get all featured products.
$args = array(
'featured' => true,
);
$products = wc_get_products( $args );