Primary

WooCommerce Coupon Shortcodes

This is the documentation page for the WooCommerce Coupon Shortcodes plugin.

Installation

You can install the plugin directly from within your WordPress dashboard under Plugins > Add New. Search for WooCommerce Coupon Shortcodes by itthinx, install and activate the plugin.

Shortcodes

The plugin provides a set of shortcodes intended to display information about coupons. There are two classes of shortcodes, conditional shortcodes which display content they enclose based on the validity of one or more coupons, coupon information shortcodes which are used to display the coupon code, description or a discount info derived from the coupon settings.

Each shortcode is described below. The shortcodes allow to specify one or more coupons, note that in some cases it can make sense to specify several codes, but in many cases you would want to indicate only one code.

Please make sure to use the correct syntax in each case.

Conditionals and Enumeration

The following shortcodes are used to show information conditionally.

[coupon_is_active]

A coupon is considered active while it has not expired and its usage limits have not been exhausted. The shortcode reveals the content it encloses when the condition evaluates favorably.

Attributes

  • code : (required) One or more coupon codes, separated by comma.
  • op : (optional) The operation applied to evaluate the coupons, and (default) as well as or are accepted. Indicating  op="and" requires all codes to be active, op="or" requires at least one to be active.

Example:

[coupon_is_active code="easterbunny"]Grab it all at a discount! Use the coupon code [coupon_code] before this promotion hops away![/coupon_is_active]

Please note that this shortcode requires WooCommerce 3.2 or higher.

[coupon_is_not_active]

This shortcode reveals the content it encloses when the code(s) are not considered active.

Attributes

  • code : (required) One or more coupon codes, separated by comma.
  • op : (optional) The operation applied to evaluate the coupons, and (default) as well as or are accepted. When using op="and" it is necessary for all codes to be inactive to show the enclosed content, op="or" will do so with any code being inactive.

Example:

[coupon_is_not_active code="easterbunny"]Our Easter Promo is over![/coupon_is_not_active]

Please note that this shortcode requires WooCommerce 3.2 or higher.


[coupon_is_applied]

This shortcode covers two usage scenarios:

  • Scenario A – Show content conditionally based on whether certain coupons have been applied to the cart.
  • Scenario B – Show coupon information for applied coupons.

Attributes

  • code : (required) One or more coupon codes, separated by comma, or * to show information for applied coupons.
  • op : (optional) The operation applied to evaluate the validity, and (default) as well as or are accepted. Indicating  op="and" requires all codes to be applied, op="or" requires at least one to be applied.

Example for Scenario A:

[coupon_is_applied code="test"]
This is only displayed if the test coupon is applied to the cart.
[/coupon_is_applied]

Example for Scenario B:

[coupon_is_applied code="*"]
The following discounts have been applied:
[coupon_discount separator="" element_tag="li"]
[/coupon_is_applied]


[coupon_is_not_applied]

This shortcode is similar to the previous one, except it will show content conditionally based on whether a coupon or a set of coupons is not currently applied to the cart.

Note that this shortcode does not allow to indicate a * for the code attribute.


[coupon_is_valid]

This shortcode will display the content it encloses only if the specified coupon codes are currently valid, or alternatively, if one of the coupon codes is valid.

A useful application of this shortcode is to increase a customer’s satisfaction, making her or him aware of the discount applied and increasing the chances of a completed checkout.

Example:

[coupon_is_valid code="test"]
This is only displayed if the test coupon is valid.
[/coupon_is_valid]

Attributes

  • code : (required) one or more coupon codes, separated by comma
  • op : (optional) the operation applied to evaluate the validity, and (default) as well as or are accepted; and requires all codes to be valid, or requires at least one to be valid

[coupon_is_not_valid]

This shortcode will display the content it encloses if the specified coupon codes are currently not valid. It also can be used to display its contents if one of the codes is not valid.

A useful application of this shortcode is to motivate a customer to add items to the cart to obtain the discounts related to the coupons.

Example:

[coupon_is_not_valid code="test"]
This is displayed if the test coupon is not valid.
[/coupon_is_not_valid]

Attributes

  • code : (required) one or more coupon codes, separated by comma
  • op : (optional) the operation applied to evaluate the validity, and (default) as well as or are accepted; and requires all codes to be invalid, or requires at least one to be invalid

[coupon_has_active]

This shortcode reveals the content it encloses if there are any active coupons. Coupons are considered active while they have not expired and their usage limits have not been exhausted.

Attributes

  • type : (optional) the coupon type (see the notes for the same attribute of the [coupon_iterate] shortcode)
  • number : (optional) limits the test to the number of coupons or uses the default (see the notes for the same attribute of the [coupon_iterate] shortcode)

Example:

[coupon_has_active]There are active coupons.[/coupon_has_active]

