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

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.

Read More

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!

Related posts

Leave a Reply

2 comments

  1. 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();
    ?>
    
  2. 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.