4 – Pedlar-Cart Laravel Package

Installation

Add bobkingstone/pedlar-cart to your composer.json

"require": {
    "bobkingstone/pedlar-cart": "dev-master"
}

Run composer update

composer update

Once this has completed add the service provider to the array of providers in app/config/app.php

'Bobkingstone\PedlarCart\PedlarCartServiceProvider'

Usage

The package generates a ‘Cart’ facade for the package automatically so there is no need to add it to the alias array.

To add an item to the cart:

$item = array(
    'id => '1', //your cart id
    'qty' => 2,
    'price' => 200.00,
);

$CartItemIdentifier = Cart::insert($item);

To get the total number of all items in cart:

Cart::countItems();

To get an array of CartItems from cart:

Cart::all();

To access cart items values:

foreach (Cart::all() as $item)
{
    echo $item->id;
    echo $item->price;
    echo $item->qty;
};

To get the total value of all items in cart:

Cart::totalValue();

To get a count of unique items in cart:

Cart::totalUniqueItems();

To empty the cart:

Cart::clear();

To set tax rate, you can either add it to each item:

$item = array(
    'id => '1',
    'qty' => 2,
    'tax' => 20,
    'price' => 200.00,
);

Cart::totalWithTax();

Or pass in the percentage with the cart total calculation (this will override each items predefined tax rate):

Cart::totalWithTax(20);

To update an item

$item = Cart::find('91936a0df88d531b5a770b614cd3c1ea');

$item->update('qty','3');

This will replace the existing value, to add to the quantity add the same item:

$item = array(
        'id => '1',
        'qty' => 2,
        'price' => 200.00,
    );

Cart::insert($item);

The item quantity will automatically be added.

To remove a single item from cart:

Cart::remove('91936a0df88d531b5a770b614cd3c1ea');