WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is more of a template tag question than a programming question, but since WPEC's wiki and forum is quite useless I had to give it a shot here.

Using shortcodes you can easily include a given number of products from a given category in any post and page.

But how can I achieve the following:

In the bottom of the single product view template I would like to show three random products from the same category as the product that is being viewed.

PS: I'm aware of the related product plugins that exists, but they're struggling with the new product variations in WPEC. That's why I prefer to use the standard category listing included in WPEC instead.

Thanks in advance for all kinds of help!

share|improve this question

migrated from stackoverflow.com Aug 22 '11 at 12:44

This question came from our site for professional and enthusiast programmers.

1  
Check this out ->wordpress.org/extend/plugins/random-posts-from-category – Rikesh Aug 22 '11 at 12:26
1  
This is a user support, Plugin-specific question that would be better-directed to the WP e-Commerce Plugin support forum. – Chip Bennett Feb 10 '12 at 17:23

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="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>
share|improve this answer

You can get the current category by:

<?php get_the_category( $id ) ?> - Here id is id of your current post.

Than-after use Random Post from Category plugin

And give the current category id to it.

And here you will get all products of the current product category.

share|improve this answer

protected by toscho Aug 27 '12 at 4:32

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?