PHP code example of riesenia / cart

1. Go to this page and download the library: Download riesenia/cart 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/ */

    

riesenia / cart example snippets


use Riesenia\Cart\Cart;

// default is ([], true, 2)
$cart = new Cart();

$cart->setContext(['customer_id' => $_SESSION['customer_id']]);
$cart->setPricesWithVat(false);
$cart->setRoundingDecimals(4);

// adding item to cart ($product has to implement CartItemInterface)
$cart->addItem($product);

// set quantity of the item when adding to cart
$cart->addItem($anotherProduct, 3);

// when $product->getCartId() returns i.e. 'abc'
$cart->setItemQuantity('abc', 7);

// remove item
$cart->removeItem('abc');

// item 1 [price: 1.00, tax rate: 10]
// item 2 [price: 2.00, tax rate: 20]

// 3.00
echo $cart->getSubtotal();

// 0.10
echo $cart->getTaxes()[10];

// 0.40
echo $cart->getTaxes()[20];

// 3.50
echo $cart->getTotal();

// get totals of type 'product'
echo $cart->getTotal('product');

// get totals of type 'product' and 'service'
echo $cart->getTotal('product,service');

// get totals of all items except type 'product' and 'service'
echo $cart->getTotal('~product,service');

$cart = new Cart();
$cart->addItem($product, 3);

// get unit price without VAT
echo $cart->getItemPrice($product, 1, false);

// get weight of type 'product'
echo $cart->getWeight('product');