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::add_coupon_used_by( WC_Coupon $coupon, string $used_by, string $coupon_held_key )

Helper function to add a _used_by record to track coupons used by the user.


Description Description


Parameters Parameters

$coupon

(Required) Coupon object.

$used_by

(Required) Either user ID or billing email.

$coupon_held_key

(Required) (Optional) Update meta key to _used_by instead of adding a new record.


Top ↑

Source Source

File: includes/data-stores/class-wc-coupon-data-store-cpt.php

	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 ) );
		}
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.