laravel - How to add "cart" and "cart lines" of Mailchimp API 3.0 to custom store using php?

We're integrating Mailchimp API 3 (ecommerce) to a custom store and having some issues on doing the "cart" and "cart lines".

  1. What is the proper way to add a cart on Mailchimp using php? I think I'm missing some body parameter.
  2. Also, why does Mailchimp requires "cart lines" (An array of the cart's line items) on adding cart (using addStoreCart function) and require cart_id on creating cart-line (using addCartLineItem function)?

Here is what I've done so far. I am using composer require mailchimp/marketing package to connect my custom app to mailchimp.


namespace App\Http\Controllers\MailChimp;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use \MailchimpMarketing;

class CartController extends Controller
{
    public function store(Request $request)
    {

        $client = new MailchimpMarketing\ApiClient();
        $client->setConfig([
            'apiKey' => env('MAILCHIMP_API_KEY'),
            'server' => env('MAILCHIMP_SERVER')
        ]);
        /*ADD CART TO STORE */
        $response = $client->ecommerce->addStoreCart(env('MAILCHIMP_STORE_ID'), [
            "id" => "1",
            "currency_code" => "USD",
            "order_total" => 0,
            "customer" => ["id" => "17"],
            "lines" => [[]],
        ]);
        print_r($response);
    }
}

We are following this: https://mailchimp.com/developer/api/marketing/ecommerce-carts/

the error is not particular on postman

[Client error - POST https://us17.api.mailchimp.com/3.0/ecommerce/stores/2/carts resulted in a 400 Bad Request... ][1] [1]: https://i.stack.imgur.com/RFL4n.png

Thanks!

Answer

Solution:

$response = $mailchimp->ecommerce->addStoreCart("store_id", [
"id" => "3",
"customer" => ["id" => "1"],
"currency_code" => "USD",
"order_total" => 1247,
"lines" => [
    [
        "id" => "1",
        "product_id" => "10709",
        "product_variant_id" => "1",
        "quantity" => 1,
        "price" => 607,
    ],[
        "id" => "2",
        "product_id" => "10712",
        "product_variant_id" => "1",
        "quantity" => 1,
        "price" => 640,
    ],
],
"tax_total" => "0",

]);

Source