WooCommerce Developers: Creating a Custom REST Endpoint to Retrieve Coupons by Email

September 15, 2023

In the ever-evolving world of e-commerce, the ability to integrate and automate various aspects of your online store is essential. WooCommerce, the popular WordPress plugin, provides a robust REST API that allows developers to extend its functionality. However, there are times when you need a custom solution, like retrieving coupons assigned to specific users by passing their registered email. In this article, we’ll walk you through creating a custom WooCommerce REST endpoint to achieve just that.

The Challenge WooCommerce developers face: Accessing User-Specific Coupons

WooCommerce offers a wide range of features, but when it comes to retrieving coupons assigned to users via the REST API, you might find yourself hitting a roadblock. The standard WooCommerce REST API doesn’t provide this functionality out of the box. This is where a custom endpoint becomes invaluable, especially if you want to integrate your WooCommerce store with third-party applications for automation or build a mobile app.

The Solution: A Custom WooCommerce REST Endpoint

To access coupons by email, we’ll create a custom WooCommerce REST endpoint. This endpoint will accept a user’s registered email as a parameter and return the relevant coupons. Let’s dive into the code and see how it’s done.

function get_coupons_by_email($data){
	global $wpdb;
	$customer_email = $data->get_param('customer_email');
	$coupons = [];
	if(! empty($customer_email)){
          $coupons = $wpdb->get_results( $wpdb->prepare("
	  SELECT p.*, pm.meta_value as coupon_amount
	  FROM {$wpdb->prefix}posts p
	  INNER JOIN {$wpdb->prefix}postmeta pm
          ON p.ID = pm.post_id
          INNER JOIN {$wpdb->prefix}postmeta pm2
          ON p.ID = pm2.post_id
	  WHERE p.post_type = 'shop_coupon'
	  AND p.post_status = 'publish'
	  AND pm.meta_key = 'coupon_amount'
          AND pm2.meta_key ='customer_email'
	  AND pm2.meta_value LIKE '%s'
	  GROUP BY p.post_name
	  ORDER BY p.post_name DESC
	  ",'%'.$customer_email.'%' ) );

	return $coupons;

add_action( 'rest_api_init', function () {
  register_rest_route( 'wc/v3', '/coupons/assignments', array(
    'methods' => 'GET',
    'callback' => 'get_coupons_by_email',
  ) );
} );

In this code snippet, we define the get_coupons_by_email function that handles the request to our custom endpoint. It takes the user’s email as a parameter and queries the WooCommerce database to fetch the assigned coupons. These coupons are returned as a JSON response, making it easy to integrate into third-party applications or mobile apps.

How to Use the Custom Endpoint

Now that we have our custom WooCommerce REST endpoint in place, here’s how you can use it:

  • Endpoint URL: To retrieve coupons for a specific user, make a GET request to the following endpoint:

Replace user@example.com with the user’s registered email.

  • Response: The endpoint will return a JSON response containing information about the coupons assigned to the user, including coupon codes and their respective amounts.

Creating a custom WooCommerce REST endpoint to retrieve coupons by email is a powerful tool for WooCommerce store owners and developers. It enables seamless integration with third-party applications and mobile apps, enhancing automation and user experience. By following the code provided in this article, you can extend the capabilities of your WooCommerce store and provide a more customised shopping experience for your customers.



Digital Agency Cape Town

© 2023 | Trinity Managed Solutions (PTY) LTD
Privacy Policy