WP eCommerce Shortcodes

Below, you’ll find a detailed guide on how to add the WP eCommerce Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the WP eCommerce Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the WP eCommerce Plugin and the shortcodes it provides:

Plugin Icon
WP eCommerce

"WP eCommerce is a robust WordPress plugin designed to transform your website into an efficient online store. It offers a seamless shopping experience, making your ecommerce venture a breeze."

★★★✩✩ (258) Active Installs: 5000+ Tested with: 5.5.13 PHP Version: 5.6
Included Shortcodes:
  • [buy_now_button]
  • [wpsc_products]
  • [showcategories]
  • [wpsc-cart]
  • [wpsc-products]
  • [wpsc-add-to-cart]

WP eCommerce [buy_now_button] Shortcode

The WP e-Commerce shortcode, ‘buy_now_button’, enables a direct purchase option for a specified product. When implemented, it calls the ‘wpsc_buy_now_shortcode’ function. This function generates a ‘Buy Now’ button for the product defined by ‘product_id’. The button redirects users to the checkout page, simplifying the buying process.

Shortcode: [buy_now_button]

Parameters

Here is a list of all possible buy_now_button shortcode parameters and attributes:

  • product_id – the unique identifier of the specific product

Examples and Usage

Basic example – A simple usage of the ‘buy_now_button’ shortcode to display a buy now button for a specific product. The product is identified by its unique ‘product_id’.

[buy_now_button product_id=101 /]

Advanced examples

Example 1: In this example, we are using the ‘buy_now_button’ shortcode to display a buy now button for multiple products. The products are identified by their unique ‘product_id’s. Note that each ‘product_id’ is separated by a comma.

[buy_now_button product_id="101, 102, 103" /]

Example 2: In this example, we are using the ‘buy_now_button’ shortcode to display a buy now button for a specific product and we are also passing a second parameter ‘true’. This second parameter, when set to ‘true’, allows the button to be displayed even if the product is out of stock.

[buy_now_button product_id=101 true /]

PHP Function Code

In case you have difficulties debugging what causing issues with [buy_now_button] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'buy_now_button', 'wpsc_buy_now_shortcode' );

Shortcode PHP function:

function wpsc_buy_now_shortcode( $atts ) {
	$output = wpsc_buy_now_button( $atts['product_id'], true );
	return $output;
}

Code file location:

wp-e-commerce/wp-e-commerce/wpsc-components/theme-engine-v1/helpers/shortcodes.php

WP eCommerce [wpsc_products] Shortcode

The ‘wpsc_products’ shortcode from the WP e-commerce plugin is used to display products. It accepts various parameters like product_id, category_id, price, limit_of_items, etc., enabling users to customize the product display. This shortcode can be disabled on certain products. It also supports pagination and sorting order, offering flexibility for product listing.

Shortcode: [wpsc_products]

Parameters

Here is a list of all possible wpsc_products shortcode parameters and attributes:

  • product_id – Identifier for specific products
  • old_product_id – Identifier for previous versions of products
  • product_url_name – URL name for the product
  • product_name – Name of the product
  • category_id – Identifier for specific categories
  • category_url_name – URL name for the category
  • tag – Tag associated with the product
  • price – Price of the product, ‘sale’ for sale products
  • limit_of_items – Restricts the number of items
  • sort_order – Order of sorting: name, price, ID etc.
  • order – Ascending or Descending order
  • number_per_page – Number of products per page
  • page – Page number to show the products

Examples and Usage

Basic example – Show a specific product by using its product_id.

[wpsc_products product_id=5 /]

Advanced examples

Show a specific product within a specific category. The shortcode will display the product with product_id 5 from the category with category_id 3.

[wpsc_products product_id=5 category_id=3 /]

Display all sale products in descending order. The shortcode will show all products with a sale price, listed in descending order.

[wpsc_products price='sale' order='DESC' /]

Show a specific number of products per page. The shortcode will display 10 products per page.

[wpsc_products number_per_page=10 /]

Display products from multiple categories. The shortcode will show products from the categories with ids 3, 5 and 7.

[wpsc_products category_id='3,5,7' /]

Show products from a specific page. The shortcode will display products from page 2.

[wpsc_products page=2 /]

Display products with a specific tag. The shortcode will show products with the tag ‘summer’.

[wpsc_products tag='summer' /]

PHP Function Code

In case you have difficulties debugging what causing issues with [wpsc_products] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wpsc_products' , 'wpsc_products_shorttag' );

Shortcode PHP function:

