Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
WC_Coupon_Data_Store_CPT::update_usage_count_meta( WC_Coupon $coupon, string $operation = 'increase' )
Increase or decrease the usage count for a coupon by 1.
Description Description
Parameters Parameters
- $coupon
-
(Required) Coupon object.
- $operation
-
(Optional) 'increase' or 'decrease'.
Default value: 'increase'
Return Return
(int) New usage count
Source Source
File: includes/data-stores/class-wc-coupon-data-store-cpt.php
private function update_usage_count_meta( &$coupon, $operation = 'increase' ) { global $wpdb; $id = $coupon->get_id(); $operator = ( 'increase' === $operation ) ? '+' : '-'; add_post_meta( $id, 'usage_count', $coupon->get_usage_count( 'edit' ), true ); $wpdb->query( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared "UPDATE $wpdb->postmeta SET meta_value = meta_value {$operator} 1 WHERE meta_key = 'usage_count' AND post_id = %d;", $id ) ); // Get the latest value direct from the DB, instead of possibly the WP meta cache. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'usage_count' AND post_id = %d;", $id ) ); }
Changelog Changelog
Version | Description |
---|---|
3.0.0 | Introduced. |