2
votes

I am looking for help (thank you) to edit the Woocommerce php widget for 'featured products'.

The file I think I need to edit is found:

wp-content/plugins/woocommerce/classes/widgets/class-wc-widget-featured-products.php

The problem: 1. Featured products currently display featured products from ALL categories.

The edit required: 1. To only display featured products within the current category being viewed.

So here is an example:

I have three categories (dog food, cat food, other stuff).

Each category has a category homepage:

> www.mysite.com/dog-food

> www.mysite.com/catfood

www.mysite.com/other-stuff

When I add a featured products to the 'dog-food' category, the featured product widget also includes featured cat-food products. Which is useless if the customer does not have a cat.

I would like featured products widget to display only products that are featured from the current category being viewed - So dog-food customers will only see dog-food featured products (not cat-food or other-things)

I (think) I need to add an arguement to the PHP file noted above. Here is the current code found within the file:

$query_args['meta_query'][] = array(
'key' => '_featured',
'value' => 'yes'
);

I have tried adding:

$query_args['meta_query'][] = array(
'key' => '_featured',
'value' => 'yes',
'product_cat' => 'current_category'
);

But this did not work.

Any help anyone could offer would be much appreciated.

Many thanks in advance.

3

3 Answers

4
votes

Use this wordpress plugin

https://wordpress.org/plugins/sp-woocommerce-featured-product-by-category/

In this plugin we have just changes the Featured product shortcode.

The default short code is : [featured_products per_page="12" columns="4"]

And change in this shortcode with the help of Featured product by categor plugin is : [featured_product_categories cats="CATEGORY_ID" per_cat="6" columns="3"]

1
votes

I'm not sure about editing the widget, but I've put it successfully in the archive-product template using this:

<?php
$term        = get_queried_object();
$category_id = empty( $term->term_id ) ? 0 : $term->term_id;    

$args = array(
    'post_type' => 'product',
    'meta_key' => '_featured',
    'meta_value' => 'yes',
    'posts_per_page' => 3,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $category_id
        )
     )
);

$featured_query = new WP_Query( $args );

if ($featured_query->have_posts()) : 

    while ($featured_query->have_posts()) : 

        $featured_query->the_post();

        $product = get_product( $featured_query->post->ID );

        wc_get_template_part( 'content', 'product' );

    endwhile;

endif;
wp_reset_query();

?>
0
votes

This is already late but since woocommerce keeps updating and they now introduce wc_get_products as the standard way to get products.

Below is the code I came up which I documented on my website. https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/

<?php 

// Display featured products by category. on this case its "shirts" which is the slug of the category.
$query_args = array(
    'featured' => true,  
    'category' => array( 'shirts' ),
);
$products = wc_get_products( $query_args );

global $post;
?>
<div class="woocommerce columns-<?php echo esc_attr( $columns ); ?>">
  <?php
    woocommerce_product_loop_start();
    foreach ($products as $product) {
        $post = get_post($product->get_id());
        setup_postdata($post);
        wc_get_template_part('content', 'product');
    }
    wp_reset_postdata();
    woocommerce_product_loop_end();
  ?>
</div>