Last few months I’m posted other tutorial for WP e-Commerce displaying featured products, I thought that’s the last thing they forgot to add in their Plugin built-in widgets but no, there are a lot more, I also noticed displaying best-selling products is one of the great feature and can’t be ignore, so today I’ll give you this simple snippet for displaying best-selling products in WP e-Commerce in an easy way.

How this snippet works?

  • Get lists of purchased products ordered by max number of product purchased
  • Use WP_Query to query those product ids, yes it’s possible since WordPress Ecommerce uses custom post-type for adding products
  • Use WPSC code to display product title, featured image, price or even add to add button
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
    global $wpdb;
    // Assign table name to a variable
    $tbl_cart = $wpdb->prefix . ‘wpsc_cart_contents’;
    // Select prodid field from wpsc_cart_contents table
    $products = $wpdb->get_results( “SELECT prodid, COUNT(prodid) as prod_num FROM $tbl_cart GROUP BY prodid ORDER BY prod_num DESC LIMIT 0, 4″ );
    // Check if there is sold product
    if( $products )
    {
        $product_ids = array();
        // Get lists of purchase product ids
        foreach( $products as $product )
            $product_ids[] = $product->prodid;
        $args = array(
            ’post_type’ => ‘wpsc-product’,
            ’post__in’     => $product_ids
        );
        $the_query = new WP_Query( $args );
// The Loop
        if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) : $the_query->the_post();
                // add your product code here like, product title, image, price or even add to cart button, don’t worry the wpsc code works fine in here ;)
            endwhile;
        } else {
            _e(‘<p>No product sold yet.</p>’);
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    }
?>

0 comments:

Post a Comment

 
Top