【问题标题】:How can i query WooCommerce product by pricing of $00 product?我如何通过定价 00 美元的产品来查询 WooCommerce 产品?
【发布时间】:2020-11-01 14:20:22
【问题描述】:

我的模板中有一个自定义查询,我想按价格显示产品,我想查询免费产品,并且在我的主页上只显示 $0 产品。

【问题讨论】:

    标签: jquery wordpress woocommerce wordpress-theming woocommerce-theming


    【解决方案1】:
    $args = array(
                'post_type'      => 'product',
                'numberposts'      => -1,
                'meta_query'     => array(
                    'relation' => 'OR',
                    array(
                        'key'           => '_regular_price',
                        'compare' => 'NOT EXISTS'
                    ),
                    array( 
                        'key'           => '_price',
                        'compare' => 'NOT EXISTS'
                        
                    ),
                    array( 
                        'key'           => '_regular_price',
                        'compare' => '=',
                        'value' => 0,
                    ),
                    array( 
                        'key'           => '_price',
                        'compare' => '=',
                        'value' => 0,
                    )
                )
            );
            
            $products =  get_posts( $args );
    

    【讨论】:

      【解决方案2】:

      将此添加到您的主题functions.php,如果它有效,请告诉我。

      add_action( 'woocommerce_product_query', 'zero_price_products' );
      function zero_price_products( $q ){
          $meta_query = $q->get( 'meta_query' );
              $meta_query[] = array(
              'key'       => '_price',
              'value'     => 0,
              'compare'   => '='
          );
          $q->set( 'meta_query', $meta_query );
      }
      

      如果您想在您的主页上显示产品,请添加此代码:

      <?php
      $args = array(
          'post_type' => 'product',
          'posts_per_page' => 8,
          'post_status' => 'publish',
          'orderby' => 'id',
          'order' => 'DESC',
          'meta_query' => array(
                 array(
                 'key'       => '_price',
                 'compare'   => '=',
                 'value'      => 0,
                 )
              ),
      );
      
      
      $custom_posts = new WP_Query( $args );
      
      if ($custom_posts->have_posts() ) : while ($custom_posts->have_posts() ) : $custom_posts->the_post(); ?>
          <?php
              wc_get_template_part( 'content', 'product' );
           ?>
      <?php endwhile; endif; ?>
      

      【讨论】:

      • 如何在首页显示输出??
      • 这将改变所有存档页面的整个产品循环查询。
      • 他就是这么问的,我现在正在为一个循环编写一个解决方案,而不改变所有产品。
      • 他只在主页上要求 0.00 美元的产品。
      猜你喜欢
      • 2015-09-05
      • 2018-12-24
      • 1970-01-01
      • 2016-05-11
      • 2019-07-09
      • 2018-01-31
      • 2019-07-02
      • 2016-12-26
      • 2020-05-16
      相关资源
      最近更新 更多