Im tinkering with generating Woocommerce coupons triggered by a Mautic campaign webhook

Hi,
In case it is of interest to anyone, I am building a Woocommerce coupon code generator that is triggered from Mautic by a campaign webhook.
The idea is that a campaign triggers a GET-call to Woocommerce, which uses the variables provided in the GET to generate a coupon code. The campaign is of the sort that “If you have not shopped for 6 months, you’ll get this coupon code that gives you free shipping and a little discount”

This is as far as i’ve gotten this sunday morning;
I registered a shortcode that i placed on an otherwise empty page on the woocommerce installation, because that is the only way i know how.
Does anyone have a better suggestion how to call the function via an external GET-call?
I have “learned” the PHP i know by hammering the keys like a monkey on a typewriter until i don’t get fatal errors anymore, so please excuse the looks of the code, i’m sure it’s horrible.

What is provided by mautic is the parameter “6mwb”, 6 month win-back, for recovering lost customers, and the contact ID.
Todays date is generated by Woocommerce and used for the coupon syntax and for calculating validity time.
Coupon is generated with a set of parameters for its usage, and then it is flung back by a API cUrl call to the same contact in Mautic to a custom field, it all seems to be working.

Questions: Do you see timeout problems happening when i run this function and around 1600 api calls are made? Because i currently have around 800 contacts on the “Not shopped for 6 months”-list, and when i eventually launch this campaign,
all those calls will trigger at the same time. I see a timeout setting in Mautics campaign webhook sender, would that help me? Or can i add delay functions to the code?

There are still things to do, for example i want to have a similiar process run after 12 months, and i need to figure out a how to let mautic know that the code on the custom field is new.
Also, i have never experimented with campaigns with this sort of dynamic content (i want the newsletter to say “Here is your code: 6mw22052912345”) but i’ll cross that bridge when i get to it.

// register shortcode
add_shortcode(‘codeCreatorWebhook’, ‘codeCreatorWebhook_function’);

function codeCreatorWebhook_function() {

$couponCodeParams = sanitize_key($_GET[‘codetype’]); ////Get from URL, and trust but verify
$couponCodeContactID = sanitize_key($_GET[‘contactid’]); ////Get contact ID from URL, and trust but verify

echo “Debug:

Codeinput: $couponCodeParams

Contact ID input: $couponCodeContactID

”;

if ($couponCodeParams==“6mwb” and (!empty($couponCodeContactID)) ) { //Check that parameter 6mwb, 6-month-winback, is provided and that contact id is not empty

$todaysDate = date(“ymd”); //Get todays date to use in coupon code generation
$todaysdatesinunix = date(‘U’); //Get todays time in Unix for calculating coupon validity time
$aWeekFromNow=$todaysdatesinunix+691200; //Add eight days to todays date

//Check if coupon exists
if( wc_get_coupon_id_by_code( “$couponCodeParams$todaysDate$couponCodeContactID” ) ) {
echo ‘Debug: Coupon exists’;
} else {
echo ‘Debug: Coupon does not exist, keep doing stuff.
’;

echo “Debug: Coupon to be generated: $couponCodeParams$todaysDate$couponCodeContactID”;

////////////////////////////////////
//Create coupon code
$coupon_code = “$couponCodeParams$todaysDate$couponCodeContactID”; // Coupon to be added

$coupon = array(
‘post_title’ => $coupon_code,
‘post_content’ => ‘’,
‘post_status’ => ‘publish’,
‘post_author’ => 1,
‘post_type’ => ‘shop_coupon’);

//$new_coupon_id = wp_insert_post( $coupon ); //Actual adding of coupon

// Add coupon meta
update_post_meta( $new_coupon_id, ‘discount_type’, ‘percent’ ); // Type: fixed_cart, percent, fixed_product, percent_product
update_post_meta( $new_coupon_id, ‘coupon_amount’, ‘10’ ); // Amount
update_post_meta( $new_coupon_id, ‘minimum_amount’, ‘300’ ); // Minimum amount for coupon validity
update_post_meta( $new_coupon_id, ‘individual_use’, ‘yes’ );
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, ‘usage_limit_per_user’, ‘1’ );
update_post_meta( $new_coupon_id, ‘expiry_date’, $aWeekFromNow );
update_post_meta( $new_coupon_id, ‘free_shipping’, ‘yes’ );
update_post_meta( $new_coupon_id, ‘exclude_sale_items’, ‘yes’ );

////////////////////////////////////
//Call Mautic with the coupon
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => “https://MY-MAUTIC-INSTALLATION.COM/api/contacts/$couponCodeContactID/edit”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => ‘’,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => ‘PATCH’,
CURLOPT_POSTFIELDS => “chwoogeneratedcoupon=$coupon_code”,
CURLOPT_HTTPHEADER => array(
‘Cache-Control: no-cache’,
‘Content-Type: application/x-www-form-urlencoded’,
‘token: MY-TOKEN’,
‘Authorization: Basic MY-LOTS-OF-NUMBERS’
),
));

$response = curl_exec($curl);

curl_close($curl);
//echo $response;

}
}
}

Edit: If you see anything utterly horrible, please let me know!

Cheers, Carl from Sweden

1 Like