WP E-commerce: Showing 3 random products from current category when viewing product

Try this in your wpsc-single_product.php template. It will give you a list with title and link. I didn’t test this with product variations, I am not using them in the site I am working with and I wasn’t sure from your question if you needed it to. Hopefully this at least gives you a starting place.

<?php 
// get the product categories
$product_categories = wp_get_object_terms( wpsc_the_product_id(), 'wpsc_product_category', array('fields' => 'ids') );
// arguments
$args = array(
'post_type' => 'wpsc-product',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
    array(
        'taxonomy' => 'wpsc_product_category',
        'field' => 'id',
        'terms' => $product_categories
    )
)
);
$related_products = new WP_Query( $args );
// loop over query
if ($related_products->have_posts()) :
echo '<ul>';
while ( $related_products->have_posts() ) : $related_products->the_post();
?>
    <li><a href="https://wordpress.stackexchange.com/questions/26563/<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>

Leave a Comment