Skip to content
Advertisement

Loop with WooCommerce user coupon not working

I’m looping through WooCommerce coupons that are restricted to the logged in customer. The problem is that coupons saved with customer restriction have the post_meta “customer_email” sometimes with single values, sometimes with an array. When making a query with WP_QUERY I can’t get the coupons that are with the target “customer_email” in array format. Example of my code:

JavaScript

The code above only returns the coupons that were saved with the customer’s email uniquely, the coupons that the same email is in an array are not returned. If someone wonders why the email is saved in the customer_email meta, sometimes as unique and sometimes as an array, it happens because if the coupon is generated as only one email allowed, the value is unique, if it is created with more of an email it is saved as an Array. Any idea why my query doesn’t return all the coupons that contain the customer’s email?

Advertisement

Answer

Could you try this :

JavaScript

EDIT add explanation why IN doesn’t match in this case ‘customer_email’ is a string containing all emails separated by comma.

customer_email.value = email1,email2,email3

When we use IN, we are looking for exact corresponding value of DB value and each value we pass in array.

JavaScript

EDIT *** Other way to do

You should have a look to this thread : How to get coupons from email restrictions with efficiency in WooCommerce

Inspired from the above thread :

JavaScript

This function return all coupons codes restricted to the ‘$current_email’ (customer email).

In your script, if you need WC_Coupon object, you can retrieve it like this :

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement