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.

9 thoughts on “WP e-Commerce Shortcode – Add & Checkout Button

  1. Thanks for this – it does what it says on the tin! However my wp is multi language (dutch and english) when the button is used on the english product page it sends the user to the dutch (default language) version of the checkout. Any idea’s how to solve this?

  2. Have reinstalled my wp e-commerce installation and this short-code is no longer working for me. The button takes takes you to checkout page, however the product is not listed in the checkout. There is an item in checkout – strangely it’s a link to the ‘Hello World’ blog page ..with no image or price. Please can you help – I’m so near but so far!!

    • A couple of thoughts. First, did you reinstall the shortcode after updating WPEC? Any updates to WPEC will overwrite the existing shortcode functions file that includes this hack, so you have to go back through these instructions to recreate it. Second, check your product ID and verify that it is correct. Third, what version of WPEC are you running? We’ve verified that this shortcode works up to WPEC v3.8.7.6.2, but have not yet tested it with WPEC v3.8.8.x. Generally we wait a while before upgrading WPEC in our test environment, as we like to wait until the initial bugs are ironed out in major releases. WPEC v3.8.8 contained a lot of core updates, so we have been a bit more cautious with that upgrade.

  3. Am using v 3.8.8.1.

    Almost got it sorted – the id was incorrect ! Thanks for the info. However now the product (which is a donation) doesn’t pick up the donation value on the checkout page.

  4. Hi,

    My name is Justin – I’m one of the core developers for WP E-Commerce. Naturally, I’d advise against this – not only because it’s never advisable to hack core files – but this in particular provides no actual bug fixes to the product. It is an enhancement that could very, very easily be created as its own plugin – no need to hack core or worry about upgrade paths that way.

  5. It’s a great fix – although hacking the code isn’t ideal, has anybody actually created a plugin as Justin suggested if its very very easy? (Completely beyond me I’m afraid). I’d happily pay for this if I could style it easily as well (see below).

    The one problem that I’m having is that no matter what class name I pass, or eben if I try inline CSS, the button looks the same. Any pointers on this – what Style Sheet will it default to?

    Many thanks for any help.

    kind regards

    Daniel