PHP code example of overtrue / laravel-shopping-cart

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

    

overtrue / laravel-shopping-cart example snippets


Overtrue\LaravelShoppingCart\ServiceProvider::class,

'ShoppingCart'      => Overtrue\LaravelShoppingCart\Facade::class,

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

$row = ShoppingCart::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 ShoppingCart::update(string $rawId, int $quantity);
Item ShoppingCart::update(string $rawId, array $arrtibutes);

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

Collection ShoppingCart::all();

$items = ShoppingCart::all();

Item ShoppingCart::get(string $rawId);

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

boolean ShoppingCart::remove(string $rawId);

ShoppingCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

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

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

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

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

int ShoppingCart::countRows();

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

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

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

Collection ShoppingCart::search(array $conditions);

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

bool ShoppingCart::isEmpty();

Cart ShoppingCart::associate(string $modelName);

ShoppingCart::associate('App\Models\Product');

ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);

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

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