个性化阅读
专注于IT技术分析

通过自定义分类将Woocommerce产品按类别分组

点击下载

我的产品具有Woocommerce默认产品类别, 因此具有自定义分类法设备。

我想按自定义分类法在每个产品类别搜索下列出我的产品。

例如:

设备具有价值:devise-1和devise-2

当使用devise-1搜索时, 列表将为:

  • 类别-1
    • 产品1
    • 产品2
  • 类别-2
    • 产品1
    • 产品2

#1


你好!你好吗?我们了解你希望通过按自定义类别类型进行搜索来导出帖子。如果是这样, 我认为这会有所帮助。对不起, 我的英语不好。

对不起, 如果你的问题我理解错了。

        $your_slug_custom_tax = ( isset($_GET['{your_var_custom_tax}']) ) ? $_GET['{your_var_custom_tax}'] : '';

    $your_config = array(
            'post_type'      => 'product', //If woocommerce native
            'posts_per_page' => '-1', //You can change for your preference
            'post_status'    => 'publish', //status of post or product for show
            'tax_query'      => array()    // this is for you custom search
        );

    if($your_slug_custom_tax != ''){
            $your_config['tax_query'][] = array(
                'taxonomy' => '{your slug of taxonomy}', 'field' => 'slug', 'terms' => $your_slug_custom_tax // your taxonomy for show post.
            );
        }
     $your_config = get_posts($your_config);

     if (  $your_config ) {
            foreach (  $your_config as $mypost ):
            print_r($mypost);
    endforeach;
    }


#2


我找到了满足我要求的解决方案, 但我认为它很昂贵。因为它是每个类别的查询数据库。

`

<?php
   $args               = array(
  'orderby'    => 'title', 'order'      => 'ASC', 'hide_empty' => TRUE, 'taxonomy'   => 'product_cat'
    );

    $product_categories =  get_terms($args);
    foreach ($product_categories as $product_category) : ?>
        <h2 class="title ">
        <a href=" <?php echo get_term_link($product_category) ?>"> <?= $product_category->name; // Print Product categories ?> </a>
        </h2>

  <?php
           $args     = array(
          'posts_per_page' => -1, 'tax_query'      => array(
            'relation' => 'AND', array(
              'taxonomy' => 'product_cat', 'field'    => 'slug', 'terms'    => $product_category->slug
            ), array(
              'taxonomy' => 'device', 'field'    => 'slug', 'terms'    => $device_type, 'operator' => 'IN'
            )
          ), 'post_type'      => 'product', 'orderby'        => 'sku, '
        );
        $products = new WP_Query($args);
     // Inner Product loop
     while ($products->have_posts()): $products->the_post(); ?>
            <?php wc_get_template_part('content', 'product');  ?>
          <?php endwhile; ?>

`

任何人都有一个简单的解决方案。谢谢

赞(0)
未经允许不得转载:srcmini » 通过自定义分类将Woocommerce产品按类别分组

评论 抢沙发

评论前必须登录!