After the order is set to completed, we trigger the mailerlight api to add the customer in a group
add_action('woocommerce_order_status_completed', 'add_customer_to_mailerlite_group');
function add_customer_to_mailerlite_group($order_id) {
$order = wc_get_order($order_id);
$email = $order->get_billing_email();
$name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$api_key = 'API_KEY';
$group_id = 'GROUP_ID';
$data = [
'email' => $email,
'name' => $name,
];
$response = wp_remote_post("https://api.mailerlite.com/api/v2/groups/{$group_id}/subscribers", [
'headers' => [
'Content-Type' => 'application/json',
'X-MailerLite-ApiKey' => $api_key,
],
'body' => json_encode($data),
]);
// Optional: Handle the response
if (is_wp_error($response)) {
error_log('MailerLite API WP_Error: ' . $response->get_error_message());
} else {
$status_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
error_log("MailerLite API response code: $status_code");
error_log("MailerLite API response body: $body");
}
}
Was this helpful?
YesNo