Multiple loops or making your own loop in WP-Ecommerce

A client of mine needed a feature that could only be solved with a second WP-Ecommerce loop and another feature that required looping WP-Ecommerce out of it’s comfort zone (index.php)

You could use this code for custom loops, for example newest products, top selling products, newest products in a category.

The following code will loop through category ID 18 which in this example is a “Featured on Homepage” category.

$wpsc_query = new WPSC_query("category_id=18");
while (wpsc_have_products()) : wpsc_the_product();
echo '
          	<div class="cr_cont"> <a href="'.wpsc_the_product_permalink().'"><img src="'.wpsc_the_product_image('57','57').'" alt="" /></a>
                <span class="rc_in">
              <h4><a href="'.wpsc_the_product_permalink().'">'.wpsc_the_product_title().'</a></h4>
</span> </div>';

Simple as that. The good thing is that WPSC_query() is available throughout WordPress, so you can put it anywhere you like on the site!

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

12 thoughts on “Multiple loops or making your own loop in WP-Ecommerce”

  1. I’m using this code in home.php but I don’t get any products, only normal posts. Can you tell why?

  2. Can’t get it working in my project. 🙁
    I don’t have enough knowledge of PHP to figure out how to use global $wpsc_query either. I will keep hitting and trying.

  3. I get this error message:

    Fatal error: Call to undefined method WP_Query::WP_Query() in D:\xampp\htdocs\nmd\wp-content\plugins\wp-e-commerce\wpsc-core\wpsc-deprecated.php on line 476

  4. Turns out that wpsc_query has been deprecated since you can do the same thing with just plain old WP_Query:

    $wpsc_query = new WP_Query(‘post_type=wpsc-product’);
    while ( $wpsc_query->have_posts() ) : $wpsc_query->the_post();
    // Posts stuff here


  5. But when you the query by plain “WP_QUERY” you have some problem retrieving prices and other stuff for each product in the loop. Looks like wpsc_have_products() and wpsc_the_product() initialize and do some logic. For example, I did my own loop for display products ordered in categories. When I use the normal WP QUERY, functions inside the loop like “wpsc_product_on_special()” throw an error. Any solution?

  6. The two ways i’ve found to get the list of products from an specific category:


    In both cases the output will be the content of “wpsc-products_page.php” file…
    If you want to do one loop for customize in a different way your products list I think the only way is the way that Kieran wrote, but following his way I cant get the products of an specific category, someone know how to do this? Using the Kieran way??

    (sory for bad english)

  7. I’ve tried setting this up on my homepage. The loop works but the, as does the buy button, but the loading animation doesn’t appear. Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *