WC_Coupon_Data_Store_CPT::delete │ public │ WC 3.0.0

Deletes a coupon from the database.

Method of the class: WC_Coupon_Data_Store_CPT{}

Returns

null. Nothing (null).

Usage

$WC_Coupon_Data_Store_CPT = new WC_Coupon_Data_Store_CPT();
$WC_Coupon_Data_Store_CPT->delete( $coupon, $args );
$coupon(WC_Coupon) (required) (passed by reference — &)
Coupon object.
$args(array)
Array of args to pass to the delete method.
Default: array()

Changelog

Since 3.0.0 Introduced.

WC_Coupon_Data_Store_CPT::delete() code WC 11.3.0

public function delete( &$coupon, $args = array() ) {
	$args = wp_parse_args(
		$args,
		array(
			'force_delete' => false,
		)
	);

	$id = $coupon->get_id();

	if ( ! $id ) {
		return;
	}

	if ( $args['force_delete'] ) {
		wp_delete_post( $id );

		/*
		 * The `deleted_post` listener of CouponCodeLookupInvalidator covers this for the core
		 * data store, but it only invalidates published coupons, the only status that store
		 * caches. A custom data store that resolves further statuses caches those too and skips
		 * the read-time check, so this delete is what keeps its force deletes covered.
		 *
		 * The edit context is the code that was written to `post_title`, and the one the hooks
		 * invalidate from. The view context would run it through the
		 * `woocommerce_coupon_get_code` filter and could point the delete at another key.
		 */
		wc_get_container()->get( CouponCodeLookupInvalidator::class )->invalidate( (string) $coupon->get_code( 'edit' ) );

		$coupon->set_id( 0 );
		do_action( 'woocommerce_delete_coupon', $id );
	} else {
		wp_trash_post( $id );
		$coupon->set_status( 'trash' );
		do_action( 'woocommerce_trash_coupon', $id );
	}
}