【问题标题】:Handle Woocommerce visibility for hidden products in a WP Query在 WP 查询中处理隐藏产品的 Woocommerce 可见性
【发布时间】:2019-04-03 03:46:26
【问题描述】:

你能帮我解决这个问题吗?我有一个产品列表,我想要隐藏那些在产品可见性细节上标记为隐藏的产品。这是我的代码:

 $args = array(
                'posts_per_page' => -1,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        // 'terms' => 'white-wines'
                        'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id,
                        'visibility' => 'visible' //NOT WORKING...
                    )
                ),
                'post_type' => 'product',
                'orderby'    => 'menu_order',      
                'order'           =>  'ASC',      

            );
            $products = new WP_Query( $args );

            if(isset($_GET['staging']) && $_GET['staging'] == "true") {
                echo "<pre>" . print_r($products) . "</pre>";
            }

我想显示所有标记为可见的产品。

【问题讨论】:

    标签: php wordpress woocommerce product custom-taxonomy


    【解决方案1】:

    自 Woocommerce 3 以来,产品可见性由分类法 product_visibility 处理,用于术语 exclude-from-catalog,因此您需要在税务查询数组中使用第二个数组:

    $terms = array( $product_categories[$wc_arr_key[$wc_cat_id]]->term_id );
    
    $products = new WP_Query( array(
        'post_type'         => 'product',
        'post_status'       => 'publish',
        'posts_per_page'    => -1,
        'tax_query'         => array(
            'relation'      => 'AND',
            array(
                'taxonomy'  => 'product_cat',
                'field'     => 'term_id',
                'terms'     => $terms
            ),
            array(
                'taxonomy'  => 'product_visibility',
                'terms'     => array('exclude-from-catalog'),
                'field'     => 'name',
                'operator'  => 'NOT IN',
            ),
        ),
        'orderby'           => 'menu_order',
        'order'             =>  'ASC',
    ) );
    
    
    if(isset($_GET['staging']) && $_GET['staging'] == "true") {
        echo "<pre>" . print_r($products) . "</pre>";
    }
    

    经过测试并且有效。

    相关:Database changes for products in woocommerce 3

    【讨论】:

      【解决方案2】:

      使用以下代码排除 hidden 产品并仅显示 visible 产品

      $args = array(
                      'posts_per_page' => -1,
                      'tax_query' => array(
                          'relation' => 'AND',
                          array(
                              'taxonomy' => 'product_cat',
                              'field' => 'term_id',
                              // 'terms' => 'white-wines'
                              'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id
                          )
                      ),
                      'meta_query' => array(
                          array(
                             'key'       => '_visibility',
                             'value'     => 'hidden',
                             'compare'   => '!='
                               )
                       ),
                      'post_type' => 'product',
                      'orderby'    => 'menu_order',      
                      'order'           =>  'ASC'      
      
                  );
                  $products = new WP_Query( $args );
      
                  if(isset($_GET['staging']) && $_GET['staging'] == "true") {
                      echo "<pre>" . print_r($products) . "</pre>";
                  }
      

      【讨论】:

        【解决方案3】:

        WooCommerce 将此数据保存为元数据,因此您需要针对名称 _visibility 运行元查询。这看起来像

        'meta_query' => array(
            array(
                'key'       => '_visibility',
                'value'     => 'hidden',
                'compare'   => '!=',
            )
        )
        

        这将拉取所有元可见性不等于隐藏的帖子

        【讨论】:

          猜你喜欢
          • 2019-12-28
          • 2019-05-11
          • 2018-07-18
          • 2013-07-25
          • 2014-08-20
          • 1970-01-01
          • 2018-11-11
          • 2019-09-15
          • 1970-01-01
          相关资源
          最近更新 更多