In the previous tutorial we already discussed how to check if a specific coupon is applied to cart or to an order. But in this tutorial I will show you a different thing – how to find out programmatically if coupon with a specific code is actually created in WooCommerce admin.
The inspiration for this tutorial was another tutorial I found where it was recommended to create WooCommerce coupons using
wp_insert_post() function and
update_post_meta(). When working with WooCommerce data since 3.0 version it is always recommended to use CRUD objects. And coupons are not an exception.
In order to create a coupon programmatically in code we have to use
WC_Coupon object. Once the object is created we are able to use its methods for coupon configuration.
Let’s do it and begin with something simple.