i created a custom webhook endpoint to receive data from moosend. The idea is to get the email address of a new subscriber and create a new customized coupon in woocommerce. I created an automation on moosend which triggers the webhook successfully. The coupon is created but i cant figure out how to get the email address of the new subscriber from the data which is being sent. As far as I understand, the data is being sent via this schema:
http://cdn.stat-track.com/send-to-webhook-context-schema.js
This is my approach so far (i tried different methods but none has worked )
add_action('rest_api_init', function () {
register_rest_route('webhook', 'newMoosendSubscriber', array(
'methods' => ['POST'],
'callback' => 'createDiscountCouponForNewSubscriberME',
));
});
/**GENERATE COUPON */
function createDiscountCouponForNewSubscriberME(WP_REST_Request $request)
{
// create coupon
$email = $request["properties"]["Event"]["ContactContext"]["properties"]["EmailAddress"];
$coupon_code = 'welcome-' . $email;
$amount = '10'; // Amount
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product
$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);
//$exclude_products = array('61707', '61688', '61684', '61679', '61399');
// Add meta
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', 'yes');
update_post_meta($new_coupon_id, 'product_ids', '');
update_post_meta($new_coupon_id, 'exclude_product_ids', '61707, 61688, 61684, 61679, 61399');
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');
return "ok";
}
EDIT: I found the solution myself: to create a php array out of the json body of the http request, the function get_json_params() is the answer. To read the emailaddress from this array i had to use this:
$requestArr = $request->get_json_params();
$emailAddress = $request["Event"]["ContactContext"]["EmailAddress"];