I successfully subscribe user and send data to Mailchimp to populate a list using this code at woocommerce checkout.
When a user place an order on my website this code send successfully user details (name, address, city etc) but I need to send the order details too (product name, price etc). Mailchimp API guide is really basic and difficult to understand for me. I went crazy googling for some help with no luck. Thanks in advance!
What I have tried:
<?php
add_action( 'woocommerce_checkout_update_order_meta', 'mailchimp_checkout_update', 1, 2 );
function mailchimp_checkout_update() {
$api_key = 'xxxx';
$list_id = 'xxxx';
$billing_email = htmlentities( $_POST[ billing_email ] );
$fname = htmlentities( $_POST[ billing_first_name ] );
$lname = htmlentities( $_POST[ billing_last_name ] );
$address = htmlentities( $_POST[ billing_address_1 ] );
$phone = htmlentities( $_POST[ billing_phone ] );
$city = htmlentities( $_POST[ billing_city ] );
$args = array(
'method' => 'PUT',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:' . $api_key )
),
'body' => json_encode( array(
'email_address' => $billing_email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $fname,
'LNAME' => $lname,
'ADDRESS' => $address,
'PHONE' => $phone,
'CITY' => $city,
),
) )
);
$response = wp_remote_post( 'https://' . substr( $api_key, strpos( $api_key, '-' ) + 1 ) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5( strtolower( $billing_email ) ), $args );
$body = json_decode( $response[ 'body' ] );
}