WP E-Commerce add_to_cart shortcode with Quantity Box

A quick little code snippet that extends the in-built add_to_cart shortcode and adds a Quantity Box with it. Feel free to modify this code all you like.
Tested on WordPress 3.5.x and WP-E-Commerce 3.8.x

The original functions can be found in :
/wp-content/plugins/wp-e-commerce/wpsc-includes/display.functions.php (approx line 198)
/wp-content/plugins/wp-e-commerce/wpsc-includes/theme.functions.php (approx line 1196)

Just copy and paste the code into your functions.php file in your theme.
Then you can use the shortcode [cdl_add_to_cart=99] in your post or page, remember to replace the 99 with your own product ID.

[sourcecode language=”php”]
/*——————————————————————————————-*/
/* cdl_add cart button function used for php template tags and shortcodes
/*——————————————————————————————-*/

function cdl_add_to_cart_button( $product_id, $return = false ) {
global $wpdb,$wpsc_variations;
$output = ”;
if ( $product_id > 0 ) {
// grab the variation form fields here
$wpsc_variations = new wpsc_variations( $product_id );
if ( $return )
ob_start();
?>
<div class=’wpsc-add-to-cart-button’>
<form class=’wpsc-add-to-cart-button-form’ id=’product_<?php echo esc_attr( $product_id ) ?>’ action=” method=’post’>
<?php do_action( ‘wpsc_add_to_cart_button_form_begin’ ); ?>
<div class=’wpsc_variation_forms’>
<?php while ( wpsc_have_variation_groups() ) : wpsc_the_variation_group(); ?>
<p>
<select class=’wpsc_select_variation’ name=’variation[<?php echo wpsc_vargrp_id(); ?>]’ id='<?php echo wpsc_vargrp_form_id(); ?>’>
<?php while ( wpsc_have_variations() ): wpsc_the_variation(); ?>
<option value='<?php echo wpsc_the_variation_id(); ?>’ <?php echo wpsc_the_variation_out_of_stock(); ?>><?php echo esc_html( wpsc_the_variation_name() ); ?></option>
<?php endwhile; ?>
</select>
</p>
<?php endwhile; ?>
</div>

<legend>Quantity</legend>
<div class="wpsc_quantity_update">
<input id="wpsc_quantity_update_<?php echo esc_attr( $product_id ) ?>" name="wpsc_quantity_update" size="2" value="1" type="text">
<input name="key" value="-1" type="hidden">
<input name="wpsc_update_quantity" value="true" type="hidden">
</div><!–close wpsc_quantity_update–>

<input type=’hidden’ name=’wpsc_ajax_action’ value=’add_to_cart’ />
<input type=’hidden’ name=’product_id’ value='<?php echo $product_id; ?>’ />
<input type=’submit’ id=’product_<?php echo $product_id; ?>_submit_button’ class=’wpsc_buy_button’ name=’Buy’ value='<?php echo __( ‘Add To Cart’, ‘wpsc’ ); ?>’ />
<?php do_action( ‘wpsc_add_to_cart_button_form_end’ ); ?>
</form>
</div>
<?php

if ( $return )
return ob_get_clean();
}
}

function cdl_add_to_cart_shortcode( $content = ” ) {
static $fancy_notification_output = false;
if ( preg_match_all( "/\[cdl_add_to_cart=([\d]+)\]/", $content, $matches ) ) {
foreach ( $matches[1] as $key => $product_id ) {
$original_string = $matches[0][$key];
$output = cdl_add_to_cart_button( $product_id, true );
$content = str_replace( $original_string, $output, $content );
}

if ( ! $fancy_notification_output ) {
$content .= wpsc_fancy_notifications( true );
$fancy_notification_output = true;
}
}
return $content;
}
add_filter( ‘the_content’, ‘cdl_add_to_cart_shortcode’, 12 );
[/sourcecode]