1

I'm trying to use the DrewM\MailChimp PHP wrapper to batch update my MailChimp subscriber list. The main thing I'm trying to achieve is to sync the cover start and cover end dates with my already existing list. Here is my code:

$MailChimp = new MailChimp('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXX');
$Batch = $MailChimp->new_batch();
$list_id = "XXXXXXXXXX";

$orders = get_posts(array(
    'numberposts' => -1,
    'meta_key' => '_customer_user',
    'post_type' => 'shop_order',
    'post_status' => 'wc-completed'
));

$body = array();

foreach($orders as $order) {

        $data = new WC_Order($order->ID);
        $meta = get_post_meta($order->ID);
        $email_address = $data->get_billing_email();
        $subscriber_hash = md5(strtolower($email_address));

        // Cover Start Date Conversion
        $cover_start_date = $meta['coverstart'][0];
        $cover_start_date = DateTime::createFromFormat('d/m/Y', $cover_start_date);
        $cover_start = $cover_start_date->format('Y/m/d');

        // Cover End Date Conversion
        $cover_end_date = DateTime::createFromFormat('d/m/Y', $meta['coverstart'][0]);
        $cover_end_date->modify('-1 day');
        $cover_end_date->modify('+1 year');
        $cover_end = $cover_end_date->format('Y/m/d');

        $body[] = array(
            'email_address' => $email_address,
            'status'        => 'subscribed',
            'merge_fields' => [
                'FNAME' => $data->get_billing_first_name(),
                'LNAME' => $data->get_billing_last_name(),
                'COVERSTART' => $cover_start,
                'COVEREND' => $cover_end,
            ]
        );

    }

    $Batch->patch("op1", "lists/$list_id/members", $body);
    $Batch->execute();
    $status = $Batch->check_status();
    print_r($status);

I seem to get a 100% error rate and can only assume I'm doing it completely wrong. A little guidance would be much appreciated!

Here are the errors from the MailChimp API:

[{"status_code":405,"operation_id":"B0","response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Method Not Allowed\",\"status\":405,\"detail\":\"The requested method and resource are not compatible. See the Allow header for this resource's available methods.\",\"instance\":\"\"}"},{"status_code":405,"operation_id":"B1","response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Method Not Allowed\",\"status\":405,\"detail\":\"The requested method and resource are not compatible. See the Allow header for this resource's available methods.\",\"instance\":\"\"}"},{"status_code":405,"operation_id":"B2","response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Method Not Allowed\",\"status\":405,\"detail\":\"The requested method and resource are not compatible. See the Allow header for this resource's available methods.\",\"instance\":\"\"}"},{"status_code":405,"operation_id":"B3","response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Method Not Allowed\",\"status\":405,\"detail\":\"The requested method and resource are not compatible. See the Allow header for this resource's available methods.\",\"instance\":\"\"}"},{"status_code":405,"operation_id":"B4","response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Method Not Allowed\",\"status\":405,\"detail\":\"The requested method and resource are not compatible. See the Allow header for this resource's available methods.\",\"instance\":\"\"}"},{"status_code":405,"operation_id":"B5",

Liam McArthur
  • 1,033
  • 3
  • 18
  • 42
  • 100% errors? You need to tell us what at least one of those errors were. Check your php error_log – delboy1978uk Jun 27 '17 at 13:29
  • I've added the errors from the MailChimp API. There aren't any PHP errors in the error log file and I do have debugging enabled. It seems to be the way I'm actually attempting the request and misunderstanding how to perform the batch update. – Liam McArthur Jun 27 '17 at 13:32
  • 405 is method not allowed. should you be GETting, POSTing, PATCHing, or PUTting, etc? – delboy1978uk Jun 27 '17 at 13:35
  • I see you are running patch(). Do the docs say the API endpoint accepts that? – delboy1978uk Jun 27 '17 at 13:36
  • I know that. I'm using `$Batch->patch` - so it must be my code somewhere. – Liam McArthur Jun 27 '17 at 13:37
  • Looking at the API documentation, you may missing the email subscriber hash on your post request. `$MailChimp->patch("lists/$list_id/members/$subscriber_hash")` -- never mind, found the batch section – aynber Jun 27 '17 at 13:43
  • I'm going from the documentation here: https://github.com/drewm/mailchimp-api - it doesn't mention using the subscriber hash when performing batch updates. Only for individual updates. – Liam McArthur Jun 27 '17 at 13:44
  • The batch process uses post, the individual process uses patch. So maybe try post instead. – aynber Jun 27 '17 at 13:45
  • I've given up on batch and I'll just stick to posting each of them individually within the foreach rather than batch processing them. It just means the script takes longer. – Liam McArthur Jun 27 '17 at 14:18
  • @LiamMcArthur Did you tried [this](https://stackoverflow.com/a/34740306/4632218)? – Mûhámmàd Yäsår K Jul 05 '17 at 04:37

0 Answers0