Get List of Coupons used by one Customer in all WooCommerce orders

The beauty of code snippets is their ability to save you time when developing a site.

If you are a WooCommerce developer you may find this snippet usefull for Coupons listing.

Have fun!

<?php
/*
 * ==============================================================
 * Display all coupons used by one customer
 * ==============================================================
 */

global $current_user,$woocommerce;

$current_user = wp_get_current_user();
$customer_id = $current_user->ID;

$args = array(
    'numberposts'     => -1,
    'meta_key'        => '_customer_user',
    'meta_value'	  => $customer_id,
    'post_type'       => 'shop_order',
    'post_status'     => 'publish'
);
$customer_orders = get_posts($args);

if ($customer_orders) :

			foreach ($customer_orders as $customer_order) :
				$order = new WC_Order();

				$order->populate( $customer_order );

				// Get the coupon array
				$couponR = $order->order_custom_fields['coupons'];
				
				foreach ($couponR as $singleCoupon) {
					echo $singleCoupon.'<br />';
				}
				
				
			endforeach;

else :
//no Coupon then...
endif;

 ?>

Comments are closed.

prevost.elvina@mailxu.com loats.melania