Shortcodes included with WooCommerce

Shortcodes included with WooCommerce

ABOUT

The Coupon Shortcodes extension allows you to render coupon information and show content based on the validity of coupons.

Customers can be motivated to proceed with their purchase, offering them to use specific coupons when the contents in the cart qualify for it, or by offering them to purchase additional items so they can use a coupon.

Conditional Shortcodes

It provides the following conditional shortcodes that allow enclosing content which is shown if coupons are applied, valid or not valid.

  • [coupon_is_active]
  • [coupon_is_not_active]
  • [coupon_is_applied]
  • [coupon_is_not_applied]
  • [coupon_is_valid]
  • [coupon_is_not_valid]

Coupon Info Shortcodes

It also provides shortcodes that allow to render the coupon code, its description and an automatic description of the discount:

  • [coupon_code] – renders a coupon code
  • [coupon_description]  – renders a coupon’s description
  • [coupon_discount] – renders information about the discount of a coupon
  • [coupon_show] – renders coupon code, discount info and description together

A coupon enumerator shortcode allows to list all or a set of coupons, to show their code, description or discount information:

  • [coupon_enumerate]

Documentation

Please refer to the plugin’s documentation for detailed descriptions.

Examples

Show a text when a coupon can be used (active) – this is useful to show promotional info while coupons can be used, as active means that the coupon has not reached its expiration date nor exceeded its usage limits:

[coupon_is_active code="eastereggs"]
Happy Easter!
Use the coupon code [coupon_code] to hop away with a great discount : [coupon_discount]
[/coupon_is_active]

This is an example of a text shown when a promotion is over:

[coupon_is_not_active code="specialdiscount"]
Our special discount sale has ended. Come back often to see more!
[/coupon_is_not_active]

Showing a coupon when the cart contents qualify for a coupon to be applied:

[coupon_is_valid code="superdiscount"]
You qualify for a discount!
Use the coupon code [coupon_code] to take advantage of this great discount : [coupon_discount]
[/coupon_is_valid]

Showing a coupon that is not valid for the current cart and motivating to add items:

[coupon_is_not_valid code="25off"]
If you purchase 5 Widgets, you can use the coupon [coupon_code] to get 25% off your purchase!
[/coupon_is_not_valid]

Show information about three random coupons, including the coupon code, its description and discount info together on each entry:

[coupon_enumerate code="*" orderby="rand" number="3"]
[coupon_show show="code,description,discount"]
[/coupon_enumerate]

Show a single random coupon code:

[coupon_enumerate code="*" orderby="rand" number="1"]
[coupon_code]
[/coupon_enumerate]

SETUP

WooCommerce comes with several shortcodes that can be used to insert content inside posts and pages.
Our WooCommerce blocks are now the easiest and most flexible way to display your products on posts and pages on your WooCommerce site. Read more about all the available WooCommerce blocks here.

How to use shortcodes

Where to use

Shortcodes can be used on pages and posts in WordPress. If you are using the block editor, there is a shortcode block you can use to paste the shortcode in.


If you are using the classic editor, you can paste the shortcode on the page or post.

Args (or Arguments)

Several of the shortcodes below will mention “Args”. These are ways to make the shortcode more specific. For example, by adding id="99" to the [add_to_cart] shortcode, it will create an add-to-cart button for the product with ID 99.

Page Shortcodes

WooCommerce cannot function properly without the first three shortcodes being somewhere on your site.

Note: You can now test the new cart and checkout blocks that are available in the WooCommerce Blocks plugin!

[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

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

Used on the checkout page, the checkout shortcode displays the checkout process.
Args: none
[woocommerce_cart]

My Account

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() ).
Current user argument is automatically set using get_user_by( 'id', get_current_user_id() ).

Order Tracking Form

Lets a user see the status of an order by entering their order details.
Args: none
[woocommerce_order_tracking]

Products

