Website Building » WooCommerce » How Do I Add a Coupon Code to WordPress WooCommerce?

How Do I Add a Coupon Code to WordPress WooCommerce?

Last updated on January 17, 2023 @ 1:48 pm

Adding a coupon code to WordPress WooCommerce is a great way to offer discounts to your customers. There are two ways to do this: using the WooCommerce interface or adding code to your functions.php file.

To add a coupon code using the WooCommerce interface, go to WooCommerce > Coupons. Then, click on the Add Coupon button.

Enter a description, coupon code, and amount. You can also set an expiration date, if desired. Once you’re finished, click on the Save Coupon button.

If you want to add a coupon code via code, open up your functions.php file and add the following:

function my_custom_discount() {
  $coupon_code = 'SOMECOUPONCODE'; //Enter your coupon code here
  $amount = 10; //Enter the amount of discount here
  add_action( 'woocommerce_before_cart', 'apply_custom_discount' );
function apply_custom_discount() {
  global $woocommerce;
  $woocommerce->cart->add_discount( $coupon_code );
}
}
add_action( 'init', 'my_custom_discount' );

Conclusion:

Adding a coupon code to WordPress WooCommerce is a great way to offer discounts to your customers.

PRO TIP: If you are planning to add a coupon code to your WordPress WooCommerce site, please be aware that there is a risk that your site could be hacked. This is because adding a coupon code to your WooCommerce site gives hackers a way to access your site and potentially steal information.
Kathy McFarland

Kathy McFarland

Devops woman in trade, tech explorer and problem navigator.