function wpsc_products_shorttag( $atts ) {
	// disable this shortcode on products
	if ( get_post_type() == 'wpsc-product' ) {
		return '';
	}

	$query = shortcode_atts( array(
		'product_id'        => 0,
		'old_product_id'    => 0,
		'product_url_name'  => null,
		'product_name'      => null,
		'category_id'       => 0,
		'category_url_name' => null,
		'tag'               => null,
		'price'             => 0, //if price = 'sale' it shows all sale products
		'limit_of_items'    => 0,
		'sort_order'        => null, // name,dragndrop,price,ID,author,date,title,modified,parent,rand,comment_count
		'order'             => 'ASC', // ASC or DESC
		'number_per_page'   => 0,
		'page'              => 0,
	),
	$atts,
	'wpsc_products' );

	$post_id_array = explode( ',', $query['product_id'] );
	$cat_id_array  = explode( ',', $query['category_id'] );

	if ( ! empty( $post_id_array ) && count( $post_id_array ) > 1 ) {
		$query['product_id'] = $post_id_array;
	}

	if ( ! empty( $cat_id_array ) && count( $cat_id_array ) > 1 ) {
		$query['category_id'] = $cat_id_array;
	}

	if ( get_option( 'use_pagination', false ) ) {
		$page_number = get_query_var( 'paged' );

		if ( ! $page_number ) {
			$page_number = get_query_var( 'page' );
		}

		$query['page']            = $page_number;
		$query['number_per_page'] = get_option( 'wpsc_products_per_page' );
	}

	if ( ! empty( $atts['number_per_page'] ) ) {
		$query['number_per_page'] = $atts['number_per_page'];
	}

	return wpsc_display_products_page( $query );
}

Code file location:

wp-e-commerce/wp-e-commerce/wpsc-components/theme-engine-v1/helpers/shortcodes.php

WP eCommerce [showcategories] Shortcode

The WP e-Commerce plugin shortcode ‘showcategories’ displays a list of all product categories. It calls the ‘wpsc_show_categories’ function which includes the ‘wpsc-category-list.php’ template file. The output is then cleaned and returned for display.

Shortcode: [showcategories]

Examples and Usage

Basic example – Show all categories in your e-commerce store

[showcategories /]

PHP Function Code

In case you have difficulties debugging what causing issues with [showcategories] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'showcategories', 'wpsc_show_categories'   );

Shortcode PHP function:

function wpsc_show_categories( $content ) {

	ob_start();
	include( wpsc_get_template_file_path( 'wpsc-category-list.php' ) );
	$output = ob_get_contents();

	ob_end_clean();
	return $output;

}

Code file location:

wp-e-commerce/wp-e-commerce/wpsc-components/theme-engine-v1/helpers/shortcodes.php

WP eCommerce [wpsc-cart] Shortcode

The WP e-Commerce shortcode ‘wpsc-cart’ manages the display of the shopping cart. It checks for items in the cart, and if empty, it returns a message. Depending on the ‘type’ attribute, it can display the cart as a form, widget, or table. It then calls the appropriate class to handle the display. Shortcode: [wpsc-cart type=”form”]

Shortcode: [wpsc-cart]

Parameters

Here is a list of all possible wpsc-cart shortcode parameters and attributes:

  • type – determines the layout of the cart display

The ‘type’ parameter can have three different values, each presenting a different layout for the cart display.

  • form – displays the cart items in a form layout
  • widget – presents the cart items in a widget form
  • table – shows the cart items in a table format

Examples and Usage

Basic example – Displaying the WPSC cart in form layout.

[wpsc-cart type="form" /]

Advanced examples

Displaying the WPSC cart in widget layout. This can be useful when you want to integrate the cart as a widget in your sidebar or other widget areas.

[wpsc-cart type="widget" /]

Displaying the WPSC cart in table layout. This can be beneficial when you have multiple products in your cart and want to display them in a structured format.

[wpsc-cart type="table" /]

Please note that these shortcodes will only work if the WP e-Commerce plugin is active and properly configured on your WordPress site.

PHP Function Code

In case you have difficulties debugging what causing issues with [wpsc-cart] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wpsc-cart'       , '_wpsc_shortcode_cart' );

Shortcode PHP function:

function _wpsc_shortcode_cart( $atts ) {
	global $wpsc_cart;

	ob_start();

	$defaults = array(
		'type' => 'form'
	);

	$atts = shortcode_atts( $defaults, $atts, 'wpsc-cart' );

	require_once( WPSC_TE_V2_CLASSES_PATH . '/cart-item-table-widget-form.php' );

	if ( ! count( $wpsc_cart->cart_items ) ) {
		return '<p>' . __( 'No items in cart.', 'wp-e-commerce' ) . '</p>';
	}

	switch ( $atts['type'] ) {
		case 'form' :
			$table = new WPSC_Cart_Item_Table_Form();
			break;
		case 'widget' :
			$table = new WPSC_Cart_Item_Table_Widget_Form();
			break;
		case 'table' :
			$table = new WPSC_Cart_Item_Table();
			break;
	}

	$table->display();
	return ob_get_clean();

}

