Woocommerce Featured Product by category - woocommerce

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:
Featured products currently display featured products from ALL categories.
The edit required:
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.

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"]

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();
?>

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>

Related

WooCommerce sidebar filter widgets not working with custom product WP_Query

I am creating a custom template to display all products that have a positive stock value. I have also manually included the shop sidebar into my template as well using dynamic_sidebar. However the product attribute filter widgets in my sidebar aren't displaying, I assume because those widgets don't know how to 'talk' to my WP_Query to know what products are being returned and what attribute filters to even show. If I manually add the filter query string to the URL (for example - ?filter_color=gray) it's not filtering my WP_Query either, I assume because this is a WooCommerce query parameter, not a Wordpress / WP_Query thing.
So I guess my questions are, 1. is there a way to connect my WP_Query to WooCommerce's attribute filter sidebar widgets, and or is there a better way to build out a custom template that's showing only products with positive stock values that's also filterable using WooCommerce's attribute filter widgets?
My WP_Query:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 1000,
'meta_query' => array(
array(
'key' => '_stock',
'value' => '1',
'compare' => '>',
)
),
'meta_key' => '_stock',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
My sidebar code:
<?php dynamic_sidebar( 'shop-sidebar' ); ?>

WordPress ACF 'post-object' and WooCommerce product = error on front page?

I have this WP_Query:
$query = new WP_Query(
array(
'posts_per_page' => $postsCount,
'post_type' => 'events',
'taxonomy' => 'events-category',
'meta_key' => 'event-start-date',
'orderby' => $postsOrderBy,
'order' => $postsOrder,
'meta_query' => array(
array(
'key' => 'event-start-date',
'compare' => '>=',
'value' => date("Y-m-d"),
'type' => 'DATE',
),
),
),
);
if ($query->have_posts()):
while ($query->have_posts()): $query->the_post();
// content
endwhile;
wp_reset_query();
endif;
In 'content' I have something like this:
<?php
$post_object = get_field('event-ticket');
if( $post_object ):
$post = $post_object;
setup_postdata( $post );
$product->get_id(); ?>
<div class="e-post__details-item e-post__details-item--event-price"><i class="e-post__details-item--icon fas fa-ticket-alt"></i><?php woocommerce_get_template( 'single-product/price.php' ); ?></div>
<div class="l-cta l-cta--events">
<div class="e-post__event-stock-status">
<?php if ( $product->is_in_stock() ) {
echo '<div class="e-post__event-stock-status-available">'.__('Dostępne!', 'woocommerce').'</div>';
} elseif ( ! $product->is_in_stock() ) {
echo '<div class="e-post__event-stock-status-unavailable">'.__('Wyprzedane!', 'woocommerce').'</div>';
} ?>
</div>
</div>
<?php
wp_reset_postdata();
endif;
?>
get_field('event-ticket') is ACF post object field type with 'Post Object' return object. This field pointing on WooCommerce product to get price and quantity stock.
WP_Query is loop of the CPT 'Events'. In every event I'm using post object field to point to the WooCommerce product, which is a ticket.
PROBLEM:
Datas from 'event-ticket' works on every subpages (archives, taxonomy, pages, cpt posts) but not on front (home) page:
Uncaught Error
: Call to a member function get_id() on null
What could be the reason?
You have not declared $product before. This is why you get this error. $product->get_id(); when you did not initialized $product earlier.
Maybe you want first:
$product = new WC_Product($post->ID);
I figure out: I just add global $product; before $product->get_id(); and now it's working :)

How to redirect on same page with first post data when click on a custom taxonomy?

I have a question related to WordPress. I have made a dropdown of custom taxonomies on single.php page That's the code I am using to list my taxonomies:
<ul class="dropdown-menu" aria-labelledby="dLabel">
<?php $terms = get_terms( array(
'taxonomy' => 'subject',
'hide_empty' => false,
) );
if( !empty( $terms ) ):
foreach ($terms as $key => $value) :
$term = $value->name;
?>
<li><?php echo $term; ?></li>
<?php endforeach; endif; wp_reset_query(); ?>
</ul>
Right now all taxonomy in dropdown have the same post link that is opend on the page. I want that every taxonomy should have the link of it's respective first post and when I click on a taxonomy it open that taxonomy's first post on the same single.php page. Is there any way of doing that?
Here is the picture of my page:
That's how my taxonomy dropdown look like if you zoom and look in the bottom left corner you will see the current post link that is showing on all subject names is there any way that I can change this link to that subject's first post link. Right now biology taxonomy post is open when I click on chemistry first post of chemistry open on this page. Please help me in doing this task I'll be very thankful to you.
After having the term-object, you can lookup the first post and get its permalink. in my example i ordered by date desc. Then set the link of the post:
<ul class="dropdown-menu" aria-labelledby="dLabel">
<?php
$terms = get_terms(array(
'taxonomy' => 'subject',
'hide_empty' => false,
));
if (!empty($terms)):
foreach ($terms as $key => $value) :
$term = $value->name;
//get the first post of this term **
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'numberposts' => 1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'subject',
'field' => 'id',
'terms' => $value->term_id,
)
)
);
$posts=get_posts($args);
$first_page_link = get_permalink($posts[0]->ID);
//**
?>
<li><?= $term ?></li>
<?php endforeach;
endif;
wp_reset_query(); ?>
</ul>
<?php
The code is not tested. And of course, you should do some checks, like what to do when no post is available with that term.

Texonomy template not showing filterd categories

I am created categories for my custom post type using regiter_texonomy. If I click on category it navigates to texonomy_template but not showing any custom posts. Why it is showing empty posts?
index.php
<?php
$defaults = array(
'taxonomy' => 'portfolio_category',
'title_li' => __( 'Categories' )
);
wp_list_categories($defaults); ?>
texonomy-portfolio_category.php
<?php
if(have_posts()):
echo 'Yes';
else:
echo 'No';
endif;
?>
But it showing an empty list.
there can be possibility that you might have empty Categories and due to that they might not being displayed using wp_list_categories so just try out the below args that should work.
<?php
$defaults = array(
'hide_empty' => false,
'taxonomy' => 'portfolio_category',
'title_li' => __( 'Categories' )
);
wp_list_categories($defaults);
?>

WP custom taxonomy description by term

Seen a lot of posts elsewhere, but nothing quite hitting the mark.
I need to echo the description of a specific term of a custom taxonomy related to a custom post type.
Right now, I'm doing the following loop successfully to pull content for related custom posts per term (in this case my custom post type is 'rental_gear' and my term is 'cameras':
<? $args = array(
'post_type'=> 'rental_gear',
'type' => 'cameras',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li>› <? the_title(); ?></li>
<?php endwhile; endif; wp_reset_postdata(); ?>
Can I pull the term's description in this loop?
I've tried the 'get_term' function and pulling '$term->description;' but those are related to the post, and this is on an overview page listing information from many posts (hence the use of the loop)
You didn't mention the name of your custom taxonomy, so replace your-taxonomy. This will query ALL rental_gear posts that have the camera term in your-taxonomy.
<?php
$args = array(
'post_type' => 'rental_gear',
'tax_query' => array(
array(
'taxonomy' => 'your-taxonomy',
'field' => 'slug',
'terms' => 'camera'
)
)
);
$query = new WP_Query( $args );

Resources