Output a WooCommerce Coupon Description Instead of Code

This Ask Sell with WP question comes from Roelof:

Is there a way, in the [WooCommerce] cart, to use the coupon description, not the code?


Sure thing! Outputting the WooCommerce coupon description instead of the code is very do-able. However, it’s not so simple to get the coupon description with current versions of WooCommerce, as you have to know a bit about how coupons are stored (though this is a lot simpler with WooCommerce 3.0+, which is coming soon).

If you want to follow this tutorial, please be sure you know how to add custom code to your site.

First, we need to determine if there’s a way to “hook” into the coupon code output to change it. By default, the coupon output will be Coupon: {code}:

WooCommerce Coupon Label: Default output
Default output

When we dig into WooCommerce a bit, we find where this is output, and see the woocommerce_cart_totals_coupon_label filter is available to let you change the label for applied coupons, giving you the label to change and the coupon itself to refer to.

Even though you have the coupon, for current and past versions of WooCommerce, there’s no “method” or straight-forward way to get the coupon description. You have to know that the coupon is a “custom post” in WordPress, and that the description is the “post excerpt” for the coupon’s post. So getting this is a little convoluted, as you have to use the coupon to get the WordPress post for it, then you can get the excerpt.

We can get the description by doing this when we have the coupon:

$coupon_post = get_post( $coupon->id );
$description = ! empty( $coupon_post->post_excerpt ) ? $coupon_post->post_excerpt : null;

Great, that works for WooCommerce pre-version 2.6. Now what about in the upcoming version 3.0 release? We can do it in a much more direct way:

$description = $coupon->get_description();

Nice, huh? So let’s put this together in a small snippet to (1) account for different WooCommerce versions to (2) get the coupon description, and (3) use this in the coupon label instead of the coupon code. You can view the full snippet here.

Now let’s see what this changes. If there is a coupon description, instead of outputting Coupon: {code}, we’ll output Coupon: {description}:

WooCommerce Coupon Label: WooCommerce coupon description
Description output

If there is no description, this code will just output Coupon instead, without a code or a description.

WooCommerce Coupon Label: no description output
No description

This won’t be a full replacement for hiding coupon fields or codes from customers, but it is handy to give them some more description about a WooCommerce coupon, or details when it’s applied to the cart smile

Hope this helps, Roelof!


Cover photo credit: Stefan Baudy (CC BY 2.0 license)

Beka Rice
Beka is the head of product at Jilt. She works on app improvements, integration plugins, helping merchants improve recovery campaigns, and shares tutorials on reducing abandonment or improving recovery on our blog.

2 Comments

  1. It makes a lot of sense to show the coupon description instead of the coupon code. It is now more flexible and convenient to manage woocommerce coupons, especially when there are many.
    Thank you for the snippet. Have an awesome day!

Leave a Reply

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