最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Cluster WooCommerce products in each Category Archive by Tags assigned to products

programmeradmin0浏览0评论

Hello WordPress specialists. I am building my dream shop and I prepared a structure which I can not build now. Can you help me think about it?

  • I prepared a product_cat categories which are based on product usage - Design, Data Storage, Web, Cloud solutions and subcategories by Product range - Microsoft 365, Google Workspace, Adobe Creative cloud etc.
  • I also prepared a product_tag tags which are based on brand or creator - Microsoft, Adobe, MalwareBytes...

Now I got stuck with building my layout for category/tag archives. My idea is to display Row for each Creator on Category page. So it should look like:

Category Archive Example: Complete cloud solutions

  • Tag: Google
  • Subcategory: Google Workspace
  • Products: G suite business, G suite enterprise...
  • Tag: Microsoft
  • Subcategory: Microsoft 365
  • Products: Microsoft 365 Business, Microsoft 365 Business Premium...

Tag Archive example: Adobe

- Category: Graphics & Design

  • Products: Illustrator, InDesign,...

- Category: Data Analysis

  • Products: Analytics

The thing is, how to set it up? I am lost after some days, propably I am looking at it from wrong side. I have already built a code for displaying the child categories under main category archive for same taxonomy typ - product_cat. Displaying child taxonomies as clusters in woocommerce

What do you suggest to use? I was thinking about:

  1. Get queried object to get the term
  2. Identify if it is product_cat or product_tag (somehow?)
  3. For each product in current query check the product_cat & product_tag and display it in rows (somehow?)

Now my code displays the subcategories and get all tags, but I dont know how to add the tag to each subcategory loop to display it as a main tag for each subcategory..

<?php
$term = get_queried_object(); // Get current archive page
$children = get_terms($term->taxonomy, array(
  'parent'    => $term->term_id,
  'hide_empty' => false
));
$stitky = get_terms( 'product_tag');

if ($children) {
  foreach ($children as $subcat) {
    echo '<h2><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . // Display child taxonomy name
    $subcat->name . ' (' . $subcat->count . ')' . '</a></h2>' . $subcat->description; // Display child taxonomy item count
    woocommerce_subcategory_thumbnail( $subcat );
    $kategorie = $subcat->slug; // Set child category slug for each query of products
    $args = array(
      'post_type' => 'product',
      'product_cat' => $kategorie, 
      'posts_per_page' => 1,
      'orderby' => 'meta_value_num',
      'meta_key' => '_price',
      'order' => 'asc'
    );

    $loop = new WP_Query($args);
    if ($loop->have_posts()) {
    $znacky = wp_get_post_terms( get_the_id(), 'product_tag' );
      
      while ($loop->have_posts()) : $loop->the_post(); ?>
        <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); //Display product title ?></a></h4>
        <?php $product = wc_get_product(get_the_ID()); // get the WC_Product Object ?>
        <p><?php echo $product->get_price_html(); // Display product Price ?></p>
        <img src="<?php echo wp_get_attachment_url( $product->get_image_id() ); ?>" />

        <?php endwhile; ?><?php
                        } else {
                          echo __('No products found');
                        }
                        wp_reset_postdata(); // Reset Query
                          ?>
        <!--/.products-->
    <?php

  }
}
    ?>  

in the end of the day, is it possible? How would you go? ...

Thanks for your time and in advance for your ideas too.

发布评论

评论列表(0)

  1. 暂无评论