Code file location:

wp-e-commerce/wp-e-commerce/wpsc-components/theme-engine-v2/helpers/shortcodes.php

WP eCommerce [wpsc-products] Shortcode

The WP e-Commerce shortcode ‘wpsc-products’ is used to display products on your WordPress site. It has several attributes to customize the display, like ‘id’, ‘display’, ‘per_page’, etc. The ‘id’ attribute allows you to select specific products. The ‘display’ attribute can be set to ‘all’, ‘sale’, or ‘not-sale’ to filter products based on their sale status. The ‘per_page’ attribute controls the number of products displayed per page. The ‘paged’ and ‘offset’ attributes are used for pagination. The ‘template_part’ attribute lets you choose the template part to use for the product loop. The ‘category_in’, ‘category_not_in’, ‘tag_in’, and ‘tag_not_in’ attributes are used to filter products based on their categories and tags.

Shortcode: [wpsc-products]

Parameters

Here is a list of all possible wpsc-products shortcode parameters and attributes:

  • id – The specific product’s unique identifier.
  • display – Determines which products to show: all, on sale, or not on sale.
  • per_page – Sets how many products to display per page.
  • paged – The page number to display.
  • offset – Skips a certain number of returned products.
  • template_part – Specifies the part of the template to use.
  • category_in – Shows products in these specific categories.
  • category_not_in – Excludes products in these specific categories.
  • tag_in – Displays products with these specific tags.
  • tag_not_in – Hides products with these specific tags.

Examples and Usage

Basic example – Display all the products without any filters or restrictions.

[wpsc-products]

Advanced examples

Display products from specific categories by using the ‘category_in’ attribute. The categories should be separated by commas.

[wpsc-products category_in="clothing,accessories"]

Display products that are not in certain categories by using the ‘category_not_in’ attribute. The categories should be separated by commas.

[wpsc-products category_not_in="books,music"]

Display products with specific tags by using the ‘tag_in’ attribute. The tags should be separated by commas.

[wpsc-products tag_in="summer,sale"]

Display products that are not tagged with certain tags by using the ‘tag_not_in’ attribute. The tags should be separated by commas.

[wpsc-products tag_not_in="winter,new"]

Display products that are on sale by using the ‘display’ attribute with the value ‘sale’.

[wpsc-products display="sale"]

Display a specific number of products per page by using the ‘per_page’ attribute.

[wpsc-products per_page="10"]

Display products starting from a certain position by using the ‘offset’ attribute.

[wpsc-products offset="5"]

Display a specific product by using the ‘id’ attribute.

[wpsc-products id="101"]

PHP Function Code

In case you have difficulties debugging what causing issues with [wpsc-products] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wpsc-products'   , '_wpsc_shortcode_products' );

Shortcode PHP function:

