Prevent the possibility of unpublished coupons from being available (#47739)

* Clean up the cache when a coupon is updated

When the coupon’s status transitions from publish to future, the ‘coupon_id_from_code’ cache entry needs to be deleted otherwise the coupon will remain available for use.

* Remove coupon cache entries when not published

* Add changefile(s) from automation for the following project(s): woocommerce

---------

Co-authored-by: Toby Herbert <tobyherbert@nialtoservices.co.uk>
Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Toby Herbert 2024-05-29 23:31:02 +01:00 committed by GitHub
parent f56d3e3ee0
commit 0f21660277
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Possible availability of unpublished coupons on sites with an object cache has been addressed through improved cache management.

View File

@ -190,6 +190,12 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
$this->update_post_meta( $coupon );
$coupon->apply_changes();
delete_transient( 'rest_api_coupons_type_count' );
// The `coupon_id_from_code` entry in the object cache must not exist when the coupon is not published, otherwise the coupon will remain available for use.
if ( 'publish' !== $coupon->get_status() ) {
wp_cache_delete( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $coupon->get_code(), 'coupons' );
}
do_action( 'woocommerce_update_coupon', $coupon->get_id(), $coupon );
}