WC_Coupon_Data_Store_CPT::add_coupon_used_by()privateWC 1.0

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

Метод класса: WC_Coupon_Data_Store_CPT{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// private - только в коде основоного (родительского) класса
$result = $this->add_coupon_used_by( $coupon, $used_by, $coupon_held_key );
$coupon(WC_Coupon) (обязательный)
Coupon object.
$used_by(строка) (обязательный)
Either user ID or billing email.
$coupon_held_key(строка) (обязательный)
(Optional) Update meta key to _used_by instead of adding a new record.

Код WC_Coupon_Data_Store_CPT::add_coupon_used_by() WC 8.7.0

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