PHP code example of yzh52521 / think-shop-cart

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

    

yzh52521 / think-shop-cart example snippets



return [
    // Session初始化
    \think\middleware\SessionInit::class,
];

'storage' => \yzh52521\ShoppingCart\storage\DatabaseStorage::class,
  
'storage' => \yzh52521\ShoppingCart\storage\SessionStorage::class,

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

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

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

Collection ShopCart::all();

$items = ShopCart::all();

Item ShopCart::get(string $rawId);

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

boolean ShopCart::remove(string $rawId);

ShopCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

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

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

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

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

int ShopCart::countRows();

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

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

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

Collection ShopCart::search(array $conditions);

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

bool ShopCart::isEmpty();

Cart ShopCart::associate(string $modelName);

ShopCart::associate('app\model\Goods');
$item = ShopCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->goods->name; // $item->goods is instanceof 'app\model\Goods'

ShopCart::associate('app\model\Goods');
ShopCart::name('web.12');
$item = ShopCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->goods->name; // $item->goods is instanceof 'app\model\Goods'

php think tauthz:publish

php think migrate:run