Note: Since version 3.6, WooCommerce Core includes several product blocks. These are easier to configure than shortcodes, so if you are using the WordPress block editor, you may want to read more about WooCommerce Blocks first.


The [products] shortcode is one of our most robust shortcodes, which can replace various other strings used in earlier versions of WooCommerce.

The [products] shortcode allows you to display products by post ID, SKU, categories, attributes, with support for pagination, random sorting, and product tags, replacing the need for multiples shortcodes such as  [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

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
  1. limit – The number of products to display. Defaults to and -1 (display all)  when listing products, and -1 (display all) for categories.
  2. columns – The number of columns to display. Defaults to 4.
  3. paginate – Toggles pagination on. Use in conjunction with limit. Defaults to false set to true to paginate .
  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:
    1. date – The date the product was published.
    2. id – The post ID of the product.
    3. menu_order – The Menu Order, if set (lower numbers display first).
    4. popularity – The number of purchases.
    5. rand – Randomly order the products on page load (may not work with sites that use caching, as it could save a specific order).
    6. rating – The average product rating.
    7. title – The product title. This is the default orderby mode.
  5. skus – Comma-separated list of product SKUs.
  6. category – Comma-separated list of category slugs.
  7. tag – Comma-separated list of tag slugs.
  8. order – States whether the product order is ascending (ASC) or descending (DESC), using the method set in orderby. Defaults to ASC.
  9. class – Adds an HTML wrapper class so you can modify the specific output with custom CSS.
  10. on_sale – Retrieve on sale products. Not to be used in conjunction with best_sellingor top_rated.
  11. best_selling – Retrieve the best selling products. Not to be used in conjunction with on_sale or top_rated.
  12. top_rated – Retrieve top-rated products. Not to be used in conjunction with on_saleor best_selling.

Content Product Attributes
  1. attribute – Retrieves products using the specified attribute slug.
  2. terms – Comma-separated list of attribute terms to be used with attribution.
  3. terms_operator – Operator to compare attribute terms. Available options are:
    1. AND – Will display products from all of the chosen attributes.
    2. IN – Will display products with the chosen attribute. This is the default terms_operator value.
    3. NOT IN – Will display products that are not in the chosen attributes.
  4. tag_operator – Operator to compare tags. Available options are:
    1. AND – Will display products from all of the chosen tags.
    2. IN – Will display products with the chosen tags. This is the default tag_operator value.
    3. NOT IN – Will display products that are not in the chosen tags.
  5. visibility – Will display products based on the selected visibility. Available options are:
    1. visible – Products visible on shop and search results. This is the default visibility option.
    2. catalog – Products visible on the shop only, but not search results.
    3. search – Products visible in search results only, but not on the shop.
    4. hidden – Products that are hidden from both shop and search, accessible only by direct URL.
    5. featured – Products that are marked as Featured Products.
  6. category – Retrieves products using the specified category slug.
  7. tag – Retrieves products using the specified tag slug.
  8. cat_operator – Operator to compare category terms. Available options are:
    1. AND – Will display products that belong in all of the chosen categories.
    2. IN – Will display products within the chosen category. This is the default cat_operator value.
    3. NOT IN – Will display products that are not in the chosen category.
  9. ids – Will display products based on a comma-separated list of Post IDs.
  10. SKUs – Will display products based on 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.

Finding the WooCommerce product ID by hovering over a product

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.
  1. best_selling – Will display your best selling products. Must be set to true.
  2. on_sale – Will display your on-sale products. Must be set to true.

Examples of Product Scenarios

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 explicitly 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 – Newest Products
I want to display the newest products first – four products across one row. To accomplish this, we’ll use the Post ID (which is generated when the product page is created), along with the order and orderby command. Since you can’t see the Post ID from the frontend, the ID#s have been superimposed over the images.
[products limit="4" columns="4" orderby="id" order="DESC" visibility="visible"]
WooCommerce Shortcodes - Newest

Scenario 5 – 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 6 – 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.

Scenario 7 – Show Only Products With tag “hoodie”
[products tag="hoodie"]


Sorting Products by Custom Meta Fields


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

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 products 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 customise it by editing the meta_key.

Product Category

These two shortcodes will display your product categories on any page.
  1. [product_category] – Will display products in a specified product category.
  2. [product_categories] – Will display all your product categories.

Available Product Category attributes

  1. ids – Specify specific category ids to be listed. To be used in [product_categories]
  2. category – Can be either the category id, name, or slug. To be used in [product_category]
  3. limit – The number of categories to display
  4. columns – The number of columns to display. Defaults to 4
  5. hide_empty – The default is “1” which will hide empty categories. Set to “0” to show empty categories
  6. parent – Set to a specific category ID if you would like to display all the child categories. Alternatively, set to “0” (like in the example below) to show only the top-level categories.
  7. orderby – The default is to order by “name”, which can be set to “id”, “slug”, or “menu_order”. If you want to order by the ids you specified then you can use orderby="include"
  8. order – States whether the category order is ascending (ASC) or descending (DESC), using the method set in orderby. Defaults to ASC.

Examples of Product Category Scenarios

Scenario 8 – Show Top Level Categories Only
Imagine you only wanted to show top-level categories on a page and exclude the subcategories, well it’s possible with the following shortcode.
[product_categories number="0" parent="0"]


Product Page

Show a full single product page by ID or SKU.
[product_page id="99"]
[product_page sku=”FOO”]

List related products.
Args:
array(
     'limit' => '12',
     'columns' => '4',
     'orderby' => 'title'
 )

limit Argument

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

Show the price and add to the cart button of a single product by ID.
Args:
array(
      'id' => '99',
      'style' => 'border:4px solid #ccc; padding: 12px;',
      'sku' => 'FOO'
      'show_price' => 'TRUE'
      'class' => 'CSS-CLASS'
      'quantity' => '1';
 )
[add_to_cart id="99"]

Add to Cart URL

Display 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 pages that are not WooCommerce

[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

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
Another common problem is that straight quotation marks (") are displayed as curly quotation marks (“). For the shortcodes to work correctly, you need straight quotation marks.

Variation Product SKU Not Shown

In regards to the use of SKU shortcode like [products skus="sku-name"], the variation product SKU isn’t intended to be displayed by itself, as opposed to the parent variable product SKU. Therefore it is expected that if we use an SKU from Product data > Variable product > Variations > Variation name > SKU, it will not get displayed.

However, if we use an SKU from the parent variable product: Product data > Variable product > Inventory > SKU, it will get displayed.


    • Related Articles

    • Groups WooCommerce - User Guide

      Setup To sell memberships with WooCommerce requires the following extensions besides WooCommerce itself: Groups – Groups is free and designed as an efficient, powerful and flexible solution for group-oriented memberships and content access control. ...
    • WooCommerce Analytics

      WooCommerce Analytics is a new reporting and data analysis tool to help you manage your WooCommerce Store. The suite introduces nine new reports along with advanced filtering and segmenting tools, the ability to download all data to CSV, and a ...
    • Basic Woocommerce - User Guide

      For pricing and how to add to your website see at the bottom of the page. Adding and Managing Products Adding a product Before adding your first product, let’s get familiar with how product categories, tags, and attributes work. Product Categories ...
    • Woocommerce Emails

      Email Settings Options available to edit email settings and templates. Go to: WooCommerce > Settings > Emails. Email notifications For each WooCommerce email listed, it’s possible to configure your settings (optional). More detailed instructions ...
    • Woocommerce Refunds

      Summary In WooCommerce itself you can process refunds in two ways: Automatic: Refunds change the WooCommerce order status and also reverse the charge so your customer gets their money. Manual: Refunds change the WooCommerce order status, but you need ...