RSS Feed

WP eCommerce product by id

June 2, 2011 by Dave

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]


13 Comments »

  1. Sarah Bayly says:

    thanks for sharing!

    It kind of works, but for some reason is showing the most popluar pages, not products – have I done something wrong?

    Also anyway of getting this in a widget (i want it in the footer) rather than a page / post?

    Many thanks, Sarah

  2. ChainsawDR says:

    Hi,

    I’m trying to get this working but I’m really struggling and would appreciate any help that you could offer. I’ve posted a question on Stack Exchange that summarises the problem here: http://wordpress.stackexchange.com/questions/21041/how-to-show-best-selling-wp-e-commerce-products)

    Any help would be a godsend

    ChainsawDR

  3. ChainsawDR says:

    Hi,

    We’ve been trying to progress this code some more, we’ve been updating a thread on this here:
    http://getshopped.org/forums/topic.php?id=20214

    • admin says:

      That’s brilliant! I’m not sure why it works for my friends site and not yours but im very happy that you’ve worked it out. Ill be putting a new post up soon to show how to integrate a custom widget to expand the image upload and text functions. Thanks for commenting and improving the code buddy! :)

  4. ChainsawDR says:

    Hi Dave,

    I’ve almost finished the coding but I’m having a real problem pulling in the price for variational products (where the product price is 0, but has variations with prices assigned).

    Was hoping you could take a quick look on that thread to see if anything jumps out at you to solve it? Hit a wall at the moment and not sure what the next thing to try should be.

    Thanks Dave

    ChainsawDR

  5. liza says:

    is there any way that I can use the popular products on widget? I’m actually trying to use the wp-ecommerce bestselling products but it’s fetching the blogpost id instead of product ids. Thanks!

  6. Faruque says:

    many many thanks, helped a lot.

  7. Imogen says:

    Hi Dave,

    Was the conclusion that there isn’t a way to find out a product’s ID on WP ecommerce? All I want to do is put an ‘add to cart’ button on a page that displays an image of one of my products (not the actual products page itself) and can’t seem to find the product ID anywhere! Driving me mad!

    Appreciate any help you might have!

    Imogen

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>