How to create coupons on WordPress websites?

Creating Coupons

Lesson Details:
December 09, 2020


I: Introduction:

WordPress:

It is a free and open-source content management system (CMS) based on PHP and MySQL. Content management systems (CMS) are computer programs designed to make website development easier by allowing the modification or creation of content on a website.

eCommerce:

It is a business model that allows people or businesses to sell goods or services over the internet, usually through a particular website. e-commerce is one of the most important trends in today’s world. It is an online trading with the help of electronic media. An e-commerce site is a combination of business and marketing strategies used for selling products or services over the Internet. It consists of a number of functionalities such as product catalog, shopping cart, payment options, customer care, delivery options etc.

II: Body:

Creating coupons:

In this section we will be creating a coupon. In previous versions of WordPress all the coupon codes were stored in the database as plain text. And that was really vulnerable as anyone with access to your database would have been able to see your coupon codes. In WordPress 3.1 it’s possible to turn your coupon code into an MD5 hash making it impossible for anyone with access to your database to find out what it is. The benefits of using a hash instead of a plain text coupon code is that it can’t be decrypted and therefore you don’t have to worry about anyone finding out what it is.

I recommend you install WordPress 3.1 before reading this tutorial as it makes use of some functions which are only available in 3.1.

Let’s create our first coupon by adding the following code to our functions.php file:

function my_coupon_code() { $hash = md5(get_option('my_coupon_code')); $hash = $hash.$this->network_domain . 'my_offer'; return $hash; } add_filter('woocommerce_coupon_code', 'my_coupon_code'); 1 2 3 4 5 function my_coupon_code ( ) { $ hash = md5 ( get_option ( 'my_coupon_code' ) ) ; $ hash = $ hash . $ this -> network _ domain . 'my_offer' ; return $ hash ; } add_filter ( 'woocommerce_coupon_code' , 'my_coupon_code' ) ;

The first line creates a function called my_coupon_code() which accepts no arguments. We then create a variable called $hash to contain our coupon code and set it equal to the md5 function (http://php.net/manual/en/function.md5.php) which returns an MD5 hash value if given a string input. The next line uses the WordPress filter feature to change the value stored in the database so that it now contains our MD5 hash value. This means that when WordPress reads the database, all that will be stored there is our MD5 hash value instead of our coupon code which can now be regenerated at any time by typing in my_coupon_code() into the WordPress admin area. Now let’s create an offer for our coupon code:

add_action('admin_notices', 'my_offer'); function my_offer() { ?>

My Offer

Code: ".$this->my_coupon_code(); ?>

Expiry Date: ".date("F jS Y", strtotime("+7 days")); ?>

Amount: ".number_format($this->discount, 2)."

< div class = "wrap"> < h3> My Offer < / h3 > Code: " . $ this -> my _ coupon _ code ( ) ; ?> < / p > < p> < strong> Expiry Date : < / strong > " . date ( " F jS Y" , strtotime ( "+7 days" ) ) ; ?> < / p > < p> < strong> Amount : < / strong > " . number _ format ( $ this -> discount , 2 ) . "

" < / p > < / div >
We put this code in our functions.php file and then hook into the admin notices action which will run every time someone logs into their admin area, this is how we trigger our offer to show up in the admin notices section where people will see it when they log in. From here you can simply copy and paste this code into any part of your theme where you want this offer to show up. You can also edit the values in the “Expiry Date” and “Amount” sections to whatever you like, but remember to change them both or else you might cause an error when saving your offer! Now go ahead and create your own coupon codes by just pasting this code into your functions file and changing $this->network_domain with your own domain. Now let’s test our coupon code by creating some products with it! Create two new products or edit existing products and add the following information: Title: Test Coupon Code Price: £10 Description: This product costs £10 using my coupon code Maximum Stock: 0 Display Settings: Show Enabled Stock Status: Disabled Save this product and save these changes, then go back to the product page and enter my_coupon_code() into the discount field like shown below: Click on update and you should get an error message saying that there is no matching discounts found for this entry because we haven’t created any discounts yet! So let’s do that now by creating another function that checks if the discount has been used or not like shown below: function my_discount() { global $woocommerce; // Return true if the coupon has been used, false if not return ($woocommerce->cart->get_quantity($this->product->id()) == 0); } add_filter('woocommerce_cartitem_discount', 'my_discount', 10, 3); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function my_discount ( ) { global $ woocommerce ; // Return true if the coupon has been used, false if not return ( $ woocommerce -> cart -> get _ quantity ( $ this -> product -> id ( ) ) == 0 ) ; } add _ filter ( 'woocommerce_cartitem_discount' , 'my_discount' , 10 , 3 ) ; The first line creates a function called my_discount() which accepts no arguments and calls the global variable woocommerce which is an object created by WooCommerce for interacting with e-commerce data. We then check if the quantity in the cart for this product equals zero which means that the discount has been used and call our my_coupon_code() function to regenerate a new one for us. The next line adds a filter to the woocommerce_cartitem_discount filter which will run every time any discounts are applied to our product, this is how we add our discount to our product when it has been used, we just add another line like shown below just above the add _ filter('woocommerce_cartitem_discount', ... line: add _ filter('woocommerce_cartitem_discount', 'my_discount', 10, 3); Now go ahead and add your new discount and go back to the product page and click on update! You should now see that your discount has been applied successfully! You can do this for all your products and then you can use this code in your theme like shown below:
    cart->get_cart() as $product): ?>
  • get_permalink(); ?>">
loader
Course content