Another thing, using Smart Coupons, you can auto apply coupon based on cart total, product categories, etc.. Not one, not two but a maximum of five coupons can be auto-applied using Smart Coupons.. Thanks four sharing this code. Using it well will give you higher sales, repeat customers and better customer loyalty. This line allows you to prevent the coupon from being applied for specific products. Sometimes you need to add the discount bases on some condition. } $woocommerce->cart->add_discount( $cw_coupon ); Add the discount amount or percentage for the code without currency or percentage symbol. The first way to add the coupon code programmatically is by using the wp_insert_post() function which allows you to add or update a WordPress post. PHP . WooCommerce provides an option to create a coupon from the dashboard. In this case, the coupon will be automatically applied to the cart once a user goes to the WooCommerce Cart page. The following comprises of WooCommerce extensions that allows you to generate coupon codes and create coupon discounts as well. Eg: CHRISTMAS30, NewYear20. Useful for validating a coupon with an This is all you need to do to create a new coupon in WooCommerce programmatically. However, you need to first create a coupon as you do and set restrictions as per your choice. Barclay ePDQ payment gateway for WordPress and WooCommerce 2.1.x, Customising WooCommerce notification emails with hooks and filters, © 2021 Moot Point Freelance Web Developer in Brighton, Sussex. The gift card validity had to be checked via a SOAP call to an external API, in other words the coupons did not exist in WooCommerce. Subscribe. The latest ones are on Apr 04, 2021 Using wp_insert_post() function. Then apply that coupon to your order: if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) $woocommerce->show_messages(); That last function returns a BOOL value: TRUE if the discount was successful, FALSE if it fails for any one of a variety of reasons. Simple Product. How to add a coupon programmatically in Woocommerce Before you start, please download and install the Discount Rules for the WooCommerce PRO plugin. Using the inbuilt WC feature you can easily create them in a … Laravel .Net . You can choose whatever type of discount you want based on your needs. It’s not working for my store. percent : It gives a fixed percentage discount for the entire cart. There is a woocommerce_coupon_is_valid filter but it only runs after checking if the coupon exists in WooCommerce, and so will never run in our case. Programmatically Apply a Coupon (PHP Snippet) This method is my favorite. Today we talk about how WooCommerce apply discount programmatically. The first step in order to be able to do so would be to create a coupon in WooCommerce. You can leave it blank or set a date in the following format inside the two single quotes. ... How to Create Unique Coupon Codes in WooCommerce programmatically. For some scenarios, we don’t need to create the coupon in the WordPress database at all. Before applying coupons, you need to create them. Codeigniter . WooCommerce Creating Order Programmatically. Magento 1 - programmatically create promotion coupon codes ... Sale For Today Only at mage2-blog.com Apr 4, 2017 I showed how to programmatically create promotion coupon codes in Magento 1. WooCommerce Next Order Coupon WooCommerce Next Order Coupon is an extension that generates a unique WooCommerce discount code for every completed order and adds it to the order confirmation email. CODES (4 days ago) Using the inbuilt WC feature you can easily create them in a few clicks. Creating promotional coupons programmatically on the WooCommerce store is easier than you think. function sh_coupons_matched() { 40% off (5 days ago) Discover the latest WooCommerce Coupon Code & Sales here and get huge savings on any premium woocommerce plan, theme, and extension. NB - Normally, you'd create a coupon by logging in to yoursite.com/wp-admin and going WooCommerce > Coupons > Add Coupon. It uses the ‘ woocommerce_get_shop_coupon_data ‘ filter that “ allows custom coupon objects to be created on the fly ” (according to the comment in the WooCommerce code). Step 1: Set up New Coupon. I'm trying to generate woocommerce coupon programmatically from my custom wordpress plugin. Get in touch if you want help setting this up on your site, Store Pro is a global e-Commerce technical support company. Other CMS systems are in the pipeline, WooCommerce Tips and Tricks | Latest WooCommerce News, How to Create a Coupon Programmatically in WooCommerce. If the cart contains three t-shirts of $20 and the discount value is 20, the buyer will get a 20% discount for each item in the cart. You can put a desired number inside the two single quotes. 1. It requires a little PHP that must be placed in your child theme’s functions.php (here’s a free video lesson in case you don’t know how to override WooCommerce the right way).. Since the woocommerce_get_shop_coupon_data is triggered several times in the cart, we need to first check if we’ve already created the coupon: Thank you for sharing this tutorial. To add a new coupon, go to WooCommerce → Coupons and click Add coupon. This allows you to control if the coupon can be used in combination with other coupons. This restriction will help a store owner to create a coupon code which when used, other coupon codes cannot be used in conjunction with it. function generate_coupon($coupon_code) {. I'm looking for a way to programmatically apply a coupon code via action/hook through functions.php. Then enter a coupon code name. It automatically implements the coupon code in buyers’ carts during checkout, provided they meet the necessary Cart Conditions. The opening screen will present you with the Coupon code and Description fields: Coupon code is the code customers will enter to apply the coupon to their cart. }. monetizer commented on Dec 29, 2013 I created a new … That is what I was looking for dynamic coupon validation. update_post_meta( $new_coupon_id, ‘usage_limit_per_user’, ” ); This line allows you to set an expiry date for the coupon. I soon found a post to Create WooCommerce coupons programmatically. Coupon creator is the WP coupon plugin you got to choose if looking to create your own coupon codes. foreach ( $woocommerce->cart->cart_contents as $key => $values ) { You can also subscribe without commenting. If you leave is blank, the coupon will be applied to all products. Home > Posts > Blog > Create WooCommerce coupons programmatically, by Robin Hislop | 9 Jan 2017 | Blog | 2 comments. $sh_coupon = ‘OnlineShop’; Home / Uncategorized / woocommerce apply discount programmatically. Required fields are marked *. You can choose by adding their product IDs inside the two empty single quotes. However, if we need to check usage limits we will need to create a database record so that WooCommerce can keep track of how many times it has been used. All . Today i had to create a system that would create different coupons for a site in Woocommerce… It was a great starting point. All you need to do is adding a few lines of code in the functions.php file in the theme. Your email address will not be published. If your discount value is 20 and the total cart value of a buyer is $200, the buyer will get a $20 discount and the final amount will be $180. The guide assumes that you already have the plugin installed and activated. We specialize in helping individuals and businesses run successful, engaging websites. Follow is the snippets how can you achieve the functionality. How to Create a Coupon Programmatically in WooCommerce , Create a WooCommerce coupon in code using the woocommerce_get_shop_coupon_data filter. Programmatically Create Coupon Woocommerce can offer you many choices to save money thanks to 12 active results. In some WooCommerce versions, you may find it under Marketing > Coupons. In this example, we choose fixed_cart type. So in our case, we are adding a post with post type shop_coupon using this function. $coupon = new WC_Coupon(); $coupon->set_code($coupon_code); //the coupon discount type can be 'fixed_cart', 'percent' or 'fixed_product', defaults to 'fixed_cart'. If our hook code returns an array with the coupon properties, WooCommerce will apply the coupon to the cart and calculate the correct discounts. GitHub Gist: instantly share code, notes, and snippets. if( in_array( $values[‘product_id’], $autocoupon ) ) { You can change the ‘yes’ to ‘no’ to apply the coupon to the cost after adding the cost. It’s recommended to create a child theme instead of directly editing your theme to make changes on your website. So, in the WordPress dashboard, go to WooCommerce > Coupons and click Add coupon. In some WooCommerce versions, you may find it under Marketing > Coupons. Then enter a coupon code name. Sometimes, you may not want to give discounts for particular products. The best way to open your functions.php is via the Theme Editor menu in the WordPress dashboard. 1. fixed_cart : Fixed cart discount is used to define a fixed discount amount to the entire cart. $autocoupon = array( 65 ); You can create multiple coupons like this using the code snippet again. For example “freeweek”, … This is all you need to do to create a new coupon in WooCommerce programmatically. Coupon creator allows you to create, style and display coupons to your customers. The easiest way to automate discounts in WooCommerce is by using a plugin such as Advanced Coupons: With Advanced Coupons, you can set up anything from discounted shipping to Buy One Get One (BOGO) deals, all through the magic of coupons. It allows you to set the number of times the coupon can be used by all customers. To create one-click notification coupon you need to ensure that you already have a coupon created on your website. Copy the following code snippet and paste it at the bottom of the functions.php file. This line allows you to choose the products that must be in the cart in order for the discount to be applied by adding the product IDs inside the two single quotes. 3. https://www.cloudways.com/blog/create-woocommerce-coupon-code/, Output a Bootstrap grid from an ACF repeater field, Create ACF field programmatically permanently in the database, WooCommerce make UK county checkout field required, Disable Yoast WordPress SEO for certain roles. global $woocommerce; To apply the discount on simple products use the following code. Click on functions.php file from the list to open it on the text editor at the center of the screen. add_action( ‘woocommerce_before_cart’, ‘sh_coupons_matched’ ); Here is a function that allow creating programmatically an order nicely with all required data in it: This line of code allows you to decide if the coupon should be applied before or after the tax or before the tax is added to the total cost. Plus, you can get WooCommerce to add discounts programmatically in more than one way.