0

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"];
3
  • 1
    have you tested this manually? Just to confirm, which URL are you expecting this to be? There's very little information about what it is that you needed to produce. Is it working but doing the wrong thing? Please be more specific! Also we can't help you with the WooCommerce parts of the question, WooCommerce and other 3rd party plugin dev support questions are offtopic here and not in this stacks scope
    – Tom J Nowell
    Commented Jun 15, 2022 at 14:21
  • i tested it manually. everytime im adding a new subscriber to a mailing list, the automation i built in moosend is calling this webhook: sanaleo.com/wp-json/webhook/newMoosEndSubscriber -- since the coupon is created in woocommerce i assume that the webhook is generally working. the only thing i cant get to work is to access the data of the request body. apparently, moosend is sending the email address of the new subscriber with the http request and i want to store this email address in a variable. Commented Jun 15, 2022 at 15:13
  • It sounds like you need to log the request data that you're receiving, your code assumes the entire data structure is an array, but the schema says some parts of it are actually objects. I'm not seeing how general WordPress knowledge can help you here though
    – Tom J Nowell
    Commented Jun 15, 2022 at 16:25

1 Answer 1

0

As you said "Coupon is created in WooCommerce end", we can confirm that custom endpoint "webhook" is working finely.

The issue is regrading to retrieve Email Address from the response. The moosend schema body is consist of JSON Object. So you have to decode the response to array first.

Try to change your code as following code. It will convert JSON Objects into PHP Array and then retrieve Email Address from the Array

$requestArr = json_decode($request, true);
$email = $requestArr["properties"]["Event"]["ContactContext"]["properties"]["EmailAddress"];
3
  • hey thanks for your answer! i tried your code but there was one issue: the json_decode function was expecting a string. the $request variable was a WP_REST_Request Object, so i removed "WP_REST_Request" from the function input and only left the $request variable in there-- after that i tried your code but it still doesnt seem to read the data from the json correctly. did you have a look at the schema? anyways, thanks for your help! Commented Jun 16, 2022 at 7:54
  • To retrieve data from WP_REST_Request Object we can use get_body(). So change your code as $requestArr = $request->get_body(); or try $requestArr = json_decode($request->get_body()); If get_body() is not worked try get_data(). Reference But I am still confused with your schema response. It has no values showing in in the responses.
    – Irfan
    Commented Jun 17, 2022 at 6:09
  • hey i found a different solution, i put it in my original post - the correct function to use would be get_json_params() -- thanks for your help anyway! Commented Jun 20, 2022 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.