dongye9453 2018-03-01 22:55
浏览 92
已采纳

Woocommerce meta_query不适用于特色产品

I am trying to make a woocommerce slider carousel. but the meta_queryis not working only default slider is working. for example if i changed to featured=yes its not showing anything where i already set some featured product. Please help me to solve this issue. Woo-commerce Version is 3+.

My shortcode

[product_carousel title="LATEST PRODUCTS" per_page="6" featured="no" latest="yes" best_sellers="no" on_sale="no" orderby="menu_order" order="desc"]

and My function is

<?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>&nbsp;</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');
    ?>
  • 写回答

2条回答 默认 最新

  • douxie1692 2018-03-02 05:11
    关注

    In your code for featured products, you should use a tax query instead. You can see that easily in WC_Shortcode_Products source code for set_visibility_featured_query_args() private function.

    if(isset( $featured) && $featured == 'yes' ){
        $args['tax_query'][] = array(
            'taxonomy'         => 'product_visibility',
            'terms'            => 'featured',
            'field'            => 'name',
            'operator'         => 'IN',
            'include_children' => false, // optional
        );
    }
    

    Since Woocommerce 3, "featured" product property is not anymore handled as postmeta data, but are now stored like a post term "featured" under 'product_visibility' taxonomy, for better performances.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题