WooCommerce Shortcodes

Below, you’ll find a detailed guide on how to add the WooCommerce 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 WooCommerce Plugin shortcodes not to show or not to work correctly.

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

Plugin Icon
WooCommerce

"WooCommerce is a comprehensive eCommerce plugin for WordPress, offering seamless online store creation. Its user-friendly interface promotes business growth."

★★★★✩ (4148) Active Installs: 5000000+ Tested with: 6.3.2 PHP Version: 7.4
Included Shortcodes:
  • [product]
  • [product_page]
  • [product_category]
  • [product_categories]
  • [add_to_cart]
  • [add_to_cart_url]
  • [products]
  • [recent_products]
  • [sale_products]
  • [best_selling_products]
  • [top_rated_products]
  • [featured_products]
  • [product_attribute]
  • [related_products]
  • [shop_messages]
  • [woocommerce_order_tracking]
  • [woocommerce_cart]
  • [woocommerce_checkout]
  • [woocommerce_my_account]

WooCommerce [product] Shortcode

The Woocommerce Product shortcode is a handy tool for displaying specific products. This shortcode allows you to showcase a product by its SKU or ID. If no attributes are provided, it will return an empty string. The ‘limit’ attribute is set to ‘1’, meaning only one product will be displayed. The final line initiates the shortcode and returns the product’s content.

Shortcode: [product]

Parameters

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

  • sku – The SKU (Stock Keeping Unit) is a unique identifier that you assign to each product in your store. You can use this parameter to display a specific product by its SKU.
  • id – The ID is a unique identifier that WordPress automatically assigns to each product when it’s created. You can use this parameter to display a specific product by its ID.
  • limit – The Limit parameter determines the number of products to be displayed. In this case, it is set to ‘1’, meaning that only one product will be displayed.

Examples and Usage

Basic example – Displaying a single product using its ID

[product id=123 /]

Advanced examples

Displaying a single product using its SKU. If the product with the specified SKU is not found, no product will be displayed.

[product sku="abc123" /]

Displaying a single product using both ID and SKU. The shortcode will first try to display the product with the specified ID. If no product is found with that ID, it will then try to display the product with the specified SKU.

[product id=123 sku="abc123" /]

These examples showcase the flexibility of the ‘product’ shortcode. You can choose to display a product based on its ID, SKU, or both. Remember that the ‘id’ and ‘sku’ parameters are optional, and if neither is provided, the shortcode will return an empty string.

PHP Function Code

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

Shortcode PHP function:

	public static function product( $atts ) {
		if ( empty( $atts ) ) {
			return '';
		}

		$atts['skus']  = isset( $atts['sku'] ) ? $atts['sku'] : '';
		$atts['ids']   = isset( $atts['id'] ) ? $atts['id'] : '';
		$atts['limit'] = '1';
		$shortcode     = new WC_Shortcode_Products( (array) $atts, 'product' );

		return $shortcode->get_content();
	}

WooCommerce [product_page] Shortcode

The ‘Product Page’ shortcode is used to display a specific product on any page. It fetches product details either by product ID or SKU. The PHP code executes the following functions: It validates the input parameters, sets up a query to fetch the product, and handles product variations. It also controls the display of the product title and modifies the add-to-cart form action to prevent redirection. Finally, it generates the HTML output for the product page. This shortcode is particularly useful for highlighting a specific product in posts or custom landing pages.

Shortcode: [product_page]

Parameters

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

  • id – The unique identifier of the product. Used to display a specific product.
  • sku – The Stock Keeping Unit of the product. Used to display a product using its SKU.
  • status – The publication status of the product. Default value is ‘publish’, and it is used to display products based on their publication status.
  • show_title – A boolean value. If set to false, the product title will not be displayed.

Examples and Usage

Basic example – Display a single product page by its ID

[product_page id="99"]

Advanced examples

Display a single product page by its SKU, and do not show the product title

[product_page sku="abc123" show_title="0"]

Display a single product page by its ID, and change the post status to ‘draft’

[product_page id="99" status="draft"]

Display a single product page by its SKU, and change the post status to ‘private’

[product_page sku="abc123" status="private"]

Please note that these shortcodes should be placed in the content area of a page or post where you want the product page to appear. The ‘id’ or ‘sku’ attribute is required to specify which product to display, and the ‘show_title’ and ‘status’ attributes are optional.

PHP Function Code

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

