List all Customer Coupons in Woocommerce

//List all Customer Coupons in Woocommerce

List all Customer Coupons in Woocommerce

Recently I was tasked with figuring out how to do something that I figured would be easy but was deceptively hard – displaying all the coupons for a customer in woocommerce. A client wanted to show a user all their coupons, and surprisingly this wasn’t built into woocomerce already.

To solve the problem, I created a custom page template and ran a query on the wp_postmeta table to get the ids of all of the coupons that were linked to the customer’s email. Then I ran a WP Query to get the posts and used the get_post_meta function to display the various coupon meta fields and build the data.

Here is an overview of the technique using generic information.

 

Get The Current User Data

global $current_user, $woocommerce; //get current user and woocommerce global variables
get_currentuserinfo(); // get current logged in user info

$current_user = wp_get_current_user(); // grab the logged in user
$user_id = $current_user->ID; // grab the logged in users id
$email = $current_user->user_email; // grab the logged in user email

 

Run a query on the meta table, targeting the coupons linked to the customer’s email and create an array of the ids returned

// Run a query on the postmeta table to get the id of every coupon that has the email in the customer_email restrictions
$couponlist = $wpdb->get_results("SELECT `wp_postmeta`.`post_id`
FROM `wp_postmeta`
WHERE `wp_postmeta`.`meta_key` LIKE 'customer_email'
AND `wp_postmeta`.`meta_value` LIKE '%"
.$email."%'");
$couponarrayfinal = array( );  //Create an array of the ids so we can use wp_query to more quickly grab the data

// Add the ids to the array in a foreach loop
foreach( $couponlist as $key => $row) {

$value = $row->post_id;
$couponarrayfinal[] = $value ;
}

 

Create a Table To Display the Coupons

<b>Coupon</b><b>Description</b><b>Amount</b>

Run A WP Query to Get the Coupons from the array, include the post meta through get_post_meta

wp_reset_postdata(); // reset post data so we aren't getting other post info// Create the arguments for the post loop, in this case shop coupon, and post__in are the ones that grab all the correct coupons
$couponargs = array(
'post_type' =&gt; 'shop_coupon',
'post__in' =&gt; $couponarrayfinal,
'orderby' =&gt; 'title',
'order' =&gt; 'ASC',
'posts_per_page' =&gt; '-1');// Create a new query to run thruogh the arguments
$couponquery = new WP_Query($couponargs);// Create a second loop linked to that query
if ($couponquery-&gt;have_posts()) :
while($couponquery-&gt;have_posts()) :
$couponquery-&gt;the_post();
$amount = get_post_meta( get_the_ID(), 'coupon_amount', true ); // Use get_post_meta to pull in data by the meta_key
$type = get_post_meta( get_the_ID(), 'discount_type', true ); // Use get_post_meta to pull in data by the meta_key
$used = get_post_meta( get_the_ID(), '_used_by', false); //Use get_post_meta to pull in the array of all _used_by

Display the coupons

// To prevent showing the coupons that are already used, do an if else based on
// the _used_by post meta and the user id of the logged in user
if (in_array($user_id, $used)) { } else {
// Build each row with the loop
echo ""; // Display the coupon name and descriptions echo "";if ($type == 'percent') { // Use if else based on the type of discount to display it correctly
echo "";} else {echo "";
}echo "";}endwhile; // End the loop and reset the post data
endif;
wp_reset_postdata();

And close the table

<table cellpadding="5">
<thead></thead>
<tbody>
<tr>
<td>" . get_the_title() . "</td>
<td>" . get_the_excerpt() . "</td>
<td>" .$amount . "%</td>
<td>$" . money_format('%i',$amount) . "</td>
</tr>
</tbody>
</table>

 

Final Thoughts
You can use the above examples to build your own query to grab all the coupon data. You could run multiple queries to show both a used and unused table, or build in different visual representations of whether a coupon is used or not. I hope this helps you figure out how to display the coupons yourself. It would be phenomenal if this sort of functionality was built into woocoommerce in the future, but until then, building your own template or plugin to run the queries is the best solution available. A previous solution used nested queries to pull in the data, but wherever possible it is best to use WP Query or Woocommerce’s WC_Coupon class to speed up the queries. I defaulted to WP Query in this instance because WC Coupon didn’t return the data in the places I wanted to show it.

John Arcadian is the owner and Lead Developer for Bee Zen Web Design. He has a passion for creating incredible sites and loves working with clients to solve unique challenges. When he isn’t creating websites, he is writing books, doing art direction, or walking around in his kilt.