CodeWP

How To Set Up Cross Website Coupon Codes on WooCommerce (No Plugin)

By James LePage, CodeWP Founder
December 6, 2022
Contents

In this quick article, I'll show you how we created a simple cross website coupon code setup for a WooCommerce shop.

Here's the use case.

Basically, when an order is successfully completed, we want to programmatically generate a coupon code on another WooCommerce shop (Shop 2), and restrict it to the billing email of the customer buying on Shop 1.

If only there was a simple way that didn't require 3rd party plugins or services... wait, there is!

We can use the built in WordPress REST API to send and receive data. We'll use it to tell site 2 to generate a coupon code when an order is successful on site 1. We'll also use the API to pass data like the value/type of coupon and restrict it to the email address that the customer has on site 1.

Send The Code Generation Request & Data

Prompt:// when an order is completed, send a post request to "https://example.com/wp-json/cwpai/v1/create-coupon" with the following content: email = user email, code = random string and amount = 10.

add_action( 'woocommerce_order_status_completed', 'cwpai_woo_order_completed_send_post_request' ); function cwpai_woo_order_completed_send_post_request( $order_id ) { $order = wc_get_order( $order_id ); $email = $order->get_billing_email(); $code = wp_generate_password( 10, false ); $amount = 10; $response = wp_remote_post( 'https://example.com/wp-json/cwpai/v1/create-coupon', array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => array( 'email' => $email, 'code' => $code, 'amount' => $amount ), 'cookies' => array() ) ); } Generation ID:cmpl-6KY8JfX4EQqobftlsP07JsX7mSrXk

This sends the request and needed data every time a WooCommerce order is completed.

Receive The Request, Make The Coupon

add_action( 'rest_api_init', 'cwpai_woo_register_api_hooks' ); function cwpai_woo_register_api_hooks() { register_rest_route( 'cwpai/v1', '/create-coupon', array( 'methods' => 'POST', 'callback' => 'cwpai_woo_create_coupon', ) ); } function cwpai_woo_create_coupon( $request ) { $email = $request['email']; $code = $request['code']; $amount = $request['amount']; $discount_type = $request['discount_type']; $coupon = array( 'post_title' => $code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon' ); $new_coupon_id = wp_insert_post( $coupon ); update_post_meta( $new_coupon_id, 'discount_type', $discount_type ); update_post_meta( $new_coupon_id, 'coupon_amount', $amount ); update_post_meta( $new_coupon_id, 'individual_use', 'no' ); update_post_meta( $new_coupon_id, 'product_ids', '' ); update_post_meta( $new_coupon_id, 'exclude_product_ids', '' ); update_post_meta( $new_coupon_id, 'usage_limit', '1' ); update_post_meta( $new_coupon_id, 'expiry_date', '' ); update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' ); update_post_meta( $new_coupon_id, 'free_shipping', 'no' ); update_post_meta( $new_coupon_id, 'customer_email', $email ); return $new_coupon_id; } Generation ID:cmpl-6KXpxSUx5AaXY41Xr4te77LUBF51e

Prompt:// make an api that creates a coupon code from post request, limited to email passed

If we send a POST request to https://example.com/wp-json/cwpai/v1/create-coupon with the necessary data, the site receiving that request will automatically create a coupon limited to the email sent in the request.

A simple, no frills way to share coupons across WooCommerce sites.

Snippets In This Tutorial Were Generated With CodeWP
php
PHP, JS or jQuery
select_all
AI models trained for WordPress & popular plugins
switch_access_shortcut
Unlimited generations
card_giftcard
100% free trial
Start For Free
link