【问题标题】:Woocommerce - List product by using specific attributesWoocommerce - 使用特定属性列出产品
【发布时间】:2015-07-03 05:27:03
【问题描述】:

我正在尝试进行自定义查询以获取具有“League = NCAA”的产品,如图所示。我总共有 74 个NCAA 联赛

http://i.stack.imgur.com/MQDAL.png

现在我的查询是这样的,但它什么也没显示。

<ul class="products grid ncaa">
        <?php
    $args = array(
        'post_type' => 'product',
        'meta_key' => 'league',
        'meta_value' => 'ncaa',
        'posts_per_page' => 12

        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
         woocommerce_get_template( 'loop/no-products-found.php' );
    }

?>
      </ul>

从数据库中搜索关键字“NCAA”后,它显示了这张表,但我不知道如何从这里获取:-

http://i.stack.imgur.com/kjK17.png

查看表格后,我尝试进行此查询,它仅在前端显示单个产品,但我想显示包含产品的所有 ncaa 属性。

 <ul class="products grid ncaa">
        <?php
    $args = array(
        'post_type' => 'product',
        'meta_key' => '_product_attributes',
        'meta_value' => 'a:2:{s:6:"league";a:6:{s:4:"name";s:6:"League";s:5:"value";s:4:"NCAA";s:11:"is_taxonomy";i:0;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}s:3:"upc";a:6:{s:4:"name";s:3:"UPC";s:5:"value";s:12:"889345125070";s:11:"is_taxonomy";i:0;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}}',
        'posts_per_page' => 12

        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
         woocommerce_get_template( 'loop/no-products-found.php' );
    }

?>
      </ul>

【问题讨论】:

  • 看看这个question
  • 对不起,我也试过这个。

标签: php mysql wordpress woocommerce


【解决方案1】:

我刚刚做了这个,它就像一个魅力:-)

 <ul class="products grid ncaa">
                            <?php
                            $args = array(
                                //'meta_compare' => 'LIKE',
                                'post_type'      => 'product',
                                'posts_per_page' => '74',
                                'meta_query'     => array(
                                    array(
                                        'key'     => '_product_attributes',
                                        'value'   => 'ncaa',
                                        'compare' => 'LIKE',
                                        'type'    => 'ncaa'
                                    )
                                )
                            );
                            $loop = new WP_Query( $args );

                            if ( $loop->have_posts() ) {
                                while ( $loop->have_posts() ) : $loop->the_post();
                                    {
                                        wc_get_template_part( 'content', 'product' );
                                    }
                                endwhile;
                            } else {
                                woocommerce_get_template( 'loop/no-products-found.php' );
                            }
                            ?>
                        </ul>

【讨论】:

    【解决方案2】:

    希望这会有所帮助:

     <ul class="products grid ncaa">
        <?php
    
    $args = array(
        'meta_key'     => '_product_attributes',
        'meta_value'   => '',
        'meta_compare' => '!=',
        'post_type'    => 'product'
    );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
    
            $data = get_post_meta($post->ID, '_product_attributes', true);
            if(in_array('NCAA',$data)){
                wc_get_template_part( 'content', 'product' );
            }
            else{
                //Do Nothing
            }
        endwhile;
    } else {
         woocommerce_get_template( 'loop/no-products-found.php' );
    }
    
    ?>
    </ul>
    

    更多参考请访问:Class Reference/WP Query - Custom Field Parameter

    【讨论】:

    • 谢谢先生的回答,但没有奏效。它向我显示空白页。
    • 试试 print_r($data) 看看你在循环中得到了什么
    • 尝试将 if 条件从 if(in_array('NCAA' , $data)) 更改为 if(in_array('ncaa', $data))。看看它是否适合你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 2017-03-27
    • 2018-12-14
    相关资源
    最近更新 更多