function _wpsc_shortcode_products( $atts ) {
	// Default and allowed attributes
	$defaults = array(
		'id'              => 0,
		'display'         => 'all',
		'per_page'        => -1,
		'paged'           => 1,
		'offset'          => 0,
		'template_part'   => 'loop-products',
		'category_in'     => '',
		'category_not_in' => '',
		'tag_in'          => '',
		'tag_not_in'      => '',
	);

	$args = array( 'post_type' => 'wpsc-product' );

	$atts = shortcode_atts( $defaults, $atts, 'wpsc-products' );

	// Query by post ID
	$atts['id'] = str_replace( ' ', '', $atts['id'] );
	$ids =   empty( $atts['id'] )
	       ? array()
	       : array_map( 'absint', explode( ',', $atts['id'] ) );

	if ( ! empty( $ids ) ) {
		$args['post__in'] = $ids;
	}

	// Meta query for whether to select products on sale or not
	switch ( $atts['display'] ) {
		case 'sale':
			$args['meta_query'] = array(
				array(
					'key'     => '_wpsc_special_price',
					'value'   => array( '', '0' ),
					'compare' => 'NOT IN',
				)
			);
			break;

		case 'not-sale':
			$args['meta_query'] = array(
				array(
					'key'     => '_wpsc_special_price',
					'value'   => array( '', '0' ),
					'compare' => 'IN',
				)
			);
			break;
	}

	// Pagination
	if ( $atts['per_page'] !== -1 ) {
		$args['posts_per_page'] = absint( $atts['per_page'] );
		$args['paged']          = absint( $atts['paged'] );
		$args['offset']         = absint( $atts['offset'] );
	} else {
		$args['nopaging'] = true;
	}

	// Taxonomy queries
	$atts['category_in']     = str_replace( ' ', '', $atts['category_in'] );
	$atts['category_not_in'] = str_replace( ' ', '', $atts['category_not_in'] );
	$atts['tag_in']          = str_replace( ' ', '', $atts['tag_in'] );
	$atts['tag_not_in']      = str_replace( ' ', '', $atts['tag_not_in'] );

	$atts['category_in']     =   empty( $atts['category_in'] )
	                           ? array()
	                           : array_map( 'absint', explode( ',', $atts['category_in'] ) );

	$atts['category_not_in'] =   empty( $atts['category_not_in'] )
	                           ? array()
	                           : array_map( 'absint', explode( ',', $atts['category_not_in'] ) );

	$atts['tag_in']          =   empty( $atts['tag_in'] )
	                           ? array()
	                           : array_map( 'absint', explode( ',', $atts['tag_in'] ) );

	$atts['tag_not_in']      =   empty( $atts['tag_not_in'] )
	                           ? array()
	                           : array_map( 'absint', explode( ',', $atts['tag_not_in'] ) );

	$args['tax_query'] = array();

	// Category slug in
	if ( ! empty( $atts['category_in'] ) ) {
		$args['tax_query'][] = array(
			'taxonomy' => 'wpsc_product_category',
			'field'    => 'slug',
			'terms'    => $atts['category_in'],
			'operator' => 'IN',
		);
	}

	// Category slug not in
	if ( ! empty( $atts['category_not_in'] ) ) {
		$args['tax_query'][] = array(
			'taxonomy' => 'wpsc_product_category',
			'field'    => 'slug',
			'terms'    => $atts['category_not_in'],
			'operator' => 'NOT IN',
		);
	}

	// Product tag in
	if ( ! empty( $atts['tag_in'] ) ) {
		$args['tax_query'][] = array(
			'taxonomy' => 'product_tag',
			'field'    => 'slug',
			'terms'    => $atts['tag_in'],
			'operator' => 'IN',
		);
	}

	// Product tag not in
	if ( ! empty( $atts['tag_not_in'] ) ) {
		$args['tax_query'][] = array(
			'taxonomy' => 'product_tag',
			'field'    => 'slug',
			'terms'    => $atts['tag_not_in'],
			'operator' => 'NOT IN',
		);
	}

	// I don't like query posts either but we need to preserve the ability
	// to use the_post() from within the templates, without having to resort
	// to tags like $query->the_post()
	query_posts( $args );

	ob_start();

	wpsc_get_template_part( $atts['template_part'] );

	$output = ob_get_clean();

	wp_reset_query();

	return $output;
}

Code file location:

wp-e-commerce/wp-e-commerce/wpsc-components/theme-engine-v2/helpers/shortcodes.php

WP eCommerce [wpsc-add-to-cart] Shortcode

The WPSC-Add-To-Cart shortcode is a part of the wp-e-commerce plugin. It allows adding products directly to the cart using their ID. The PHP function _wpsc_shortcode_add_to_cart() is triggered when this shortcode is used. It takes product ID as an attribute. If the ID is empty, it returns nothing; otherwise, it calls the wpsc_get_add_to_cart_form() function with the product ID as a parameter, displaying the ‘Add to Cart’ form for the specified product.

Shortcode: [wpsc-add-to-cart]

Parameters

Here is a list of all possible wpsc-add-to-cart shortcode parameters and attributes:

  • id – The unique identifier of the product to be added to the cart

Examples and Usage

Basic Example – A simple way to use the shortcode to add a product to the cart by referencing the product’s ID.

[wpsc-add-to-cart id=1 /]

Advanced Examples

Example 1 – Adding multiple products to the cart by using their respective IDs. This can be useful when you want to promote a bundle of products or a set.

[wpsc-add-to-cart id=1 /]
[wpsc-add-to-cart id=2 /]
[wpsc-add-to-cart id=3 /]

Example 2 – Although the shortcode doesn’t support multiple IDs in a single instance, you can use it multiple times on a page to add different products. This example shows how to add three different products to the cart using their IDs.

[wpsc-add-to-cart id=1 /]
[wpsc-add-to-cart id=2 /]
[wpsc-add-to-cart id=3 /]

Please note that the ID should correspond to the actual product ID in your WP e-Commerce store for the shortcode to work properly.

PHP Function Code

In case you have difficulties debugging what causing issues with [wpsc-add-to-cart] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wpsc-add-to-cart', '_wpsc_shortcode_add_to_cart' );

Shortcode PHP function:

function _wpsc_shortcode_add_to_cart( $atts ) {
	$defaults = array( 'id' => 0 );
	$atts     = array_map( 'absint', shortcode_atts( $defaults, $atts ) );

	if ( empty( $atts['id'] ) ) {
		return '';
	}

	return wpsc_get_add_to_cart_form( $atts['id'] );
}

Code file location:

wp-e-commerce/wp-e-commerce/wpsc-components/theme-engine-v2/helpers/shortcodes.php

Conclusion

Now that you’ve learned how to embed the WP eCommerce Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *