Do you want to create coupons and add discounts to increase customer participation? This guide describes how to programmatically create and apply coupons in woocommerce. In order to increase sales, we will show various examples that can be done. Why does woocomerce use discount coupons? The conversion rate of most e-commerce websites is almost no more than 3%, so store owners always find ways to increase sales. One of the most effective ways to achieve this is to use discount coupons. Discounts can provide additional benefits to help you make decisions and buy products. For example, use coupons on important e-commerce dates such as Christmas or Black Friday, offer discounts for a limited time, create coupons for the most loyal customers, etc. There are many ways to offer coupons to improve conversion rates and sales.
Therefore, this guide will show you how to programmatically create and apply coupons in woocommerce. In this tutorial, you will learn how to programmatically create and apply coupons in woocommerce. Woocommerce coupon making coupon applicable: the function of shopping basket theme according to all product IDS, specific product subtotal shopping basket value and quantity of goods. You will edit the PHP file, so you must install the subtopics before you begin. You can create or use a sub theme plug-in. In addition, we will also use several woocomer hooks. If you are not familiar with it, we suggest you check this post.
1) Before applying coupons to generate coupons in woocommerce settings, you must generate coupons. Therefore, the first step is to create coupons in woocommerce. The built-in woocommerce feature makes it easy to create with a few clicks. On the WordPress dashboard, navigate to woocommerce > coupon and click Add coupon. In some versions of woocommerce, you can find it in marketing > coupons. Then enter the coupon code name. You can enter the desired name or use an automatically generated name. In this case, we specify the name auto_union.
Then, you must select the discount type (percentage, fixed cart discount or fixed product discount), amount (percentage or fixed amount), and expiration date. You can also set the product restrictions applicable to coupons, the minimum amount that customers need to pay to apply coupons, the usage restrictions of coupons or each user, etc. After setting up the coupon, click publish. These settings can be changed at any time. If you do, you must adjust the script to use to match the settings in the new coupon code. For more information on how to create and set up coupons in woocommerce, please check the guide at this stage.
Now we know how woocomerce creates coupons. Let’s see how to apply. 2.1) before applying coupons programmatically on all products, let’s see how to apply coupons to woocommerce products. To do this, use the woocommerce_before_cart hook to run the script before loading the shopping cart page. In addition, the woommerce shopping cart object is used to search for the information needed to run the validation before applying the auto_coupon code just created. The WC – > cart object is available on the front page and can search all information related to the shopping cart page. Therefore, the sub topic of the function. Paste the following script into the PHP file:
add_action( ‘woocom
ommerce_before_cart’,’QuadLayers_apply_coupon_cart_values’);
Function quadlayers_apply_coupon_cart_values (){
\/\/Previously generated coupons
$coupon _code = \
\/\/Get coupons
$current_coupons = WC – > shopping cart – > get_coupons();
\/\/Import cart subtotal
$cart_st = WC () – > shopping cart – > subtotal;
\/\/&Coupons do not apply until the condition is true.
if(empty($current_coupon)&&$cart_ST>=50):
\/\/Apply coupons
WC – > shopping cart – > applicable coupons ($coupon_code);
wc_print_notices();
\/\/Coupon Applied & conditional false
Else if (! Empty ($current_coupon) & & $cart_st maleshopping basket – > remove_coupons (sanitize_text_field ($coupon_code);
wc_print_notices();
WC – > shopping cart – > calculate_totals();
Echo \
\/\/Condition is false, not applicable & coupons.
Else: \/ \/ do nothing
Echo \
Endif
}
Of course, you can replace $50 in the script with another price that is more suitable for your business. When conditionally adding or deleting coupons, the same WC object instance is used for accurate control. WC – > cart – > subject allows you to store the value in the $cart_st variable to understand the shopping cart section and.
Male: WC – > cart – > calculate_totals (); After applying the coupon, recalculate the total amount of the shopping cart. 2.5) according to the quantity of goods in the shopping cart, just like applying coupons to the whole shopping cart, woocommerce coupons can be applied programmatically only when the following script is used to minimize the number of products in the shopping cart. For example, if a customer has 4 or more products in their shopping cart, a discount is added. add_action(‘woocommerce_before_cart’,’QuadLayers_apply_coupon_cart_values’);
Function quadlayers_apply_coupon_cart_values (){
\/\/Previously generated coupons
$coupon _code = \
\/\/Get coupons
$current_coupons = WC – > shopping cart – > get_coupons();
\/\/Get product quantity
$p_count = wc() – > shopping cart – > get_cart_contents_count();
\/\/&Coupons do not apply until the condition is true.
if(empty($current_coupon)&&$P_count>=4):
\/\/Apply coupons
WC – > shopping cart – > applicable coupons ($coupon_code);
wc_print_notices();
\/\/Coupon Applied & conditional false
Else if (! Empty ($current_count) & & $p_count maleshopping basket – > remove_coups (sanit_text_field ($coupon_code)
);
wc_print_notices();
WC – > shopping cart – > calculate_totals();
Echo \
\/\/Condition is false, not applicable & coupons.
Else: \/ \/ do nothing
Echo \
Endif
}
If the discount applied by shoppers when purchasing 4 or more products is not suitable for your business, you can replace it with another number you need. The new variable used here is $p_count, which stores the number of products added to the shopping cart. This will ensure that coupons can only be added or deleted when needed. This is just an example of several ways to programmatically apply woocommerce coupons, but the possibilities are infinite. On the basis of this script, give full play to your creativity and create your own script!
WC object instances are a complete list of woocommerce object instances used in all the scripts above, as well as several other instances that may be useful when adding discounts programmatically. WC – > shopping cart – > applicable coupons (\
Limits that can be set. For example, you can limit minimum or maximum expenses, limit discounts for specific products, and so on. You can also set a limit on the number of coupons used in the usage limit and limit the amount used by each user. Note: it is recommended that you limit the usage of each user so that they can only be used once. Male then go to the URL coupon section and enter coupon details. Select the URL that the customer wants to redirect and create a success message. You can see that the coupon URL will be generated automatically. However, if you want to customize, you can override the box with the code URL. Finally, click \