Woocommerce meta_query 不适用于特色产品 [英] Woocommerce meta_query not working for featured products

查看:28
本文介绍了Woocommerce meta_query 不适用于特色产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作 woocommerce 滑块轮播.但 meta_query 不起作用,只有默认滑块在起作用.例如,如果我更改为 featured=yes,它不会在我已经设置了一些特色产品的地方显示任何内容.请帮我解决这个问题.Woo-commerce 版本是 3+.

我的短代码

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

而我的功能是

'是','per_page'='12','特色' =>'不','销售' =>'不','best_sellers'=>'no','on_sale'='no','orderby'=>'menu_order','order'='desc',), $atts, 'product_carousel' );//从短代码中获取值$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 = 数组('post_type' =>数组('产品','product_variation'),'post_status' =>'发布','posts_per_page' =>-1,'ignore_sticky_posts' =>1、'meta_query' =>'','字段' =>'id=> 父母');if(isset( $featured) && $featured == 'yes' ){$args['meta_query'][] = array('键' =>'_特色','价值' =>'是的');}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 );//检查尚未开始的预定销售foreach ( $product_ids as $key => $id ) {if ( get_post_meta( $id, '_sale_price_dates_from', true ) > current_time('timestamp') ) {取消设置( $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 = 数组('posts_per_page'=>$per_page,'no_found_rows' =>1、'post_status' =>'发布','post_type' =>'产品','订单' =>$订单,'meta_query' =>$args['meta_query'],);if(isset($atts['skus'])){$skus = expand(',', $atts['skus']);$skus = array_map('trim', $skus);$query_args['meta_query'][] = array('键' =>'_sku','价值' =>$skus,'比较' =>'在');}if(isset($atts['ids'])){$ids = expand(',', $atts['ids']);$ids = array_map('trim', $ids);$query_args['post__in'] = $ids;}if ( isset( $category ) && $category!= 'null' && $category != 'a:0:{}' && $category != '0' && $类别!="0,") {$query_args['product_cat'] = $category;}如果 (strcmp($on_sale, 'yes') == 0 ) {如果(空($product_ids_on_sale)){ 返回;}$query_args['post__in'] = $product_ids_on_sale;}如果 ( isset( $latest ) && $latest == 'yes' ) {$orderby = '日期';$order = 'desc';}开关($orderby){案例兰德":$query_args['orderby'] = 'rand';休息;案例日期":$query_args['orderby'] = 'date';休息;案例价格":$query_args['meta_key'] = '_price';$query_args['orderby'] = 'meta_value_num';休息;案例销售":$query_args['meta_key'] = 'total_sales';$query_args['orderby'] = 'meta_value_num';休息;案例标题":$query_args['orderby'] = 'title';休息;}$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如果 (isset($title)&&$title!=''){echo '<h4>'.$title.'</h4>';}别的{echo '<h4>&nbsp;</h4>';}?><ul class="products vpm-product-slider"><?phpif($the_query->have_posts()) :while($the_query->have_posts()) : $the_query->the_post();//产品详情get_template_part( "/templates/content", "product-shortcode" );//产品详情终了;万一;?>

<!-- 查询中的查询-->

<?phpwp_reset_query();返回 ob_get_clean();}add_shortcode('product_carousel','product_slider_carousel');?>

解决方案

在您的特色产品代码中,您应该改用税务查询.您可以在 中轻松看到这一点WC_Shortcode_Products set_visibility_featured_query_args() 私有函数.

if(isset( $featured) && $featured == 'yes' ){$args['tax_query'][] = array('分类' =>'product_visibility','条款' =>'特色','字段' =>'姓名','运算符' =>'在','include_children' =>false,//可选);}

从 Woocommerce 3 开始,"featured" 产品属性不再作为 postmeta 数据处理,而是像 post term "featured" 一样存储code> 在 'product_visibility' 分类法下,以获得更好的性能.

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

解决方案

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.

这篇关于Woocommerce meta_query 不适用于特色产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