[coupon_has_not_active]

This shortcode reveals the content it encloses if there are no active coupons. Coupons are considered not active when they have expired or their usage limits have been exhausted.

Attributes

  • type : (optional) the coupon type (see the notes for the same attribute of the [coupon_iterate] shortcode)
  • number : (optional) limits the test to the number of coupons or uses the default (see the notes for the same attribute of the [coupon_iterate] shortcode)

[coupon_has_valid]

This shortcode reveals the content it encloses if there are any valid coupons. This will be true when certain coupons can be applied based on the current cart contents.

Attributes

  • type : (optional) the coupon type (see the notes for the same attribute of the [coupon_iterate] shortcode)
  • number : (optional) limits the test to the number of coupons or uses the default (see the notes for the same attribute of the [coupon_iterate] shortcode)

Example:

[coupon_has_valid]There are valid coupons that can be applied to your cart.[/coupon_has_valid]

[coupon_has_not_valid]

This shortcode reveals the content it encloses if there are no valid coupons.

Attributes

  • type : (optional) the coupon type (see the notes for the same attribute of the [coupon_iterate] shortcode)
  • number : (optional) limits the test to the number of coupons or uses the default (see the notes for the same attribute of the [coupon_iterate] shortcode)

[coupon_has_applied]

This shortcode reveals the content it encloses if any coupons are applied.

Attributes

  • type : (optional) the coupon type (see the notes for the same attribute of the [coupon_iterate] shortcode)
  • number : (optional) limits the test to the number of coupons or uses the default (see the notes for the same attribute of the [coupon_iterate] shortcode)

Example:

[coupon_has_applied]You're getting a discount, hooray![/coupon_has_applied]

[coupon_has_not_applied]

This shortcode reveals the content it encloses if no coupons are applied.

Attributes

  • type : (optional) the coupon type (see the notes for the same attribute of the [coupon_iterate] shortcode)
  • number : (optional) limits the test to the number of coupons or uses the default (see the notes for the same attribute of the [coupon_iterate] shortcode)

[coupon_iterate]

This coupon iterator shortcode makes it easy to work with a set of coupons, using the content it encloses as a template to render information about each coupon code in a sequential manner.

The content that the shortcode encloses, is rendered for each coupon in sequence. As you can include informational shortcodes, you can use this to dynamically render information about a set of coupons, or even for all coupons.

 

Here’s an example that will show coupon codes along with their descriptions for all coupons:

[coupon_iterate code="*"]
[coupon_code] – [coupon_description]
[/coupon_iterate]

 

We could expand on the example to only show this for coupons that are active:

[coupon_iterate code="*"]
[coupon_is_active][coupon_code] – [coupon_description][/coupon_is_active]
[/coupon_iterate]

 

Another example that makes use of the discount info and applies to a set of specific coupons:

[coupon_iterate code="test,welcome,premium,customer,summersale"]
[coupon_is_active]
Use the coupon code [coupon_code] for [coupon_discount].
[/coupon_is_active]
[/coupon_iterate]

 

This shortcode provides an easy way to apply a content template to render the same information for a set of coupons. The supported attributes allow to refine the set of coupons for which the content is displayed.

Attributes

  • code : (required) one or more coupon codes, separated by comma, * can be used for all published coupons
  • type : (optional) the coupon type (see below)
  • orderby : code (default) or IDor rand for random order
  • order : ASC (default) or DESC
  • number : if provided, will limit the number of coupons to the given number; the default value is 25*

Allowed types (for the type attribute) :

  • Specific coupon types: fixed_cart, percent, fixed_product, percent_product, sign_up_fee, sign_up_fee_percent, recurring_fee, recurring_percent
  • Coupon type sets: cart, fixed, percent, product, recurring, sign_up, subscription

One or more types can be indicated, separated by comma.

Regarding the number attribute, the default value can be changed by indicating the attribute explicitly. Due to the high performance impact of processing large numbers of coupons, it is not recommended to use high values. It is advisable to test and adjust, because even with the default limit a page using the shortcode in combination with coupon display shortcodes can take a considerable amount of time to be rendered. Also see the Notes section regarding the hard limit imposed via the WOOCOMMERCE_COUPON_SHORTCODES_HARD_LIMIT constant.


[coupon_enumerate]

This shortcode produces a list of codes that can be used to display information when combined with coupon information shortcodes. We recommend to use the [coupon_iterate] shortcode if you would like to display composed content for a set of coupons.

Example:

[coupon_enumerate code="*" type="percent"]
[coupon_discount element_tag="div" prefix="code"]
[/coupon_enumerate]

The above example will produce a list of percentage discount coupons showing the coupon code along with their discount information.