Shortcode PHP function:

	public static function product_page( $atts ) {
		if ( empty( $atts ) ) {
			return '';
		}

		if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) {
			return '';
		}

		$args = array(
			'posts_per_page'      => 1,
			'post_type'           => 'product',
			'post_status'         => ( ! empty( $atts['status'] ) ) ? $atts['status'] : 'publish',
			'ignore_sticky_posts' => 1,
			'no_found_rows'       => 1,
		);

		if ( isset( $atts['sku'] ) ) {
			$args['meta_query'][] = array(
				'key'     => '_sku',
				'value'   => sanitize_text_field( $atts['sku'] ),
				'compare' => '=',
			);

			$args['post_type'] = array( 'product', 'product_variation' );
		}

		if ( isset( $atts['id'] ) ) {
			$args['p'] = absint( $atts['id'] );
		}

		// Don't render titles if desired.
		if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
			remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
		}

		// Change form action to avoid redirect.
		add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );

		$single_product = new WP_Query( $args );

		$preselected_id = '0';

		// Check if sku is a variation.
		if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {

			$variation  = wc_get_product_object( 'variation', $single_product->post->ID );
			$attributes = $variation->get_attributes();

			// Set preselected id to be used by JS to provide context.
			$preselected_id = $single_product->post->ID;

			// Get the parent product object.
			$args = array(
				'posts_per_page'      => 1,
				'post_type'           => 'product',
				'post_status'         => 'publish',
				'ignore_sticky_posts' => 1,
				'no_found_rows'       => 1,
				'p'                   => $single_product->post->post_parent,
			);

			$single_product = new WP_Query( $args );
			?>
			<script type="text/javascript">
				jQuery( function( $ ) {
					var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' );

					<?php foreach ( $attributes as $attr => $value ) { ?>
						$variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' );
					<?php } ?>
				});
			</script>
			<?php
		}

		// For "is_single" to always make load comments_template() for reviews.
		$single_product->is_single = true;

		ob_start();

		global $wp_query;

		// Backup query object so following loops think this is a product page.
		$previous_wp_query = $wp_query;
		// @codingStandardsIgnoreStart
		$wp_query          = $single_product;
		// @codingStandardsIgnoreEnd

		wp_enqueue_script( 'wc-single-product' );

		while ( $single_product->have_posts() ) {
			$single_product->the_post()
			?>
			<div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>">
				<?php wc_get_template_part( 'content', 'single-product' ); ?>
			</div>
			<?php
		}

		// Restore $previous_wp_query and reset post data.
		// @codingStandardsIgnoreStart
		$wp_query = $previous_wp_query;
		// @codingStandardsIgnoreEnd
		wp_reset_postdata();

		// Re-enable titles if they were removed.
		if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
			add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
		}

		remove_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );

		return '<div class="woocommerce">' . ob_get_clean() . '</div>';
	}

WooCommerce [product_category] Shortcode

The WooCommerce ‘product_category’ shortcode is a powerful tool for displaying products from a specific category. This shortcode filters products based on the ‘category’ attribute. If no category is assigned, it returns an empty string. It allows customization of product limit, columns, order, and category operator. The default limit is 12 products, displayed in 4 columns, ordered by menu order and title in ascending order.

Shortcode: [product_category]

Parameters

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

  • limit – sets the maximum number of products you want to display. By default, it’s set to 12.
  • columns – defines the number of columns in which the products will be displayed. The default is 4, meaning your product grid will have 4 products per row.
  • orderby – determines the sort order of the products. The default setting is ‘menu_order title’, which means products are sorted by their menu order and title.
  • order – defines the direction of the sort order. The default is ‘ASC’, which stands for ascending. If you want to display products in descending order, you should set it to ‘DESC’.
  • category – specifies the category of the products you want to display. If it’s left empty, the shortcode won’t display any products.
  • cat_operator – determines how the categories are queried. The default operator is ‘IN’, meaning it will display products that are in the specified category.

Examples and Usage

Basic example – Display products from a specific category by providing the category slug.

[product_category category="electronics"]

Advanced examples

Display only a limited number of products from a specific category. In this case, we are limiting it to 6 products.

[product_category category="electronics" limit="6"]

Display products from a specific category with a specific number of columns. Here, we are displaying the products in 3 columns.

[product_category category="electronics" columns="3"]

Display products from a specific category and order them by title in ascending order.

[product_category category="electronics" orderby="title" order="ASC"]

Display products from multiple categories by providing the category slugs separated by a comma.

[product_category category="electronics,furniture" orderby="title" order="ASC"]

These examples illustrate the flexibility and power of the product_category shortcode in Woocommerce. By manipulating the parameters, you can tailor the output to fit your specific needs.

PHP Function Code

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

Shortcode PHP function:

	public static function product_category( $atts ) {
		if ( empty( $atts['category'] ) ) {
			return '';
		}

		$atts = array_merge(
			array(
				'limit'        => '12',
				'columns'      => '4',
				'orderby'      => 'menu_order title',
				'order'        => 'ASC',
				'category'     => '',
				'cat_operator' => 'IN',
			),
			(array) $atts
		);

		$shortcode = new WC_Shortcode_Products( $atts, 'product_category' );

		return $shortcode->get_content();
	}

WooCommerce [product_categories] Shortcode

The WooCommerce Product Categories shortcode is designed to display product categories in a grid layout. It offers customization options, such as limiting the number of categories shown, sorting by name, and hiding empty categories.

Shortcode: [product_categories]

Parameters

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

  • limit – It sets the maximum number of product categories to display. If set to ‘-1’, it will display all product categories.
  • orderby – It determines the order in which the product categories are displayed. It can be set to ‘name’ to order by category name, or to any other valid WordPress orderby parameter.
  • order – It determines the direction of the ordering. ‘ASC’ for ascending order, ‘DESC’ for descending order.
  • columns – It sets the number of columns to display the product categories in.
  • hide_empty – If set to ‘1’, it hides categories that have no products. If set to ‘0’, it displays all categories, regardless of whether they have products or not.
  • parent – It sets the parent category to display its child categories. If left empty, it displays all categories.
  • ids – It is used to display specific product categories. You have to enter the IDs of the categories, separated by commas.

Examples and Usage

Basic Example – Display WooCommerce product categories using the default settings

[product_categories]

Advanced Examples

Display WooCommerce product categories ordered by name in descending order. The shortcode will display all the categories, including the ones without any products.

[product_categories orderby="name" order="DESC" hide_empty="0"]

Display a specific number of WooCommerce product categories. In this example, the shortcode will display the first 5 categories based on the specified order. The categories will be displayed in 2 columns.

[product_categories limit="5" columns="2"]

Display WooCommerce product categories that belong to a specific parent category. The parent category is specified by its ID. The shortcode will display all the categories, including the ones without any products.

[product_categories parent="123" hide_empty="0"]

Display specific WooCommerce product categories. The categories are specified by their IDs. The shortcode will display all the specified categories, including the ones without any products.

[product_categories ids="123,456,789" hide_empty="0"]

PHP Function Code

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

Shortcode PHP function:

	public static function product_categories( $atts ) {
		if ( isset( $atts['number'] ) ) {
			$atts['limit'] = $atts['number'];
		}

		$atts = shortcode_atts(
			array(
				'limit'      => '-1',
				'orderby'    => 'name',
				'order'      => 'ASC',
				'columns'    => '4',
				'hide_empty' => 1,
				'parent'     => '',
				'ids'        => '',
			),
			$atts,
			'product_categories'
		);

		$ids        = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
		$hide_empty = ( true === $atts['hide_empty'] || 'true' === $atts['hide_empty'] || 1 === $atts['hide_empty'] || '1' === $atts['hide_empty'] ) ? 1 : 0;

		// Get terms and workaround WP bug with parents/pad counts.
		$args = array(
			'orderby'    => $atts['orderby'],
			'order'      => $atts['order'],
			'hide_empty' => $hide_empty,
			'include'    => $ids,
			'pad_counts' => true,
			'child_of'   => $atts['parent'],
		);

		$product_categories = apply_filters(
			'woocommerce_product_categories',
			get_terms( 'product_cat', $args )
		);

		if ( '' !== $atts['parent'] ) {
			$product_categories = wp_list_filter(
				$product_categories,
				array(
					'parent' => $atts['parent'],
				)
			);
		}

		if ( $hide_empty ) {
			foreach ( $product_categories as $key => $category ) {
				if ( 0 === $category->count ) {
					unset( $product_categories[ $key ] );
				}
			}
		}

		$atts['limit'] = '-1' === $atts['limit'] ? null : intval( $atts['limit'] );
		if ( $atts['limit'] ) {
			$product_categories = array_slice( $product_categories, 0, $atts['limit'] );
		}

		$columns = absint( $atts['columns'] );

		wc_set_loop_prop( 'columns', $columns );
		wc_set_loop_prop( 'is_shortcode', true );

		ob_start();

		if ( $product_categories ) {
			woocommerce_product_loop_start();

			foreach ( $product_categories as $category ) {
				wc_get_template(
					'content-product_cat.php',
					array(
						'category' => $category,
					)
				);
			}

			woocommerce_product_loop_end();
		}

		wc_reset_loop();

		return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
	}

WooCommerce [add_to_cart] Shortcode

The ‘product_add_to_cart’ shortcode is a WooCommerce plugin that enables users to add products to their cart directly. It accepts various parameters like ‘id’, ‘class’, ‘quantity’, ‘sku’, ‘style’, and ‘show_price’.

Shortcode: [add_to_cart]

Parameters

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

  • id – The unique identifier of the specific product. It’s used to fetch the product data from the database.
  • class – The CSS class that will be added to the product’s HTML wrapper. This can be used to apply custom styling to the specific product.
  • quantity – The number of the product that will be added to the cart when the ‘Add to Cart’ button is clicked. By default, it is set to ‘1’.
  • sku – The Stock Keeping Unit of the product. If an ‘id’ is not provided, the ‘sku’ can be used to fetch the product data.
  • style – The inline CSS that will be applied to the product’s HTML wrapper. By default, it is set to ‘border:4px solid #ccc; padding: 12px;’
  • show_price – A boolean value that determines whether the product’s price will be displayed or not. If set to ‘true’, the price will be shown, otherwise, it will be hidden.

Examples and Usage

Basic example – The following shortcode displays the ‘Add to Cart’ button for a specific product by referencing its ID.

[product_add_to_cart id="123" /]

Advanced examples

Adding the ‘Add to Cart’ button for a product using its SKU, with a custom CSS class and style.

[product_add_to_cart sku="abc123" class="custom-class" style="border:2px solid #000; padding: 10px;" /]

Displaying the ‘Add to Cart’ button for a product by its ID, with a specified quantity, and showing the product price.

[product_add_to_cart id="123" quantity="3" show_price="true" /]

Using the shortcode to add the ‘Add to Cart’ button for a product by its SKU, without displaying the product price.

[product_add_to_cart sku="abc123" show_price="false" /]

PHP Function Code

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

Shortcode PHP function:

	public static function product_add_to_cart( $atts ) {
		global $post;

		if ( empty( $atts ) ) {
			return '';
		}

		$atts = shortcode_atts(
			array(
				'id'         => '',
				'class'      => '',
				'quantity'   => '1',
				'sku'        => '',
				'style'      => 'border:4px solid #ccc; padding: 12px;',
				'show_price' => 'true',
			),
			$atts,
			'product_add_to_cart'
		);

		if ( ! empty( $atts['id'] ) ) {
			$product_data = get_post( $atts['id'] );
		} elseif ( ! empty( $atts['sku'] ) ) {
			$product_id   = wc_get_product_id_by_sku( $atts['sku'] );
			$product_data = get_post( $product_id );
		} else {
			return '';
		}

		$product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;

		if ( ! $product ) {
			return '';
		}

		ob_start();

		echo '<p class="product woocommerce add_to_cart_inline ' . esc_attr( $atts['class'] ) . '" style="' . ( empty( $atts['style'] ) ? '' : esc_attr( $atts['style'] ) ) . '">';

		if ( wc_string_to_bool( $atts['show_price'] ) ) {
			// @codingStandardsIgnoreStart
			echo $product->get_price_html();
			// @codingStandardsIgnoreEnd
		}

		woocommerce_template_loop_add_to_cart(
			array(
				'quantity' => $atts['quantity'],
			)
		);

		echo '</p>';

		// Restore Product global in case this is shown inside a product post.
		wc_setup_product_data( $post );

		return ob_get_clean();
	}

WooCommerce [add_to_cart_url] Shortcode

The ‘Add to Cart URL’ shortcode from WooCommerce plugin generates a specific product’s URL for the ‘Add to Cart’ button. It uses either the product’s ID or SKU to identify the product. This PHP function checks if the product ID or SKU is provided in the shortcode attributes. If not, it returns an empty string. The function then retrieves the product data and checks if it’s a valid product or product variation. If not valid, it again returns an empty string. Finally, it fetches the product’s ‘Add to Cart’ URL and returns it.

Shortcode: [add_to_cart_url]

Parameters

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

  • id – The unique identifier of the product. When you use this parameter in the shortcode, it will generate the ‘add to cart’ URL for the specific product that has the given ID.
  • sku – The Stock Keeping Unit of the product. This is another unique identifier that can be used instead of the ‘id’. When you use this parameter, the shortcode will fetch the product ID corresponding to the given SKU and generate the ‘add to cart’ URL for that product.

Examples and Usage

Basic example – A simple shortcode that uses the product ID to generate the add to cart URL.

[add_to_cart_url id="99"]

Advanced examples

Generating the add to cart URL by providing the product SKU instead of the ID. This is useful when you want to use the product SKU, which may be easier to remember or more meaningful in your context.

[add_to_cart_url sku="abc123"]

Another advanced usage could be to create a link with the add to cart URL. This could be useful when you want to create custom add to cart buttons or links in your posts or pages.

<a href="[add_to_cart_url id='99']">Add to cart</a>

PHP Function Code

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

Shortcode PHP function:

	public static function product_add_to_cart_url( $atts ) {
		if ( empty( $atts ) ) {
			return '';
		}

		if ( isset( $atts['id'] ) ) {
			$product_data = get_post( $atts['id'] );
		} elseif ( isset( $atts['sku'] ) ) {
			$product_id   = wc_get_product_id_by_sku( $atts['sku'] );
			$product_data = get_post( $product_id );
		} else {
			return '';
		}

		$product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;

		if ( ! $product ) {
			return '';
		}

		$_product = wc_get_product( $product_data );

		return esc_url( $_product->add_to_cart_url() );
	}

WooCommerce [products] Shortcode

The ‘Products’ shortcode in the WooCommerce plugin allows users to list products based on specific conditions. This shortcode can be used to display products that are on sale, best selling, or top rated. The type of products to be displayed is determined by the attributes ‘on_sale’, ‘best_selling’, and ‘top_rated’. The WC_Shortcode_Products function then generates the content based on these conditions.

Shortcode: [products]

Parameters

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

  • on_sale – A parameter that allows you to display only the products that are currently on sale. If set to true, the shortcode will list all sale products.
  • best_selling – A parameter that when set to true, will list the products which are the best selling ones. It’s a great way to highlight your most popular products.
  • top_rated – This parameter is used to display products that have the highest customer ratings. If set to true, it will show the top-rated products based on customer reviews.

Examples and Usage

Basic example – Display a list of products using the ‘products’ shortcode.

[products limit="5" columns="2"]

Advanced examples

Display a list of products that are currently on sale using the ‘on_sale’ attribute.

[products limit="5" columns="2" on_sale="true"]

Showcase the best selling products using the ‘best_selling’ attribute.

[products limit="5" columns="2" best_selling="true"]

Highlight the top rated products using the ‘top_rated’ attribute.

[products limit="5" columns="2" top_rated="true"]

These examples demonstrate the flexibility of the ‘products’ shortcode in WooCommerce. By manipulating the attributes, you can customize the display of your product listings to suit your specific needs.

PHP Function Code

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

Shortcode PHP function:

	public static function products( $atts ) {
		$atts = (array) $atts;
		$type = 'products';

		// Allow list product based on specific cases.
		if ( isset( $atts['on_sale'] ) && wc_string_to_bool( $atts['on_sale'] ) ) {
			$type = 'sale_products';
		} elseif ( isset( $atts['best_selling'] ) && wc_string_to_bool( $atts['best_selling'] ) ) {
			$type = 'best_selling_products';
		} elseif ( isset( $atts['top_rated'] ) && wc_string_to_bool( $atts['top_rated'] ) ) {
			$type = 'top_rated_products';
		}

		$shortcode = new WC_Shortcode_Products( $atts, $type );

		return $shortcode->get_content();
	}

WooCommerce [recent_products] Shortcode

The ‘recent_products’ shortcode from WooCommerce is a powerful tool that allows you to display your most recent products on your website. This shortcode uses PHP to pull the 12 most recent products from your WooCommerce store, displaying them in 4 columns. It orders products by date, in descending order. You can customize it by specifying a category.

Shortcode: [recent_products]

Parameters

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

  • limit – The number of recent products to display. By default, it’s set to ’12’.
  • columns – The number of columns to divide the products into. By default, it’s set to ‘4’.
  • orderby – The criteria to sort the products by. By default, it’s set to ‘date’ which means the products will be displayed in the order they were added.
  • order – The order in which to display the products. ‘DESC’ means the most recent products will be displayed first. ‘ASC’ would display the oldest products first.
  • category – The specific category of products to display. If left blank, products from all categories will be displayed.
  • cat_operator – This determines how the category parameter is interpreted. ‘IN’ means that products from the category specified will be included. ‘NOT IN’ would exclude products from the specified category.

Examples and Usage

Basic example – A simple shortcode to display 12 most recent products in a 4-column layout.

[recent_products limit="12" columns="4"]

Advanced examples

Display 8 most recent products from a specific category in a 2-column layout. The ‘category’ parameter accepts the slug of the desired category, and the ‘cat_operator’ parameter can be used to control how the category is queried.

[recent_products limit="8" columns="2" category="electronics" cat_operator="IN"]

Display 6 most recent products, ordered by title in ascending order.

[recent_products limit="6" orderby="title" order="ASC"]

These examples show the flexibility of the ‘recent_products’ shortcode, allowing you to customize the display of your WooCommerce products to suit your specific needs.

PHP Function Code

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

Shortcode PHP function:

	public static function recent_products( $atts ) {
		$atts = array_merge(
			array(
				'limit'        => '12',
				'columns'      => '4',
				'orderby'      => 'date',
				'order'        => 'DESC',
				'category'     => '',
				'cat_operator' => 'IN',
			),
			(array) $atts
		);

		$shortcode = new WC_Shortcode_Products( $atts, 'recent_products' );

		return $shortcode->get_content();
	}

WooCommerce [sale_products] Shortcode

The “Sale Products” shortcode is a powerful tool within the Woocommerce plugin. It displays a list of products currently on sale. The PHP code for this shortcode allows you to customize its output. You can set the limit of products shown, define the number of columns, sort order, and even specify a particular category.

Shortcode: [sale_products]

Parameters

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

  • limit – defines the maximum number of sale products to be displayed. The default value is ’12’, meaning that if no limit is set, up to 12 sale products will be displayed.
  • columns – determines how many products are displayed per row. By default, it is set to ‘4’, so four products will be shown in each row.
  • orderby – sets the criteria for arranging the displayed products. ‘title’ is the default setting, which means products will be arranged alphabetically by their titles.
  • order – determines the order in which the products are displayed. ‘ASC’ means the products will be shown in ascending order. If set to ‘DESC’, products will be displayed in descending order.
  • category – allows you to specify the category of products to be displayed. If left blank, products from all categories will be shown.
  • cat_operator – sets the condition to be met by the category parameter. ‘IN’ is the default value, meaning that the products displayed will be from the categories specified in the ‘category’ parameter.

Examples and Usage

Basic example – A simple usage of the ‘sale_products’ shortcode to display up to 12 sale products ordered by title in ascending order.

[sale_products limit="12" columns="4" orderby="title" order="ASC"]

Advanced examples

Displaying 6 sale products from a specific category. The products are ordered by date in descending order, showcased in 3 columns.

[sale_products limit="6" columns="3" orderby="date" order="DESC" category="electronics"]

Displaying 10 sale products, excluding those from a specific category. The products are ordered by popularity in ascending order, showcased in 5 columns.

[sale_products limit="10" columns="5" orderby="popularity" order="ASC" category="books" cat_operator="NOT IN"]

These examples give you a sense of the flexibility and customization offered by the ‘sale_products’ shortcode. You can easily tailor the shortcode to suit the specific needs of your WooCommerce store.

PHP Function Code

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

Shortcode PHP function:

	public static function sale_products( $atts ) {
		$atts = array_merge(
			array(
				'limit'        => '12',
				'columns'      => '4',
				'orderby'      => 'title',
				'order'        => 'ASC',
				'category'     => '',
				'cat_operator' => 'IN',
			),
			(array) $atts
		);

		$shortcode = new WC_Shortcode_Products( $atts, 'sale_products' );

		return $shortcode->get_content();
	}

WooCommerce [best_selling_products] Shortcode

The ‘Best Selling Products’ shortcode displays the top 12 best-selling items on your WooCommerce site. It organizes them into 4 columns and can be filtered by category.

Shortcode: [best_selling_products]

Parameters

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

  • limit – It controls the number of best selling products to show. The default value is ’12’.
  • columns – It determines how many columns to use for displaying the products. The default is ‘4’, meaning the products will be shown in four columns.
  • category – This allows you to specify the category of products to display. By default, it’s empty, meaning products from all categories will be displayed.
  • cat_operator – This operator is used to determine how the ‘category’ parameter is interpreted. The default is ‘IN’, meaning it will display products that are in the specified category. Other possible values are ‘NOT IN’, ‘AND’.

Examples and Usage

Basic example – Use the best_selling_products shortcode to display 12 best-selling products in 4 columns without specifying a category.

[best_selling_products limit="12" columns="4" /]

Advanced examples

Display the top 10 best-selling products from a specific category. In this example, we’ll use the category ‘books’. The ‘cat_operator’ is set to ‘IN’, which means the products will be fetched from within the specified category.

[best_selling_products limit="10" columns="2" category="books" cat_operator="IN" /]

Display the top 5 best-selling products, but exclude a certain category. In this case, we’ll exclude the ‘electronics’ category by setting ‘cat_operator’ to ‘NOT IN’.

[best_selling_products limit="5" columns="5" category="electronics" cat_operator="NOT IN" /]

PHP Function Code

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

Shortcode PHP function:

	public static function best_selling_products( $atts ) {
		$atts = array_merge(
			array(
				'limit'        => '12',
				'columns'      => '4',
				'category'     => '',
				'cat_operator' => 'IN',
			),
			(array) $atts
		);

		$shortcode = new WC_Shortcode_Products( $atts, 'best_selling_products' );

		return $shortcode->get_content();
	}

WooCommerce [top_rated_products] Shortcode

The Top Rated Products shortcode is a powerful tool in WooCommerce that displays your top-rated products. This shortcode allows you to set parameters such as limit, columns, order, and category. The ‘limit’ parameter defines the number of products to display, ‘columns’ sets the number of columns, ‘orderby’ and ‘order’ determine the order in which products are displayed, and ‘category’ allows you to specify the product category.

Shortcode: [top_rated_products]

Parameters

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

  • limit – Determines the number of top-rated products to display. The default value is ’12’, which means it will display 12 top-rated products.
  • columns – Specifies the number of columns to display the products in. The default value is ‘4’, which will display the products in 4 columns.
  • orderby – Defines the parameter to sort the products by. The default value is ‘title’, which means the products will be sorted by their title.
  • order – Specifies the order in which the products are displayed. The default value is ‘ASC’, which means the products will be displayed in ascending order. If you want them to be displayed in descending order, you can change this value to ‘DESC’.
  • category – Allows you to limit the products displayed to a specific category. By default, it is empty, which means it will display products from all categories. If you want to display products from a specific category, you can specify the category name here.
  • cat_operator – Defines the operator used to test the ‘category’ parameter. The default value is ‘IN’, which means it will display products that are in the specified category. Other possible values are ‘NOT IN’, ‘AND’.

Examples and Usage

Basic example – A simple usage of the top_rated_products shortcode displays the top 12 rated products in a 4-column layout, ordered by title in ascending order.

[top_rated_products limit="12" columns="4" orderby="title" order="ASC"]

Advanced examples

Displaying the top 5 rated products in a 5-column layout, ordered by title in descending order.

[top_rated_products limit="5" columns="5" orderby="title" order="DESC"]

Displaying the top rated products from a specific category. In this example, the category is “electronics”.

[top_rated_products category="electronics"]

Displaying the top 10 rated products from multiple categories. In this example, the categories are “electronics” and “books”. The ‘cat_operator’ parameter is set to ‘AND’, meaning the products must belong to both categories.

[top_rated_products limit="10" category="electronics, books" cat_operator="AND"]

PHP Function Code

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

Shortcode PHP function:

	public static function top_rated_products( $atts ) {
		$atts = array_merge(
			array(
				'limit'        => '12',
				'columns'      => '4',
				'orderby'      => 'title',
				'order'        => 'ASC',
				'category'     => '',
				'cat_operator' => 'IN',
			),
			(array) $atts
		);

		$shortcode = new WC_Shortcode_Products( $atts, 'top_rated_products' );

		return $shortcode->get_content();
	}

WooCommerce [featured_products] Shortcode

The ‘featured_products’ shortcode from WooCommerce plugin displays a list of your store’s featured products. It allows customization such as limiting the number of products displayed, arranging them in columns, and sorting them by date or category.

Shortcode: [featured_products]

Parameters

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

  • limit – Defines the maximum number of featured products to display. The default value is ’12’.
  • columns – Specifies the number of columns to use for displaying the featured products. The default value is ‘4’.
  • orderby – Determines the order in which the featured products are displayed. The default value is ‘date’, which means the products are sorted by the date they were added.
  • order – Sets the sorting order of the featured products. ‘DESC’ means the products are sorted in descending order, from newest to oldest.
  • category – Filters the featured products by a specific category. If left blank, it will display featured products from all categories.
  • cat_operator – Determines how multiple categories are handled. ‘IN’ means that it will display featured products that are in any of the specified categories.
  • visibility – This is set to ‘featured’, which means only the products marked as featured will be displayed.

Examples and Usage

Basic example – Display 12 featured products in 4 columns, ordered by date in descending order.

[featured_products limit="12" columns="4" orderby="date" order="DESC"]

Advanced examples

Display 6 featured products from a specific category, in 3 columns, ordered by date in ascending order.

[featured_products limit="6" columns="3" orderby="date" order="ASC" category="electronics"]

Display 10 featured products from multiple categories, in 5 columns, ordered by title in ascending order. The ‘cat_operator’ parameter is set to ‘AND’, which means the products must belong to all specified categories.

[featured_products limit="10" columns="5" orderby="title" order="ASC" category="electronics,clothing" cat_operator="AND"]

Display all featured products in 2 columns, ordered by date in descending order. The ‘limit’ parameter is set to ‘-1’, which means there is no limit to the number of products displayed.

[featured_products limit="-1" columns="2" orderby="date" order="DESC"]

PHP Function Code

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

Shortcode PHP function:

	public static function featured_products( $atts ) {
		$atts = array_merge(
			array(
				'limit'        => '12',
				'columns'      => '4',
				'orderby'      => 'date',
				'order'        => 'DESC',
				'category'     => '',
				'cat_operator' => 'IN',
			),
			(array) $atts
		);

		$atts['visibility'] = 'featured';

		$shortcode = new WC_Shortcode_Products( $atts, 'featured_products' );

		return $shortcode->get_content();
	}

WooCommerce [product_attribute] Shortcode

The WooCommerce ‘product_attribute’ shortcode is designed to display products based on specific attributes. The attributes can be customized including limit, columns, order, and the specific attribute. If no attribute is specified, the shortcode will return an empty string.

Shortcode: [product_attribute]

Parameters

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

  • limit – This sets the maximum number of products that will be displayed by the shortcode. The default value is 12.
  • columns – This determines how many columns will be used to display the products. The default value is 4.
  • orderby – This parameter is used to sort the products. By default, products are sorted by title.
  • order – This parameter sets the direction of the sort. The default value is ‘ASC’, meaning products are sorted in ascending order. To sort in descending order, use ‘DESC’.
  • attribute – This value specifies the product attribute to display. If left empty, the shortcode will return an empty string.
  • terms – This value is used to specify the terms of the product attribute to be displayed. If left empty, all terms of the specified attribute will be displayed.

Examples and Usage

Basic example – A simple way to use the ‘product_attribute’ shortcode to display products with a specific attribute. In this case, we are showing up to 12 products in 4 columns, ordered by the product title in ascending order.

[product_attribute attribute='color' terms='blue' limit='12' columns='4' orderby='title' order='ASC']

Advanced examples

1. In this example, we are using the ‘product_attribute’ shortcode to display products with multiple attributes. We are showing up to 10 products in 5 columns, ordered by the product date in descending order.

[product_attribute attribute='color,size' terms='blue,large' limit='10' columns='5' orderby='date' order='DESC']

2. Here, we are using the ‘product_attribute’ shortcode to display products with a specific attribute but without specifying the term. This will display all products that have the ‘material’ attribute, regardless of the term value. We are showing up to 6 products in 3 columns, ordered by the product title in ascending order.

[product_attribute attribute='material' limit='6' columns='3' orderby='title' order='ASC']

PHP Function Code

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

Shortcode PHP function:

	public static function product_attribute( $atts ) {
		$atts = array_merge(
			array(
				'limit'     => '12',
				'columns'   => '4',
				'orderby'   => 'title',
				'order'     => 'ASC',
				'attribute' => '',
				'terms'     => '',
			),
			(array) $atts
		);

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

		$shortcode = new WC_Shortcode_Products( $atts, 'product_attribute' );

		return $shortcode->get_content();
	}

WooCommerce [related_products] Shortcode

The Related Products shortcode in WooCommerce allows you to display related products on your site. The shortcode has parameters for limit, columns, and orderby. The ‘limit’ parameter controls the number of related products to display. The ‘columns’ parameter sets the number of columns in the display grid. The ‘orderby’ parameter determines the order in which products are displayed.

Shortcode: [related_products]

Parameters

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

  • limit – Determines the maximum number of related products to be displayed. By default, it is set to ‘4’. You can change it to any number as per your requirement.
  • columns – Controls how many columns will be used to display the related products. It’s set to ‘4’ by default, meaning related products will be shown in 4 columns. You can adjust this number to fit your layout.
  • orderby – This parameter decides the order in which the related products will be displayed. It’s set to ‘rand’ by default, which means the related products will be displayed in a random order. You can change this to ‘date’, ‘title’, ‘name’, ‘id’, etc. to order the products based on date of publishing, title, name, id, and more.
  • posts_per_page – This parameter is not directly used in the shortcode but it is set internally by the ‘limit’ parameter. It determines the number of related products to display per page. It is set to the absolute integer value of the ‘limit’ parameter.

Examples and Usage

Basic example – This shortcode displays related products with the default parameters. By default, it shows 4 related products in 4 columns, ordered randomly.

[related_products /]

Advanced examples

Example 1: This shortcode will display 6 related products in 3 columns. The products are ordered randomly.

[related_products limit="6" columns="3" /]

Example 2: This shortcode will display 2 related products in 2 columns. The products are ordered by date.

[related_products limit="2" columns="2" orderby="date" /]

These shortcodes allow you to customize the display of related products on your WooCommerce site. By adjusting the limit, columns, and orderby parameters, you can tailor the related products section to suit your site’s design and your customers’ needs.

PHP Function Code

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

Shortcode PHP function:

	public static function related_products( $atts ) {
		if ( isset( $atts['per_page'] ) ) {
			$atts['limit'] = $atts['per_page'];
		}

		// @codingStandardsIgnoreStart
		$atts = shortcode_atts( array(
			'limit'    => '4',
			'columns'  => '4',
			'orderby'  => 'rand',
		), $atts, 'related_products' );
		// @codingStandardsIgnoreEnd

		ob_start();

		// Rename arg.
		$atts['posts_per_page'] = absint( $atts['limit'] );

		woocommerce_related_products( $atts );

		return ob_get_clean();
	}

WooCommerce [shop_messages] Shortcode

The WooCommerce ‘shop_messages’ shortcode is a handy tool for displaying user notices. It prints messages like errors, success, or notices on your shop page. The PHP code for this shortcode checks if the ‘wc_print_notices’ function exists. If it doesn’t, it returns an empty string. If it does, it returns a div containing the WooCommerce notices.

Shortcode: [shop_messages]

Examples and Usage

Basic example – The following shortcode is used to display shop messages. If there are any notices, they will be shown in a div with the class “woocommerce”.

[shop_messages]

Advanced examples

Unfortunately, the ‘shop_messages’ shortcode does not accept any parameters. It’s designed to simply return any WooCommerce notices in a div with the class “woocommerce”. This makes it a very straightforward shortcode with no advanced examples possible. The simplicity of this shortcode makes it easy to use, as you don’t have to worry about configuring any parameters – just place it where you want your shop messages to appear.

However, it’s possible to manipulate the output by adding some custom CSS to your theme. For example, you could style the div that the messages appear in, or change the appearance of the messages themselves. Here’s an example of how you might do this:


.woocommerce {
    background-color: #f9f9f9;
    padding: 20px;
    border-radius: 5px;
}

.woocommerce .woocommerce-error {
    color: #a00;
    font-weight: bold;
}

Please note that the above CSS should be added to your theme’s style.css file, or in the custom CSS option if your theme provides one.

PHP Function Code

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

Shortcode PHP function:

	public static function shop_messages() {
		if ( ! function_exists( 'wc_print_notices' ) ) {
			return '';
		}
		return '<div class="woocommerce">' . wc_print_notices( true ) . '</div>';
	}

WooCommerce [woocommerce_order_tracking] Shortcode

The WooCommerce Order Tracking shortcode is a useful tool that allows customers to track their orders. The shortcode works by returning the status of a specific order. It uses the shortcode wrapper to output the order status. This helps enhance customer experience by providing real-time order updates.

Shortcode: [woocommerce_order_tracking]

Examples and Usage

Basic example – The shortcode for order tracking in WooCommerce is used to display the order tracking form on a page. The basic usage of this shortcode does not require any parameters or attributes.

[woocommerce_order_tracking]

Advanced examples

While the ‘woocommerce_order_tracking’ shortcode does not have explicit parameters, you can customize its behavior by manipulating the global WooCommerce query. For instance, you can pre-fill the order tracking form with an order ID and email. This is a more advanced use case and requires knowledge of PHP and WordPress hooks.


add_action( 'wp', 'prefill_order_tracking_form' );
function prefill_order_tracking_form() {
    if ( is_page( 'track-order' ) ) {
        global $wp;
        $wp->query_vars['order_id'] = '1234';
        $wp->query_vars['order_email'] = 'customer@example.com';
    }
}

In the above example, replace ‘track-order’ with the slug of your order tracking page, ‘1234’ with the order ID, and ‘customer@example.com’ with the customer’s email address. This code should be added to your theme’s functions.php file or a custom plugin.

Note: This is an advanced example and should only be used by experienced developers. Always backup your website before making changes to the code.

PHP Function Code

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

Shortcode PHP function:

	public static function order_tracking( $atts ) {
		return self::shortcode_wrapper( array( 'WC_Shortcode_Order_Tracking', 'output' ), $atts );
	}

WooCommerce [woocommerce_cart] Shortcode

The WooCommerce Cart shortcode is a function that displays the customer’s shopping cart content. When implemented, it checks if the cart is null, and if not, it outputs the cart’s content.

Shortcode: [woocommerce_cart]

Examples and Usage

Basic example – A simple usage of the woocommerce_cart shortcode to display the cart on your page.

[woocommerce_cart /]

Advanced examples

Unfortunately, the woocommerce_cart shortcode does not accept any additional parameters/attributes. The function of this shortcode is to display the cart, and it performs this function without the need for any customization or additional parameters. However, you can customize the appearance and functionality of the cart by modifying the WooCommerce templates or using CSS.

For example, you can add a custom CSS class to the div that contains the shortcode, and then use that class to apply custom styles. Here’s how you can do it:

<div class="my-custom-cart">[woocommerce_cart /]</div>

Then, in your CSS file, you can add styles for the .my-custom-cart class. This will allow you to customize the appearance of the cart as per your needs.

Remember, the woocommerce_cart shortcode is a powerful tool that allows you to display the cart anywhere on your site. While it doesn’t accept additional parameters, you can still customize it using CSS or by modifying the WooCommerce templates.

PHP Function Code

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

Shortcode PHP function:

	public static function cart() {
		return is_null( WC()->cart ) ? '' : self::shortcode_wrapper( array( 'WC_Shortcode_Cart', 'output' ) );
	}

WooCommerce [woocommerce_checkout] Shortcode

The WooCommerce Checkout shortcode is used to display the checkout page to the users. It’s a crucial part of any eCommerce site as it facilitates the final purchase process. The PHP code for this shortcode invokes the ‘output’ function from the ‘WC_Shortcode_Checkout’ class, which generates the checkout page. This allows for seamless integration within your WordPress site.

Shortcode: [woocommerce_checkout]

Examples and Usage

Basic example – A straightforward usage of the woocommerce_checkout shortcode. This shortcode will display the checkout page of your WooCommerce store.

[woocommerce_checkout]

Advanced examples

1. Using the shortcode with attributes to customize the checkout page. The attributes can be used to control the display of different elements on the checkout page. For example, the ‘order_review’ attribute can be used to control the display of the order review section.

[woocommerce_checkout order_review="true"]

2. Another example of using the shortcode with multiple attributes. In this case, the ‘order_button_text’ attribute is used to customize the text of the order button, and the ‘show_login_reminder’ attribute is used to control the display of the login reminder.

[woocommerce_checkout order_button_text="Place your order now" show_login_reminder="false"]

Please note that the actual usage of the attributes can depend on the configuration of your WooCommerce store and the specific version of the WooCommerce plugin you are using. Always refer to the official WooCommerce documentation for the most accurate and up-to-date information.

PHP Function Code

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

Shortcode PHP function:

	public static function checkout( $atts ) {
		return self::shortcode_wrapper( array( 'WC_Shortcode_Checkout', 'output' ), $atts );
	}

WooCommerce [woocommerce_my_account] Shortcode

The Woocommerce My Account shortcode is a handy tool that displays user account details. It’s used to customize the My Account page. The PHP code shows that this shortcode wraps around the ‘WC_Shortcode_My_Account’ function, which outputs user account details.

Shortcode: [woocommerce_my_account]

Examples and Usage

Basic example – The shortcode “woocommerce_my_account” allows you to display the My Account page of WooCommerce on any part of your website.

[woocommerce_my_account]

Advanced examples

While the “woocommerce_my_account” shortcode doesn’t inherently support additional parameters, you can use it in combination with other shortcodes or functions to modify its behavior or display. For example:

Using the shortcode with a conditional statement – This example demonstrates how to use the shortcode within a conditional statement. If the user is logged in, the My Account page will be displayed. If not, a login form will be displayed instead.


if ( is_user_logged_in() ) {
    echo do_shortcode( '[woocommerce_my_account]' );
} else {
    echo do_shortcode( '[woocommerce_login_form]' );
}

Using the shortcode with a custom function – In this example, a custom function is used to modify the My Account page. The function adds a welcome message at the top of the page.


function custom_my_account_message() {
    echo '

Welcome to your account page. Here you can manage your orders and details.

'; } add_action( 'woocommerce_account_content', 'custom_my_account_message' ); echo do_shortcode( '[woocommerce_my_account]' );

PHP Function Code

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

Shortcode PHP function:

	public static function my_account( $atts ) {
		return self::shortcode_wrapper( array( 'WC_Shortcode_My_Account', 'output' ), $atts );
	}

Conclusion

Now that you’ve learned how to embed the WooCommerce 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 *