WooCommerce programatically add a coupon based on a page view via a shortcode

September 17, 2015 | PHP, Plugins, Tutorials, WooCommerce, Wordpress | 0 comments

Just a quick one from me today. I am using a coupon to apply a discount to an order for a client (why not eh..?!) but the problem is they don’t want a coupon based approach for everyone and sometimes just coming from a certain source or landing on a certain page is enough. So I wrote this little shortcode snippet to do just that. Essentially it takes the coupon code given as part of the shortcode arguments list and applies it when the shortcode is processed.

Ideally you need to ensure that the coupon doesn’t have any restrictions on it or require certain items to be in the cart or it will fail to add however for my purpose where a fixed deal is added to anyone who uses it it’s perfect!

It’s safe for use on pages like the homepage because once a coupon has been added it won’t try and add it again.

The code

add_shortcode('sb_add_coupon', 'sb_add_coupon');

function sb_add_coupon($atts) {
     if (function_exists('WC')) { //is woocommerce active
          if (@$atts['code']) {
               if (!WC()->cart->has_discount(sanitize_text_field($atts['code']))) {
                    wc_print_notices(); //wc_clear_notices();


Just add the following to any piece of content that supports shortcodes on your site. Best used on landing pages rather than places like the homepage.

[sb_add_coupon code="coupon_code"]


If you want to get rid of the message that shows up when the coupon is added then simply remove the line which contains wc_print_notices(); . WooCommerce won’t be happy though until the user is notified of the addition or failure so as soon as they visit a WooCommerce controlled page the message(s) will show. To suppress this also then add wc_clear_notices(); to the function. This will suppress all messages from this action so it happens transparently.

A Donate Button!


Submit a Comment

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

CommentLuv badge

Divi United

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!