Query a customer’s generated coupons

This helper returns coupon IDs for an AutomateWoo customer. The limit is capped to avoid an unbounded query on stores with many coupons.

Developer note:

Add custom PHP in a small custom plugin or child theme, and test it on a staging site before production. This example requires ongoing maintenance when extension APIs change.

functions.php

<?php

/**
 * Get AutomateWoo-generated coupon IDs for a user.
 *
 * @param int $user_id WordPress user ID.
 * @param int $limit   Maximum coupons to return.
 * @return int[]
 */
function my_store_get_automatewoo_coupon_ids( $user_id, $limit = 100 ) {
	$customer = AutomateWoo\Customer_Factory::get_by_user_id( absint( $user_id ), false );

	if ( ! $customer ) {
		return array();
	}

	$query = new WP_Query(
		array(
			'post_type'              => 'shop_coupon',
			'post_status'            => 'any',
			'posts_per_page'         => min( 500, max( 1, absint( $limit ) ) ),
			'fields'                 => 'ids',
			'no_found_rows'          => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
			'meta_query'             => array(
				array(
					'key'   => '_is_aw_coupon',
					'value' => '1',
				),
				array(
					'key'   => '_aw_customer_id',
					'value' => $customer->get_id(),
				),
			),
		)
	);

	return array_map( 'absint', $query->posts );
}
Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.