How to generate WooCommerce Coupon Shortcodes

Want to boost the sales of your online store? Easy. Give your customers a sense of being rewarded. When buyers realize that they are getting something in return when they purchase something, makes them want to spend more on the same site.

A great way to do this is to give away coupon codes to your customers. The coupon codes can be used to avail an offer set by you. Supposedly, coupons could be used to get a 30% off on the next checkout.

Getting this functionality on your website is really simple. You just have to add the piece of code below in the theme functions. The code allows you to display the coupon input field wherever you may like. Making it more convenient for you to set it up however you like.

Follow the steps coming up to get this on your website:

You will have to add the code below to Theme function. Just go to Dashboard > Appearance > Theme Editor > Theme Functions and add the following code to the end of the existing code and click Update File.

add_shortcode( 'coupon_field', 'display_coupon_field' );
function display_coupon_field() {
    if( isset($_GET['coupon']) && isset($_GET['redeem-coupon']) ){
        if( $coupon = esc_attr($_GET['coupon']) ) {
            $applied = WC()->cart->apply_coupon($coupon);
        } else {
            $coupon = false;
        $success = sprintf( __('Coupon "%s" Applied successfully.'), $coupon );
        $error   = __("This Coupon can't be applied");
        $message = isset($applied) && $applied ? $success : $error;
    $output  = '<div class="redeem-coupon"><form id="coupon-redeem">
    <p><input type="text" name="coupon" id="coupon"/>
    <input type="submit" name="redeem-coupon" value="'.__('Redeem Offer').'" /></p>';
    $output .= isset($coupon) ? '<p class="result">'.$message.'</p>' : '';
    return $output . '</form></div>';


After adding the code to Theme Functions, you will have to display a message with a box for users to input the coupon code or simply a coupon input message.

This can be done by pasting the following short-code to any place on your website you want to show it,


As it can be seen I have pasted the short code on the about section:


In just these simple and quick steps you can get hold of a very helpful option to help you in developing your online store.

Do give us your feedback on this in the comments section below.

Leave a Reply