Show Featured products in product category pages

I would like some help with the following issue with woocommerce.

I am using wordpress 3.5.2, woocommerce 2.0.13 and sommerce theme.

Read More

I have a product category page, in which I display 5 subcategories.
Under the 5 subcategory images, I would like to have some featured products or random products of these categories. Do you know how I could do that?

Related posts

2 comments

  1. I just double checked and WooCommerce runs the product category description through the_content filters, which means that it should run shortcodes.

    WooCommerce has plenty of shortcodes, see their documentation

    Including featured products:

    [featured_products per_page="12" columns="4"]
    

    The downside to that is that the featured products might not all be from that specific category. You didn’t mention if that was an issue or not.

    If so, then you can duplicate the code from the [featured_products] (which is really just running a secondary loop with WP_Query), tweak it a bit and add the output to the woocommerce_before_shop_loop hook.

    function wpa_107952_featured (){
    
        if( !is_product_category() )
            return;
    
    
        $args = array(
            'post_type' => 'product',
            'product_cat' => get_query_var('product_cat'),
            'post_status' => 'publish',
            'ignore_sticky_posts'   => 1,
            'posts_per_page' => 8,
            'meta_query' => array(
                array(
                    'key' => '_visibility',
                    'value' => array('catalog', 'visible'),
                    'compare' => 'IN'
                ),
                array(
                    'key' => '_featured',
                    'value' => 'yes'
                )
            )
        );
    
        ob_start();
    
        $products = new WP_Query( $args );
    
        $woocommerce_loop['columns'] = 4;
    
        if ( $products->have_posts() ) : ?>
    
            <?php woocommerce_product_loop_start(); ?>
    
                <?php while ( $products->have_posts() ) : $products->the_post(); ?>
    
                    <?php woocommerce_get_template_part( 'content', 'product' ); ?>
    
                <?php endwhile; // end of the loop. ?>
    
            <?php woocommerce_product_loop_end(); ?>
    
        <?php endif;
    
        wp_reset_postdata();
    
        echo '<div class="woocommerce">' . ob_get_clean() . '</div>';
    }
    
    add_action( 'woocommerce_before_shop_loop', 'wpa_107952_featured' );
    
  2. This is a bit late but for people who are looking for answers you can download, install and activate https://wordpress.org/plugins/featured-products-by-category/ it uses the latest and standard woocommerce function wc_get_products.

    Once activated put the following code in your functions.php

    <?php
    
    /*
       Show featured products first. Before the normal product list
    */
    add_action('woocommerce_before_shop_loop', function() {
    
        if(is_product_category()) {
            $current_term = get_queried_object();
            echo do_shortcode('[featured_products_by_category cat="'.$current_term->slug.'" limit=3]');
        }
      
    });
    

    See the full post here -> https://jameshwartlopez.com/plugin/show-featured-products-in-product-category-pages/

Comments are closed.