WPeC-display Inventory count - text
Embed
You can embed this paste into a blog or website with this code:
<iframe class="codetidy" type="text/html" width="100%" src="http://codetidy.com/paste/embed/2890" frameborder="0"></iframe>
In the wpsc-single_product.php file I replaced line #151

<div id="stock_display_<?php echo wpsc_the_product_id(); ?>" class="in_stock"><?php _e('Product in stock', 'wpsc'); ?></div>

To this:

<div id="stock_display_<?php echo wpsc_the_product_id(); ?>" class="in_stock">

<?php function wpsc_variations_stock_remaining($product_id){

global $wpdb;

return $wpdb->get_var('

SELECT

sum(`pm`.`meta_value`)

FROM

`' . $wpdb->postmeta . '` `pm`

JOIN

`' . $wpdb->posts . '` `p`

ON

`pm`.`post_id` = `p`.`id`

WHERE

`p`.`post_type`= "wpsc-product"

AND

`p`.`post_parent` = ' . $product_id . '

AND

`pm`.`meta_key` = "_wpsc_stock"

');

}

?>

<?php// Added if statement to message will appear at 3 or less items;?>

<?php

$stock = get_post_meta( $post->ID, '_wpsc_stock', true );

$remaining_quantity = wpsc_variations_stock_remaining( $post->ID );

if ($stock <= "3" && $stock !=0){

_e('Only ', 'wpsc'); echo $stock;echo $remaining_quantity; _e(' left in stock. Order today!', 'wpsc');

}

;?>

this covers both stock for items without variations and items with variations (combining the number for all variation stock). However it does show 0 if you have variations without any stock set.

Please let me know if anyone who would know a better way or cleaner code to accomplish this. And possibly any alternative if statements to not show this for items without variation stock set.

Aside from this you can easily change the wording of the message, if you don't want to display the actual inventory number.

Change:

<code><?php _e('Product in stock', 'wpsc'); ?></code>

to:

<code><?php _e('Your Message Here', 'wpsc'); ?></code>
