PHP code example of cartboss / cartboss-php

1. Go to this page and download the library: Download cartboss/cartboss-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cartboss / cartboss-php example snippets


$cartboss = new \CartBoss\Api\CartBoss("l33t87zd1n87e8712z87nzdqs87z1278dnz187wzne");

$cartboss->onAttributionIntercepted(function(Attribution $attribution) {
    print $attribution->isValid(); // bool
    print $attribution->getToken(); // n87e8712z87nzdqs87z12712z87nzdq78dnz187w
    
    
});

$cartboss->onCouponIntercepted(function(Coupon $coupon) {
    print $attribution->isValid(); // bool
    print $attribution->getType(); // PERCENTAGE|FIXED_AMOUNT|FREE_SHIPPING|CUSTOM
    print $attribution->getValue(); // float
    print $attribution->getCode(); // str
    
    // helper methods
    print $attribution->isFreeShipping(); // bool
    print $attribution->isPercentage(); // bool
    print $attribution->isFixedAmount(); // bool
    print $attribution->isCustom(); // bool
});

// Create AddToCart event
$event = new AddToCartEvent();


// Create contact
$contact = new Contact();
$contact->setPhone("1234567");
$contact->setCountry("US");
$contact->setFirstName("Jake");
$contact->setAcceptsMarketing(true); // in case your site supports "consent checkout field"

// Add contact to event
$event->setContact($contact);

// Create order
$order = new Order();
$order->setId("1001"); // id that uniquely represents cart/order in your local database 
$order->setValue(19.99); // float 2 decimals 
$order->setCurrency('EUR'); // currency code

// Set publicly accessible url to the script that can restore visitor's order from provided order id  
// https://www.your-site.com/cartboss-restore-cart.php?order_id=1001
// https://www.your-site.com/cartboss-restore-cart.php?order_hash=9n12998as7as798d791dn988jdsa (if you don't want to expose db order_id)
$order->setCheckoutUrl("https://site.com/cartboss/cart_restore.php?order_id=1001"); 

// Add order to event
$event->setOrder($order);

// Send event to CartBoss
try {
    $cartboss->sendOrderEvent($event);

} catch (EventValidationException $e) {
    var_dump($e->getMessage());

} catch (ApiException $e) {
    var_dump($e->getMessage());
}

// Create AddToCart event
$event = new PurchaseEvent();

// Attach attribution token, you have received through Attribution URL Interceptor when visitor visited your site 
// through SMS link.  
$event->setAttributionToken("...");

// Create contact
$contact = new Contact();
$contact->setPhone("1234567");
$contact->setCountry("US");
$contact->setFirstName("Jake");
$contact->setAcceptsMarketing(true); // in case your site supports "consent checkout field"

// Add contact to event
$event->setContact($contact);

// Create order
$order = new Order();
$order->setId("1001"); // id that uniquely represents cart/order in your local database 
$order->setValue(19.99); // float 2 decimals 
$order->setCurrency('EUR'); // currency code

// Add order to event
$event->setOrder($order);

// Send event to CartBoss
try {
    $cartboss->sendOrderEvent($event);

} catch (EventValidationException $e) {
    var_dump($e->getMessage());

} catch (ApiException $e) {
    var_dump($e->getMessage());
}