Attributes

  • code : (required) one or more coupon codes, separated by comma, * can be used for all published coupons
  • type : (optional) the coupon type (see below)
  • orderby : code (default) or IDor rand for random order
  • order : ASC (default) or DESC
  • number : if provided, will limit the number of coupons to the given number; the default value is 25

Allowed types (for the type attribute) :

  • Specific coupon types: fixed_cart, percent, fixed_product, percent_product, sign_up_fee, sign_up_fee_percent, recurring_fee, recurring_percent
  • Coupon type sets: cart, fixed, percent, product, recurring, sign_up, subscription

One or more types can be indicated, separated by comma.

Regarding the number attribute, the default value can be changed by indicating the attribute explicitly. Due to the high performance impact of processing large numbers of coupons, it is not recommended to use high values. It is advisable to test and adjust, because even with the default limit a page using the shortcode in combination with coupon display shortcodes can take a considerable amount of time to be rendered. Also see the Notes section regarding the hard limit imposed via the WOOCOMMERCE_COUPON_SHORTCODES_HARD_LIMIT constant.


Informational

The following shortcodes can be used on their own or within the conditional shortcodes above. If used within [coupon_is_valid] or [coupon_is_not_valid], you do not need to specify the coupon codes again as these shortcodes will be aware of the coupon codes that have been used to evaluate the display condition. This can be an advantage depending on the use case and the number of codes specified.

[coupon_show]

This shortcode allows to render the coupon code, its description and its discount info together. It works with the [coupon_enumerate] shortcode if you want to show this information for several codes. For each entry, it will show the information about the code, description and discount together and is thus useful if you want to produce a listing of coupons and their discount. By default, it will render only the coupon code and discount info for each coupon. Here’s an example:

[coupon_enumerate code="*"]
[coupon_show]
[/coupon_enumerate]

The above will list all coupon codes along with their discount info.

The following example will 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]

Attributes

  • show : Specify a comma-separated list of information to show for each entry. Valid options are code, discount and description. The default is “code,discount”.
  • code : An list of coupon codes. If used with the [coupon_enumerate] shortcode, this is not required.
  • before : HTML to render before the output is produced. The default is an opening "<div>".
  • after : HTML to render after the output is produced. The default is a  closing "</div>".
  • before_entry : HTML to render before each entry. By default this is the empty string "".
  • after_entry : HTML to render after each entry. By default this is the empty string "".
  • separator : This is output between each aspect rendered within one entry. For example, it will separate the code and discount info. Defaults to a single space " ".

[coupon_code]

This will display the coupon code(s) as provided through the code attribute. Rather than using this shortcode on its own, it usually makes more sense to use it within one of the conditional shortcodes, as in that case, you don’t need to specify the code(s) again and can use it to display the coupon code to the customer.

Attributes

  • code : one or more coupon codes, separated by comma – only required when used on its own (see above)
  • separator : characters used to separate several codes, defaults to a space

[coupon_description]

This shortcode displays the description(s) of the coupon code(s) specified, or when used within a conditional shortcode without specifying the code attribute, for those coupons indicated with the conditional shortcode.

Attributes

  • code : one or more coupon codes, separated by comma – only required when used on its own (see above)
  • separator : characters used to separate several codes, defaults to a space
  • element_tag : defaults to span, also allows li (the ul will enclose the list), div and p
  • prefix : if code is indicated for this attribute, the coupon codes will be prefixed to the descriptions
  • prefix_separator : defaults to a single space, used when prefix="code" is indicated

[coupon_discount]

This shortcode displays information about the discount related to the coupon. This will be displayed for the coupon code(s) specified, or when used within a conditional shortcode without specifying the code attribute, for those coupons indicated with the conditional shortcode.

The shortcode builds a standardized message for coupons or uses the information provided when the WooCommerce Volume Discount Coupons extension is used.

Attributes

  • code : one or more coupon codes, separated by comma – only required when used on its own (see above)
  • separator : characters used to separate several codes, defaults to a space
  • element_tag : defaults to span, also allows li (the ul will enclose the list), div and p
  • prefix : if code is indicated for this attribute, the coupon codes will be prefixed to the descriptions
  • prefix_separator : defaults to a single space, used when prefix="code" is indicated

Notes

The extension will process up to WOOCOMMERCE_COUPON_SHORTCODES_HARD_LIMIT coupons when treating the [coupon_iterate] or [coupon_enumerate] shortcodes. This hard limit can be overridden in your wp-config.php by adding:

define( 'WOOCOMMERCE_COUPON_SHORTCODES_HARD_LIMIT', 100 );

In the above case, the limit is reduced to 100 coupon codes. You could also increase the hard limit but we recommend to keep it low. If you iterate over a large number of coupon codes on a page, it can take quite some time to render the page content because processing coupons is rather slow.