Site icon Orion Origin

How to create coupon codes in WooCommerce natively or with PHP

coupon code in woocommerce

WooCommerce coupon codes, just like any coupon from any content management system (CMS)  can help attract new customers and generate a lot of sales, as of 2020, almost 90% of consumers used coupons (Source: Statista). Therefore for every online store owner, coupons are of the utmost importance. Knowing when and how to use them can be just the push you need to own a million-dollar store.

 Easily attract more customers and grow your business by using our Custom Product Designer for Woocommerce.

How to create a WooCommerce Coupon Code natively 

WooCommerce makes it pretty easy to create and manage coupon codes. Coupons are a standard WooCommerce feature that allows store admins to generate a code that can be used by the customer to apply a discount on his order without using any external add-ons. The coupon could either be for applying a discount on a specific product on the entire cart.

Creating a WooCommerce Coupon Code that will apply a discount on a specific product

Step 1: In order to set up a percentage discount on products using coupons, kindly go to Marketing > Coupons from your store admin area.

Step 2: Click on the ‘Add Coupon’ button to access the coupons creation page

Step 3: Enter your coupon code or click on ‘Generate coupon code’ to generate a random code for you. Keep in mind that all coupon codes must be unique.

Step 4: Please select Percentage discount as the discount type.

Step 5: Enter your percentage number in the coupon amount field.

Step 6: If you want to set an expiration date for the coupon, you can enter that date in the coupon expiry date field

Step 7: Click on Usage restriction to configure the coupon conditions for the customer to get the discount:

Step 8: Click on Usage limits to configure the coupon conditions for the customer to get the discount:

Usage limit per coupon: allows you to set the number of times the coupon can be used

Once you’re done, click on the ‘Publish’ button to make your coupon live.

 Easily attract more customers and grow your business by using our Custom Product Designer for Woocommerce.

Creating a WooCommerce Coupon Code that will apply a discount on the cart total

If you want the coupon to be applied on the entire cart instead of specific products, follow the above steps but, make sure the ‘products list’ to apply the discount on is empty. 

That way, instead of applying the discount on a specific set of products, the coupon will apply the discount to the entire cart.

 Easily attract more customers and grow your business by using our Custom Product Designer for Woocommerce.

How to create a WooCommerce Coupon Code by using the programming language – PHP 

As a WordPress developer, you can use the PHP programming language to create coupon codes for your Woocommerce store or for your clients that have Woocommerce stores, simply because WordPress and Woocommerce run on PHP. To do so, you can use the code below: 

//Once you choose your coupon code you can enter it below
$coupon_code = 'UNIQUECODE2';

//Then you can set the amount that is fixed if you're setting a fixed discount. 
//If not, the number you set will be considered as a percentage.
$amount = '10'; // Amount

//Choose your discount type among these: fixed_cart, percent, fixed_product, percent_product
$discount_type = 'fixed_cart';

//Coupons are WordPress custom post types. So we can create them just like a regular custom post type
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon' );

$new_coupon_id = wp_insert_post( $coupon );

//We make sure the coupon has been created so we can update it's properties
if ( $new_coupon_id ) {

    update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
    update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
    update_post_meta( $new_coupon_id, 'individual_use', 'no' );
    
    //The list of products to which the coupon should apply is an array of the product id numbers
    update_post_meta( $new_coupon_id, 'product_ids', '' );
    
    //The list of products to exclude from the coupon application is an array of the product id numbers
    update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
    update_post_meta( $new_coupon_id, 'usage_limit', '' );
    update_post_meta( $new_coupon_id, 'expiry_date', '' );
    update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
    update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

} else

echo "The coupon creation failed....";

There you have it, how to create your coupon codes natively with your WooCommerce or using PHP programming language. Other than using coupon codes, there are other ways of offering discounts to your customer and target audience in order to increase your sales, you can check here for a guide about discount types and how to set them up. 

Drop your thoughts or questions in the comment section, we are more than happy to listen and help!

Exit mobile version