Merge carts - Fabric

Merge carts

cURL

curl --request POST \
  --url https://api.fabric.inc/v3/carts/{cartId}/actions/merge \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --header 'x-fabric-tenant-id: <x-fabric-tenant-id>' \
  --data '
{
  "secondaryCartId": "462c08f6-cecb-49fd-84e2-96bfa7b1c4c8",
  "mergeBehavior": {
    "attributes": "MERGE",
    "fees": "ALL",
    "adjustments": "ALL",
    "customerContext": {
      "id": "PRIMARY",
      "sessionId": "PRIMARY",
      "segments": "ALL",
      "attributes": "MERGE"
    },
    "item": {
      "behavior": "ALL",
      "maxQuantity": "ALL",
      "quantity": "SUM"
    },
    "coupons": "ALL",
    "addresses": "REFERENCED",
    "fulfillments": "REFERENCED"
  }
}
'

Python

import requests

url = "https://api.fabric.inc/v3/carts/{cartId}/actions/merge"

payload = {
    "secondaryCartId": "462c08f6-cecb-49fd-84e2-96bfa7b1c4c8",
    "mergeBehavior": {
        "attributes": "MERGE",
        "fees": "ALL",
        "adjustments": "ALL",
        "customerContext": {
            "id": "PRIMARY",
            "sessionId": "PRIMARY",
            "segments": "ALL",
            "attributes": "MERGE"
        },
        "item": {
            "behavior": "ALL",
            "maxQuantity": "ALL",
            "quantity": "SUM"
        },
        "coupons": "ALL",
        "addresses": "REFERENCED",
        "fulfillments": "REFERENCED"
    }
}
headers = {
    "x-fabric-tenant-id": "<x-fabric-tenant-id>",
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)

JavaScript

const options = {
  method: 'POST',
  headers: {
    'x-fabric-tenant-id': '<x-fabric-tenant-id>',
    Authorization: 'Bearer <token>',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    secondaryCartId: '462c08f6-cecb-49fd-84e2-96bfa7b1c4c8',
    mergeBehavior: {
      attributes: 'MERGE',
      fees: 'ALL',
      adjustments: 'ALL',
      customerContext: {id: 'PRIMARY', sessionId: 'PRIMARY', segments: 'ALL', attributes: 'MERGE'},
      item: {behavior: 'ALL', maxQuantity: 'ALL', quantity: 'SUM'},
      coupons: 'ALL',
      addresses: 'REFERENCED',
      fulfillments: 'REFERENCED'
    }
  })
};

fetch('https://api.fabric.inc/v3/carts/{cartId}/actions/merge', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));

PHP

<?php

$curl = curl_init();

curl_setopt_array($curl, [\
  CURLOPT_URL => "https://api.fabric.inc/v3/carts/{cartId}/actions/merge",\
  CURLOPT_RETURNTRANSFER => true,\
  CURLOPT_ENCODING => "",\
  CURLOPT_MAXREDIRS => 10,\
  CURLOPT_TIMEOUT => 30,\
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,\
  CURLOPT_CUSTOMREQUEST => "POST",\
  CURLOPT_POSTFIELDS => json_encode([\
    'secondaryCartId' => '462c08f6-cecb-49fd-84e2-96bfa7b1c4c8',\
    'mergeBehavior' => [\
        'attributes' => 'MERGE',\
        'fees' => 'ALL',\
        'adjustments' => 'ALL',\
        'customerContext' => [\
                'id' => 'PRIMARY',\
                'sessionId' => 'PRIMARY',\
                'segments' => 'ALL',\
                'attributes' => 'MERGE'\
        ],\
        'item' => [\
                'behavior' => 'ALL',\
                'maxQuantity' => 'ALL',\
                'quantity' => 'SUM'\
        ],\
        'coupons' => 'ALL',\
        'addresses' => 'REFERENCED',\
        'fulfillments' => 'REFERENCED'\
    ]\
  ]),\
  CURLOPT_HTTPHEADER => [\
    "Authorization: Bearer <token>",\
    "Content-Type: application/json",\
    "x-fabric-tenant-id: <x-fabric-tenant-id>"\
  ],\
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
?>

Golang

package main

import (
    "fmt"
    "strings"
    "net/http"
    "io"
)

func main() {

url := "https://api.fabric.inc/v3/carts/{cartId}/actions/merge"

payload := strings.NewReader("{\n  \"secondaryCartId\": \"462c08f6-cecb-49fd-84e2-96bfa7b1c4c8\",\n  \"mergeBehavior\": {\n    \"attributes\": \"MERGE\",\n    \"fees\": \"ALL\",\n    \"adjustments\": \"ALL\",\n    \"customerContext\": {\n      \"id\": \"PRIMARY\",\n      \"sessionId\": \"PRIMARY\",\n      \"segments\": \"ALL\",\n      \"attributes\": \"MERGE\"\n    },\n    \"item\": {\n      \"behavior\": \"ALL\",\n      \"maxQuantity\": \"ALL\",\n      \"quantity\": \"SUM\"\n    },\n    \"coupons\": \"ALL\",\n    \"addresses\": \"REFERENCED\",\n    \"fulfillments\": \"REFERENCED\"\n  }\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("x-fabric-tenant-id", "<x-fabric-tenant-id>")
    req.Header.Add("Authorization", "Bearer <token>")
    req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
    body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))
}

Example Response

200 OK

{
  "id": "c86f777b-1885-4ddf-961d-542ba80a69b8",
  "attributes": {
    "name": "wishlist"
  },
  "configuration": {
    "order": {
      "validate": {}
    },
    "product": {
      "cacheExpiry": 7776000,
      "behaviors": {
        "add": "REJECT",
        "update": "WARN",
        "get": "WARN",
        "cacheExpiry": "WARN"
      },
      "maxQuantity": {
        "limit": 100,
        "behaviors": {
          "add": "REJECT",
          "update": "WARN",
          "get": "WARN"
        }
      }
    }
  }
}

Possible Errors

400 Bad Request
{
  "type": "BAD_REQUEST",
  "message": "x-fabric-tenant-id is required"
}
404 Not Found
{
  "type": "CART_NOT_FOUND",
  "message": "Cart not found"
}

Authorizations