WC_Coupon_Data_Store_CPT::add_coupon_used_by()
Helper function to add a _used_by record to track coupons used by the user.
Method of the class: WC_Coupon_Data_Store_CPT{}
No Hooks.
Return
null
. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->add_coupon_used_by( $coupon, $used_by, $coupon_held_key );
- $coupon(WC_Coupon) (required)
- Coupon object.
- $used_by(string) (required)
- Either user ID or billing email.
- $coupon_held_key(string) (required)
- (Optional) Update meta key to _used_by instead of adding a new record.
WC_Coupon_Data_Store_CPT::add_coupon_used_by() WC Coupon Data Store CPT::add coupon used by code WC 9.4.2
private function add_coupon_used_by( $coupon, $used_by, $coupon_held_key ) { global $wpdb; if ( $coupon_held_key && '' !== $coupon_held_key ) { // Looks like we added a tentative record for this coupon getting used. // Lets change the tentative record to a permanent one. $result = $wpdb->query( $wpdb->prepare( " UPDATE $wpdb->postmeta SET meta_key = %s, meta_value = %s WHERE meta_key = %s LIMIT 1", '_used_by', $used_by, $coupon_held_key ) ); if ( ! $result ) { // If no rows were updated, then insert a `_used_by` row manually to maintain consistency. add_post_meta( $coupon->get_id(), '_used_by', strtolower( $used_by ) ); } } else { add_post_meta( $coupon->get_id(), '_used_by', strtolower( $used_by ) ); } $this->refresh_coupon_data( $coupon ); }