> For the complete documentation index, see [llms.txt](https://cartsguru.gitbook.io/custom-integration/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cartsguru.gitbook.io/custom-integration/manual-integration/php-sdk.md).

# PHP SDK

## Introduction

We have developed a ready to use PHP library to allow you to connect your shop with our service in less than one.&#x20;

## How install it ?

### Using composer

Our plugin is available via compose. Just run the following command to install it\
`composer require cartsguru/cartsguru`

### Using our git&#x20;

You can download the library follow this url\
[https://bitbucket.org/market-wave/cartsguru-lib-php](https://bitbucket.org/market-wave/cartsguru-lib-php/overview)

## How to use it

### #1 - Create a CartsGuruController class

Our SDK expose a CartsGuru class that you should extend to adapt it to your needs:\
\- override the constructor to set your API credentials.\
\- override adaptCart, adaptOrder and adaptProduct to convert your data into our API models.\
You can find documentation about [cart model](https://cartsguru.gitbook.io/custom-integration/carts-guru-integration/carts#data-model) and [order model](https://cartsguru.gitbook.io/custom-integration/carts-guru-integration/orders#data-model)

#### Example of class&#x20;

{% code title="cartsguru-controller.php" %}

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use CartsGuru\CartsGuru\CartsGuru as CartsGuru;

class CartsGuruController extends CartsGuru
{
    function __construct()
    {
        $params = array(
            'site_id' => 'your-site-id',
            'auth_key' => 'your-auth-key',
        );
        parent::__construct($params);
    }

    public function adaptCart($item)
    {
        return $item;
    }

    public function adaptOrder($item)
    {
        return $item;
    }

    public function adaptProduct($item)
    {
        return $item;
    }
}
```

{% endcode %}

### &#x20;#2 - Use it !

#### Send your cart&#x20;

```php
$cg = new CartsGuruController();
$cg->trackCart($cart);
```

A cart must be sent on each update.

#### Send your orders

```php
$cg = new CartsGuruController();
$cg->trackOrder($order);
```

An order must be sent when it is created or state change.

#### Add online retargeting feature&#x20;

You should insert the tag on each page that the visitor can access

```php
$cg = new CartsGuruController();
$context = array(
    'cart' => $cart,        // Everywhere when cart exists
    'order' => $order,      // Only on the order page confirmation
    'product' => $product,  // On product page
);

$cg->generateTag($context);
```

### FAQ

### How carts are identified ?

We must distinguish two kind of cart : identified and non identified.\
When the **visitor is logged** in, we speak about identified cart. You must set the **accountId** field with the user **email** on this case.\
When the **visitor is anonymous**, it's a non identified cart. And so, you should set the **visitorId** field using the value of the **cookie trkcg\_vid**
