删除产品包时如何以编程方式删除 WooCommerce 优惠券?

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

我有一个自定义 post 类型 捆绑包 ,用于创建 woocommerce 产品捆绑包 。对于每个捆绑包,还有一个创建适用于该产品捆绑包的 "Discount Coupons" 的选项。

现在我想在删除捆绑包时删除与产品捆绑包关联的任何优惠券。 (Bundle 是自定义 post 类型,Coupon 也是 woocommerce 中的 post 类型)

当优惠券链接到的捆绑包被删除时,如何从数据库中删除优惠券? 提前致谢!

你应该使用 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.