0

We're updating our Mailchimp implementation from 1.3 to 3.0. We succesfully updated our code to subscribe someone to a list. Now we're trying to add an ecommerce order. In API v1.3 we did this with the function campaignEcommOrderAdd. I found the function to this with in v3.0: /ecommerce/stores/{store_id}/orders(website link).

But I can't get it to work. When posting to Mailchimp I get an 404 error, but I don't know what I'm doing wrong. Below is my code.

$apiKey = "xxx"; //xxx for privacy reasons

$json = json_encode(array(
    'id'            => $mailchimp_order['id'],
    'customer'      => array(
        'id'    => $mailchimp_order['email_id'],
    ),
    'campaign_id'   => $mailchimp_order['campaign_id'],
    'currency_code' => "EUR",
    'order_total'   => $mailchimp_order['total'],
    'tax_total'     => $mailchimp_order['tax'],
    'lines'         => $mailchimp_order['items'],
));

$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/ecommerce/stores/'.$mailchimp_order['store_id'].'/orders';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

This is the output of my $json var:

{
    "id":"10000003",
    "customer":{
        "id":"a90f52f710"
    },
    "campaign_id":"641657",
    "currency_code":"EUR",
    "order_total":"56.90",
    "tax_total":"47.02",
    "lines":[
        {
            "id":"224",
            "product_id":"4427",
            "product_title":"Product name",
            "product_variant_id":0,
            "quantity":"1",
            "price":"49.95"
        }
    ]
}

And this is the error I get:

object(stdClass) {
    type => 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/'
    title => 'Resource Not Found'
    status => (int) 404
    detail => 'The requested resource could not be found.'
    instance => ''
}
ekad
  • 14,436
  • 26
  • 44
  • 46
ZoFem
  • 309
  • 4
  • 20

1 Answers1

0

Without knowing more I would try a few things here. One (you may have already tried this, but check the output of $url) to make sure that all that is getting set correctly. Secondly I would make sure that the store instance you are posting this order to is reachable/exists by making a GET request to, what would be:

$url = 'https://'.$dataCenter.'.api.mailchimp.com/3.0/ecommerce/stores/'.$mailchimp_order['store_id']'

Lastly I would verify that both the campaign and product instances associated with the order are reachable using:

  • GET https://{dc}.api.mailchimp.com/3.0/campaigns/641657

  • GET https://{dc}.api.mailchimp.com/3.0/ecommerce/stores/{store_id}/products/4427

Also if you are doing a lot of updating to 3.0 for your app it might be useful to implement a library that abstracts out a lot of this code I use this one:

https://github.com/Jhut89/Mailchimp-API-3.0-PHP

My reputation score wont let me post more links to those endpoints but they should be easily found in the MailChimp documentation. Hope that helps out.

jhut89
  • 83
  • 5
  • 1
    Thanks for your comment! I can't seem to find the time to check this. I will eventually, but I wanted to thank you in advance for taking the time to answer my question – ZoFem Feb 03 '17 at 10:47
  • No problem friend! Hope it helps. – jhut89 Mar 17 '17 at 19:02