'Woocommerce - Append a recommended product at the start of the product loop
On product archive page, i filter results according to certain attributes. Everything works well with filtering but additionally i want to show 2 of results at the start of the loop which belongs to certain category as recommended products. The idea is show the correct results but also promoting first 2 products of a given category by showing them at first.
Current situation: url: http://localhost/woo/product-category/clothing/?filter_color=blue
Results: 10 blue, clothing category products.
What i want to do is, if there are any product also has jean (blue color) category, showing these at most 2 blue jean/clothing products at the start of the loop. I will be having 12 products with the same filtered url. Later i will add different badge to these 2 items to hightlight them.
woocommerce/layouts/category.php
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
woocommerce_product_loop_end();
funtions.php
add_action( 'woocommerce_product_loop_start', function(){
if ( is_product_category() && is_main_query() ) :
wc_get_template( 'loop/loop-start.php' );
$args = array(
'post_type' => 'product',
'post__in' => array( 12, 13 ), // Your product IDs
);
$sticky = new WP_Query( $args );
while ($sticky->have_posts()) :
$sticky->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
endif;
} );
Solution 1:[1]
Try this:
woocommerce/layouts/category.php
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
do_action( 'woocommerce_product_loop_sticky' );
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
woocommerce_product_loop_end();
funtions.php
add_action( 'woocommerce_product_loop_sticky', function(){
wc_get_template( 'loop/loop-start.php' );
$args = array(
'post_type' => 'product',
//'post__in' => array( 12, 13 ), // Your product IDs
//OR
'posts_per_page' => 2,
'tax_query' => array(
array (
'taxonomy' => 'advert_tag',
'field' => 'id',
'terms' => array( 18 ), // Your product catedory ID
)
),
);
$sticky = new WP_Query( $args );
while ($sticky->have_posts()) :
$sticky->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
} );
Or you can use only this code (without changing the loop in the category template) in the functions.php:
add_action( 'woocommerce_shop_loop', 'woocommerce_shop_loop_stiky');
function woocommerce_shop_loop_stiky(){
$args = array(
'post_type' => 'product',
//'post__in' => array( 12, 13 ), // Your product IDs
//OR
'posts_per_page' => 2,
'tax_query' => array(
array (
'taxonomy' => 'advert_tag',//
'field' => 'id',
'terms' => array( 18 ), // Your product catedory ID
)
),
);
$sticky = new WP_Query( $args );
while ($sticky->have_posts()) :
$sticky->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
remove_action( 'woocommerce_shop_loop', 'woocommerce_shop_loop_stiky' );
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
