phpwordpresspluginswoocommercewoothemes

How to delete a WooCommerce coupon programmatically when a product bundle is deleted?


I have a custom post type Bundles, to create woocommerce product bundles. With each Bundle, there is also an option to create "Discount Coupons" which applies to that Product Bundle.

Now I want to delete any associated coupon to a Product Bundle whenever a Bundle is deleted. (Bundle is a custom post type and Coupon is also a post type in woocommerce)

How can I delete a coupon from database when a Bundle, to which the coupon is linked to, is deleted? Thanks in advance!


Solution

  • You should use before_delete_post. Check if the deleted post is you product bundle post type and then query all coupons linked to this product bundle using get_posts with meta_key. If you have all these coupons just loop through them and delete them too using wp_delete_post.