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

**URL:** <https://forum.mautic.org/t/im-tinkering-with-generating-woocommerce-coupons-triggered-by-a-mautic-campaign-webhook/24264>\
**Category:** Development\
**Created:** [May 29, 2022, 8:09am UTC](https://forum.mautic.org/t/im-tinkering-with-generating-woocommerce-coupons-triggered-by-a-mautic-campaign-webhook/24264 "2022-05-29T08:09:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![calle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/calle/32/6739_2.png) [@calle](https://forum.mautic.org/u/calle)\
**Post date:** [May 29, 2022, 8:09am UTC](https://forum.mautic.org/t/im-tinkering-with-generating-woocommerce-coupons-triggered-by-a-mautic-campaign-webhook/24264/1 "2022-05-29T08:09:05Z")

</div>

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](https://MY-MAUTIC-INSTALLATION.COM/api/contacts/%24couponCodeContactID/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
