Site icon Qaisar Satti's Blogs

How to Create Coupons Programmatically in WooCommerce

In this tutorial, you are going to learn how to create discount coupons programmatically in WooCommerce. You can easily achieve this by using the WC_Coupon class. Let’s walk through the process with a simple example.

Here is the code snippet:

$coupon = new WC_Coupon();

    $coupon->set_code( 'blackfriday' );
    $coupon->set_amount( 20 );
    $coupon->set_description( 'coupon description here.' );
    $coupon->set_discount_type( 'percent' );
    $coupon->set_date_expires( '31-12-2024' );

$coupon->save();
 

 

Exit mobile version