Woocommerce meta_query not working for featured pr

2019-07-18 02:03发布

问题:

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');
    ?>

回答1:

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.



回答2:

You're using the old way of querying products, WooCommerce 3 introduced a standard way of querying products. Here is an example of getting all the featured products. It's much easier to use and future proofs you code so you should rewrite what you have done and convert it to the new methods.

// Get all featured products.
$args = array(
    'featured' => true,
);
$products = wc_get_products( $args );