Created
May 16, 2017 01:06
-
-
Save doubleedesign/2165125c8d9b8be2d15cc60926e0b347 to your computer and use it in GitHub Desktop.
WooCommerce - Show an unordered list of cross-sells on the single product page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Use on single product template ?> | |
<div class="related"> | |
<?php | |
// Customised: Show cross-sells on single product pages, under the attributes and short description | |
global $post; | |
$crosssells = get_post_meta( $post->ID, '_crosssell_ids',true); | |
if($crosssells) { | |
echo '<h2>Related products</h2>'; | |
echo '<ul>'; | |
foreach ($crosssells as $item) { | |
// WP_Query arguments | |
$args = array ( | |
'p' => $item, | |
'post_type' => array( 'product' ), | |
'post_status' => array( 'publish' ), | |
); | |
// The Query | |
$related = new WP_Query( $args ); | |
// The Loop | |
if ( $related->have_posts() ) { | |
while ( $related->have_posts() ) { | |
$related->the_post(); | |
?> | |
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li> | |
<?php | |
} | |
} else { | |
// no posts found | |
} | |
// Restore original Post Data | |
wp_reset_postdata(); | |
} | |
echo '</ul>'; | |
} | |
?> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment