Shortcodes included with WooCommerce

WooCommerce comes with several shortcodes that can be used to insert content inside posts and pages.

Page Shortcodes ↑ Back to top

  • [woocommerce_cart] – shows the cart page
  • [woocommerce_checkout] – shows the checkout page
  • [woocommerce_my_account] – shows the user account page
  • [woocommerce_order_tracking] – shows the order tracking form

In most cases, these shortcodes will be added to pages automatically via our onboarding wizard and do not need to be used manually.

Cart ↑ Back to top

Used on the cart page, the cart shortcode displays cart content and interface for coupon codes and other cart bits and pieces.

Args: none

[woocommerce_cart]

Checkout ↑ Back to top

Used on the checkout page, the checkout shortcode displays the checkout process.

Args: none

[woocommerce_checkout]

My Account ↑ Back to top

Shows the ‘my account’ section where the customer can view past orders and update their information. You can specify the number of orders to show. By default, it’s set to 15 (use -1 to display all orders.)

Args:

array(
     'current_user' => ''
 )
[woocommerce_my_account]
Current user argument is automatically set using get_user_by( ‘id’, get_current_user_id() ).

Order Tracking Form ↑ Back to top

Lets a user see the status of an order by entering their order details.

Args: none

[woocommerce_order_tracking]

Products ↑ Back to top

The products shortcode is one of our most robust shortcodes, which can replace various other strings used in earlier versions of WooCommerce. For WooCommerce version 3.1 and lower, please use this document instead.
The [products] shortcode allows you to display products by post ID, SKU, categories, attributes, and more, replacing the need for multiples shortcodes like [product_category],  [featured_products], [sale_products][best_selling_products][recent_products][product_attribute], and [top_rated_products], which are needed in versions of WooCommerce below 3.2. Review the examples below.

Available Product Attributes ↑ Back to top

The following attributes are available to use in conjunction with the [products] shortcode. They have been split into sections for primary function for ease of navigation, with examples below.

Display Product Attributes

  • limit – The number of products to display. Defaults to display all (-1).
  • columns – The number of columns to display. Defaults to 4.
  • orderby – Sorts the products displayed by the entered option. One or more options can be passed by adding both slugs with a space between them. Available options are:
    • date – The date the product was published.
    • id – The post ID of the product.
    • menu_order – The Menu Order, if set (lower numbers display first).
    • popularity – The number of purchases.
    • rand – Randomly order the products on page load (may not work with sites that use caching, as it could save a specific order).
    • rating – The average product rating.
    • title – The product title. This is the default orderby mode.
  • skus – Comma separated list of product SKUs.
  • category – Comma separated list of category slugs.
  • order – States whether the product ordering is ascending (ASC) or descending (DESC), using the method set in orderby. Defaults to ASC.
  • class – Adds an HTML wrapper class so you can modify the specific output with custom CSS.
  • on_sale – Retrieve on sale products. Not to be used in conjunction with best_sellingor top_rated.
  • best_selling – Retrieve best selling products. Not to be used in conjunction with on_sale or top_rated.
  • top_rated – Retrieve top rated products. Not to be used in conjunction with on_saleor best_selling.

Content Product Attributes

  • attribute – Retrieves products using the specified attribute slug.
  • terms – Comma separated list of attribute terms to be used with attribute.
  • terms_operator – Operator to compare attribute terms. Available options are:
    • AND – Will display products from all of the chosen attributes.
    • IN – Will display products with the chosen attribute. This is the default terms_operator value.
    • NOT IN – Will display products that are not in the chosen attributes.
  • visibility – Will display products based on the selected visibility. Available options are:
    • visible – Products visibile on shop and search results. This is the default visibility option.
    • catalog – Products visible on the shop only, but not search results.
    • search – Products visible in search results only, but not on the shop.
    • hidden – Products that are hidden from both shop and search, accessible only by direct URL.
    • featured – Products that are marked as Featured Products.
  • category – Retries products using the specified category slug.
  • cat_operator – Operator to compare category terms. Available options are:
    • AND – Will display products that belong in all of the chosen categories.
    • IN – Will display products within the chosen category. This is the default cat_operator value.
    • NOT IN – Will display products that are not in the chosen category.
  • ids – Will display products based off of a comma separated list of Post IDs.
  • skus – Will display products based off of a comma separated list of SKUs.

*If the product is not showing, make sure it is not set to Hidden in the Catalog Visibility.

To find the Product ID, go to the Products screen, hover over the product and the ID appears as shown below.

WooCommerce-Product-ID

Special Product Attributes

These attributes cannot be used with the “Content Attributes” listed above, as they will likely cause a conflict and not display. You should only use one of the following special attributes.

  • best_selling – Will display your best selling products. Must be set to true.
  • on_sale – Will display your on-sale products. Must be set to true.

Product Category shortcodes

These two shortcodes will display your product categories on any page.

  • [product_category] – Will display a specified product category.
  • [product_categories] – Will display all your product categories.

Example Product Scenarios ↑ Back to top

In the following scenarios, we’ll use an example clothing store.

Scenario 1 – Random Sale Items

I want to display four random on sale products.

[products limit="4" columns="4" orderby="popularity" class="quick-sale" on_sale="true" ]

This shortcode explicity states four products with four columns (which will be one row), showing the most popular on-sale items. It also adds a CSS class quick-sale, which I can modify in my theme.

WooCommerce Shortcode - Sale Products

Scenario 2 – Featured Products

I want to display my featured products, two per row, with a maximum of four items.

[products limit="4" columns="2" visibility="featured" ]

This shortcode says up to four products will load in two columns, and that they must be featured. Although not explicitly stated, it uses the defaults such as sorting by title (A to Z).

WooCommerce Shortcode - Featured Products

Scenario 3 – Best Selling Products

I want to display my three top best selling products in one row.

[products limit="3" columns="3" best_selling="true" ]

WooCommerce Shortcode - Best Selling Products

Scenario 4 – Specific Categories

I only want to display hoodies and shirts, but not accessories. I’ll use two rows of four.

[products limit="8" columns="4" category="hoodies, tshirts" cat_operator="AND"]

WooCommerce Shortcode - Products by Category

Alternatively, I only want to display products not in those categories. All I need to change is the cat_operator to NOT IN.

[products limit="8" columns="4" category="hoodies, tshirts" cat_operator="NOT IN"]

Note that even though the limit is set to 8, there are only four products that fit that criteria, so four products are displayed.

WooCommerce Shortcode - Products by Category

Scenario 5 – Attribute Display

Each of the clothing items has an attribute, either “Spring/Summer” or “Fall/Winter” depending on the appropriate season, with some accessories having both since they can be worn all year. In this example, I want three products per row, displaying all of the “Spring/Summer” items. That attribute slug is season, and the attributes are warm and cold. I also want them sorted from the newest products to the oldest.

[products columns="3" attribute="season" terms="warm" orderby="date"]

WooCommerce Shortcode - Products by Attribute

Alternatively, if I wanted to display exclusively cold weather products, I could add NOT IN as my terms_operator:

[products columns="3" attribute="season" terms="warm" terms_operator="NOT IN"]

WooCommerce Shortcode - Products by Attribute
Note that by using NOT IN, I exclude products that are both in “Spring/Summer” and “Fall/Winter”. If I wanted to show all cold-weather appropriate gear including these shared accessories, I would change the term from warm to cold.

Sorting Products by Custom Meta Fields ↑ Back to top

When using the Products shortcode, you can choose to order products by the pre-defined values above. You can also sort products by custom meta fields using the code below (in this example we order product by price):

add_filter( 'woocommerce_shortcode_products_query', 'woocommerce_shortcode_products_orderby' );

function woocommerce_shortcode_products_orderby( $args ) {

    $standard_array = array('menu_order','title','date','rand','id');

    if( isset( $args['orderby'] ) && !in_array( $args['orderby'], $standard_array ) ) {
        $args['meta_key'] = $args['orderby'];
        $args['orderby']  = 'meta_value_num'; 
    }

    return $args;
}

You need to place this snippet in functions.php in your theme folder and then customize by editing the meta_key.

Note: We are unable to provide support for customizations under our Support Policy. If you are unfamiliar with code/templates and resolving potential conflicts, you can contact a WooExpert.

Product Page ↑ Back to top

Show a full single product page by ID or SKU.

[product_page id="99"]
[product_page sku="FOO"]

Related Products ↑ Back to top

List related products.

Args:

array(
     'limit' => '12',
     'columns' => '4',
     'orderby' => 'title'
 )

[related_products limit="12"]

‘limit’ Argument ↑ Back to top

Note: the ‘limit’ shortcode argument will determine how many products are shown on a page. This will not add pagination to the shortcode.

Add to Cart ↑ Back to top

Show the price and add to cart button of a single product by ID.

Args:

array(
      'id' => '99',
      'style' => 'border:4px solid #ccc; padding: 12px;',
      'sku' => 'FOO'
      'style' => 'TRUE'
      'class' => 'CSS-CLASS'
 )
[add_to_cart id="99"]

Add to Cart URL ↑ Back to top

Echo the URL on the add to cart button of a single product by ID.

Args:

array(
      'id' => '99',
      'sku' => 'FOO'
 )
[add_to_cart_url id="99"]

Display WooCommerce notifications on non-WooCommerce pages ↑ Back to top

[shop_messages] allows you to show WooCommerce notifications (like, ‘The product has been added to cart’) on non-WooCommerce pages. Helpful when you use other shortcodes, like [add_to_cart], and would like the users to get some feedback on their actions.

Troubleshooting Shortcodes ↑ Back to top

If you correctly pasted your shortcodes and the display looks incorrect, make sure you did not embed the shortcode between <pre> tags. This is a common issue. To remove these tags, edit the page, and click the Text tab:

Remove Pre Tags from Shortcode

WooCommerce - the most customizable eCommerce platform for building your online business.

Back to the top