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 🙂

Hope this helps, Roelof!


Cover Photo Credit: Stefan Baudy (CC BY 2.0 license)
Note: This post may contain affiliate links, which means we get a commission from the plugin, theme, or service provider if you choose to purchase. Regardless of this, we are committed to providing high quality, unbiased resources. Have more questions? See our affiliate policy. You can use the plain links below if you prefer that we not receive a referral commission:

Articles you may also like:

Posted by Beka Rice

Beka Rice manages the direction of Sell with WP content and writes or edits most of our articles to share her interests in eCommerce. Or she just writes as an excuse to spend more time jamming out to anything from The Clash to Lady Gaga. Who knows.

Leave a reply

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