WordPress

Woocommerce: Adding Cross-sells to single product template

The client wanted to hand-pick which related products would be visible on the product pages.  So I replaced ‘related products’ with ‘cross sells’.

Step 1: get the ids of the cross sell products using the  ‘_crosssell_ids’ meta key.

<?php
 
/* crossells */
 
$crosssell_ids = get_post_meta( get_the_ID(), '_crosssell_ids' ); 
$crosssell_ids=$crosssell_ids[0];
 
?>

Step 2: Loop through the products by id

<?php
if(count($crosssell_ids)>0){
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'post__in' => $crosssell_ids );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?><a href='<?php the_permalink(); ?>'><?php
the_post_thumbnail( 'thumbnail' );
the_title();
?></a><?php
endwhile;
}
?>

14 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.