Skip to content

Instantly share code, notes, and snippets.

@davemackintosh
Created May 6, 2012 16:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davemackintosh/2623103 to your computer and use it in GitHub Desktop.
Save davemackintosh/2623103 to your computer and use it in GitHub Desktop.
Popular Products Module
<?php
//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);
//Keep a counter
$i = 1;
//Loop through the results
foreach ($pp as $item) {
$product = get_post($item['prodid']);
the_post();
var_dump(get_post_meta($item['prodid']));
?>
<div class="product-grid popular-<?php echo $i?>">
<p class="popular-name">
<?php
$string = $product->post_title;
if (strlen($string) > 17) {
$string = substr($string, 0, 17);
print "{$string}..";
}
else print $string;
?>
</p>
<a href="<?php echo get_permalink($item['prodid'])?>">
<span class="img">
<?php echo get_the_post_thumbnail($item['prodid'])?>
</span>
<span class="price">
<span>From only </span><?php echo the_product_price($item['prodid'])?>
</span>
</a>
</div>
<?php
$i++;
}
//reset everything
$pp = $i = $item = null;
}
add_shortcode('popular_products', popular_products);
function the_product_price($id, $special_price = false) {
if ($special_price) {
return wpsc_currency_display(get_post_meta($id, '_wpsc_special_price', true));
} else {
return wpsc_currency_display(get_post_meta($id, '_wpsc_price', true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment