Add Cross-sells to single product template in Woocommerce

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;
}
?>

Comments

Popular posts from this blog

Stop video playing when Bootstrap modal is closed