PHP code example of ibrand / laravel-shopping-cart

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

    

ibrand / laravel-shopping-cart example snippets


'storage' => \iBrand\Shoppingcart\Storage\DatabaseStorage::class,
  
'storage' => \iBrand\Shoppingcart\Storage\SessionStorage::class,

Item | null Cart::add(
                    string | int $id,
                    string $name,
                    int $quantity,
                    int | float $price
                    [, array $attributes = []]
                 );

$row = Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
// Item:
//    id       => 37
//    name     => 'Item name'
//    qty      => 5
//    price    => 100.00
//    color    => 'red'
//    size     => 'M'
//    total    => 500.00
//    __raw_id => '8a48aa7c8e5202841ddaf767bb4d10da'
$rawId = $row->rawId();// get __raw_id
$row->qty; // 5
...

Item Cart::update(string $rawId, int $quantity);
Item Cart::update(string $rawId, array $arrtibutes);

Cart::update('8a48aa7c8e5202841ddaf767bb4d10da', ['name' => 'New item name');
// or only update quantity
Cart::update('8a48aa7c8e5202841ddaf767bb4d10da', 5);

Collection Cart::all();

$items = Cart::all();

Item Cart::get(string $rawId);

$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');

boolean Cart::remove(string $rawId);

Cart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

boolean Cart::destroy();
boolean Cart::clean(); // alias of destroy();

Cart::destroy();// or Cart::clean();

int | float Cart::total(); // alias of totalPrice();
int | float Cart::totalPrice();

$total = Cart::total();
// or
$total = Cart::totalPrice();

int Cart::countRows();

Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(127, 'foobar', 15, 100.00, ['color' => 'green', 'size' => 'S']);
$rows = Cart::countRows(); // 2

int Cart::count($totalItems = true);

Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
$count = Cart::count(); // 11 (5+1+5)

Collection Cart::search(array $conditions);

$items = Cart::search(['color' => 'red']);
$items = Cart::search(['name' => 'Item name']);
$items = Cart::search(['qty' => 10]);

bool Cart::isEmpty();

Cart Cart::associate(string $modelName);

Cart::associate('App\Models\Product');
$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->product->name; // $item->product is instanceof 'App\Models\Product'

Event::on('cart.adding', function($attributes, $cart){
    // code
});

php artisan vendor:publish --provider="iBrand\Shoppingcart\ServiceProvider"