So I’ve trawled the interwebs for days and hours now trying to find out how you bloody get products by their ID in WP eCommerce (the best WordPress eCommerce solution) and I’ve given in and just edited the core files of WP eCommerce to allow this to work.
Basically, I edited a few functions to get the top sellers on a friends website on the front page.
Below are the functions I edited they’re all in wp-includes/product-template.php.
wpsc_the_product_permalink ( ) on line 716 my function now looks like this
/**
* wpsc product permalink function
* @var int - The ID of the product you want the permalink for (optional)
* @return string - the URL to the single product page for this product
*/
function wpsc_the_product_permalink ( $id = false ) {
global $wp_query;
if ( $id != false ){
return get_permalink ( $id );
}
else return get_permalink ( );
}
And wpsc_the_product_title ( ) on line 684, my code now looks like this
/**
* wpsc the product title function
* @var int - the product you want the name of (optional)
* @return string - the product title
*/
function wpsc_the_product_title ( $id = false) {
if ( $id != false )
return get_the_title( $id );
else return get_the_title ( );
}
For wpsc_the_product_thumbnail ( ) I just passed the third argument through so I used the function like this
wpsc_the_product_thumbnail ( null, null, $product_id );
For wpsc_the_product_price ( ) I added an extra argument to the function (I’m not posting the whole thing here, its massive) so line 387 looks like this
function wpsc_the_product_price( $no_decimals = false, $id = false ) {
I also changed line 394 to look like this
$product_id = ( $id != false ) ? $id : get_the_ID ( );
For anyone looking for a quick easy “best sellers” function in wp ecommerce use the below code. Piece of cake, I even made it a short code! WIN.
//Popular products function
function popular_products ( $atts ) {
//Expose the Db to the function
global $wpdb;
//Get the results
$pp = $wpdb -> get_results ( "SELECT `prodid`, SUM(quantity) FROM `{$wpdb->prefix}wpsc_cart_contents` GROUP BY `prodid` ORDER BY `quantity` DESC LIMIT {$atts['limit']}", ARRAY_A);
//Loop through the results
foreach ( $pp as $item ) {
//Output it
?>
<a href="<?php echo wpsc_the_product_permalink ( $item['prodid'] )?>">
<img alt="<?php echo wpsc_the_product_title ( $item['prodid'] )?>" src="<?php echo wpsc_the_product_thumbnail ( null, null, $item['prodid'] )?>" />
<span><?php echo wpsc_the_product_price ( false, $item['prodid'] )?>>
<?php echo wpsc_the_product_title ( $item['prodid'] )?>
</a>
<?php
}
}
add_shortcode ( 'dm_popular', popular_products );
to use the wp ecommerce best seller shortcode just put into your posts/pages [dm_popular limit=10]