MailChimp API v3.0 – Add Store to List, Add Product to Store, Add Order to Campaign

This article shows how you can use MailChimp API v3.0 with PHP and CURL to add ecommerce data to your MailChimp campaign. When purchases happen in your shop then you can add your purchase order data to MailChimp to track purchases from your MailChimp Campaign. A MailChimp campaign is associated to any particular MailChimp list.

To add order data into MailChimp you need to follow the below steps sequentially:

1) First add store to mailchimp list.
2) Then add products to store in mailchimp.
3) And then we can add orders to mailchimp campaign.

If you try to add order data into mailchimp campaign without adding products to mailchimp store then you get error. Similarly, you need to first add store to mailchimp list. Only then you can add products to that store.

Developer Documentation on Get Started with the E-Commerce API

Add Store to Mailchimp List

You need an API Key to add store to a mailchimp list. This article shows how you can get an API key on MailChimp: About API Keys.

The API Key is in the format as xxxxxxxxxxxxxxxxxx-us13.

Here,
xxxxxxxxxxxxxxxxxx = some random number and letter
us13 = server name, it can be us2, us3, us10, etc.

The other necessary thing is the list ID to which we will be adding our store. Below is the procedure to get List ID in MailChimp:

Getting your list ID

The list ID is not the numeric ID of the list. It is a combination of random letter and number.

You will get the list ID from ‘List name and defaults’ page.

– Create a list on your MailChimp account
– Click on that list
– Click on the ‘Settings’ menu
– A dropdown with sub-menus is displayed
– Click on the ‘List name and defaults’ sub-menu.
– On that page, on the right side, you will find the list ID highlighted in red/maroon color.

This link can be helpful to get into the ‘List name and defaults’ page: Find your List ID

You need to post the store data to this URL: https://'.MAILCHIMP-SERVER.'api.mailchimp.com/3.0/ecommerce/stores

Here, MAILCHIMP-SERVER = Some random number and letter. It can be us2, us3, us10, etc.

Here is the code to add store to mailchimp list:


<?php
$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx-us3'; // YOUR API KEY
$server = 'us3.';  
$auth = base64_encode( 'user:'.$api_key );

// id = store id
$data = array(
    'id'      => '1',
    'list_id' => 'xxxxxxxxx',
    'name' => 'Your Store Name',
    'domain' => 'example.com',
    'email_address' => 'your-name@example.com',
    'currency_code' => 'USD'
    );

$json_data = json_encode($data);

// id = store id
/*$json_data = '{
  "id" : "1",
  "list_id" : "xxxxxxxxxxx",
  "name" : "Your Store Name",
  "domain" : "example.com",
  "email_address" : "your-name@example.com",
  "currency_code" : "USD"
}';*/

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/ecommerce/stores');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
    'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
 
$result = curl_exec($ch);
 
$result_obj = json_decode($result);
 
echo '<pre>'; 
print_r($result);
print_r($result_obj); 
echo '</pre>';
?>

Add Product to Store in MailChimp

You need to post the store data to this URL: https://'.MAILCHIMP-SERVER.'api.mailchimp.com/3.0/ecommerce/stores/1/products/

Here, MAILCHIMP-SERVER = Some random number and letter. It can be us2, us3, us10, etc.

Here is the code to add product to mailchimp store:


<?php
$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx-us3'; // YOUR API KEY
$server = 'us3.';
$auth = base64_encode( 'user:'.$api_key );

$json_data = '{
    "id": "PROD001", 
    "title": "Your Product Name", 
    "handle": "product-handle", 
    "url": "example.com", 
    "description": "Test Product", 
    "type": "Book",
    "vendor": "Your Company", 
    "image_url": "https://static.mailchimp.com/web/brand-assets/Freddie_OG.png", 
    "variants": [
        {
            "id": "PROD001A", 
            "title": "Your Product Variant Name", 
            "url": "", 
            "sku": "Jokes001", 
            "price": 45, 
            "inventory_quantity": 100, 
            "image_url": "https://static.mailchimp.com/web/brand-assets/Freddie_wink.png", 
            "backorders": "0", 
            "visibility": "visible", 
            "created_at": "2016-02-08T13:06:44+00:00", 
            "updated_at": "2016-02-08T13:06:44+00:00"
        },
        {
            "id": "PROD001B", 
            "title": "Your Product Variant Name", 
            "url": "", 
            "sku": "Jokes002", 
            "price": 45, 
            "inventory_quantity": 99, 
            "image_url": "https://static.mailchimp.com/web/brand-assets/bro_freddie.png", 
            "backorders": "0", 
            "visibility": "true", 
            "created_at": "2016-02-08T22:14:37+00:00", 
            "updated_at": "2016-02-08T22:22:38+00:00"
        }
    ]
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/ecommerce/stores/1/products/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
    'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
 
$result = curl_exec($ch);
 
$result_obj = json_decode($result);

echo '<pre>'; 
print_r($result);
print_r($result_obj); 
echo '</pre>';
?>

Here is the Reference Doc for Products

Add Order data to MailChimp Campaign

You need to post the store data to this URL: https://'.MAILCHIMP-SERVER.'api.mailchimp.com/3.0/ecommerce/stores/1/orders/

Here, MAILCHIMP-SERVER = Some random number and letter. It can be us2, us3, us10, etc.

Here is the code to add order to mailchimp campaign:


<?php 
$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx-us3'; // YOUR API KEY
$server = 'us3.'; 
$auth = base64_encode( 'user:'.$api_key );

$json_data = '{
  "id" : "1000000000043",
  "customer" : {
    "id" : "73",
    "email_address": "your-name@example.com", 
    "opt_in_status": true, 
    "company": "Your Company Name", 
    "first_name": "Mukesh", 
    "last_name": "Chapagain", 
    "orders_count": 1, 
    "total_spent": 45
  },
  "campaign_id" : "xxxxxxxxx",
  "checkout_url" : "example.com/checkout/onepage",
  "currency_code" : "USD",
  "order_total" : 45,
  "shipping_address" : {
    "address1" : "675 Ponce de Leon Ave NE",
    "address2" : "Suite 5000",
    "city" : "Atlanta",
    "province" : "GA",
    "province_code" : "30033"
  },
  "lines": [
     {
      "id" : "1000000000043",
      "product_id" : "PROD001",
      "product_title" : "Your Product Name",
      "product_variant_id" : "PROD001A",
      "product_variant_title" : "Your Product Variant Name",
      "quantity" : 1,
      "price" : 45
    }
  ]
}';
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/ecommerce/stores/1/orders/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
    'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
 
$result = curl_exec($ch);
 
$result_obj = json_decode($result);
 
echo '<pre>'; 
print_r($result);
print_r($result_obj); 
echo '</pre>';
?>

Here is the Reference Doc for Orders

Hope this helps. Thanks.