WP e-Commerce Shortcode – Add & Checkout Button

WP e-Commerce Shortcode: Add & Go Direct to Checkout Button for WP e-Commerce

While customizing WP e-Commerce recently, I needed to add a button that would add a specific product variation to a customer’s shopping cart and go direct to the checkout page. What I ended up doing was adding a custom WP e-Commerce Shortcode. It’s a simple function really. It creates a button that runs the custom function.

Brief instructions follow. NOTE: technically this is a WP e-Commerce hack, and will be overwritten during upgrades of WP e-Commerce. In order to preserve it, the code below will need to be re-added to the WP e-Commerce shortcodes PHP file following an upgrade.

  1. Make a backup copy of
    ../wordpress/wp-content/plugins/wp-e-commerce/wpsc-includes/shortcode.functions.php
  2. At the bottom of the WP e-Commerce shortcodes PHP file –
    ../wordpress/wp-content/plugins/wp-e-commerce/wpsc-includes/shortcode.functions.php

    Add the code listed below, without removing or editing the existing text in that file.

  3. Save and close the WP e-Commerce shortcodes PHP file.
  4. Go to Store->Settings->Presentation and click on Flush Theme Cache.
  5. Add the add_and_go_direct_to_cart shortcode to any page or post where you wish to use it.

Code to be added to the shortcode.functions.php file

/**
 * Custom shortcode function added to create a button that adds product to cart and takes customer directly to checkout page.
 */
function wpsc_add_and_go_direct_to_cart_shortcode( $atts ) {
	// extract parameters and substitute defaults if necessary
	extract( shortcode_atts( 
		array(
			'class' => '',
    			'id' => '0',
    			'name' => '',
    			'value' => 'Add to Cart'
    			),
		$atts ) 
		);
	// build form for button
	$output = array(
		"<form action='" . get_option( 'shopping_cart_url' ) . "' method='post'>n",
		"<input type='hidden' name='wpsc_ajax_action' value='add_to_cart'>n",
		"<input type='hidden' name='product_id' value='" . $id . "'>n",
		"<input type='submit' id='product__submit_button' class='" . $class . "' name='" . $name . "' value='" . $value . "'>n",
		"</form>n"
		);
	// return form for output
	return implode( "", $output );
}

add_shortcode( $tag = 'add_and_go_direct_to_cart', $func = 'wpsc_add_and_go_direct_to_cart_shortcode');

add_and_go_direct_to_cart Shortcode Syntax

[add_and_go_direct_to_cart class='my_button_class' id=1 name='buy_my_product' value='Buy My Product']

Where id = your WPSC product id, class = your CSS button class, and value = your desired button label.