PHP code example of tomshaw / shopcart

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

    

tomshaw / shopcart example snippets


use TomShaw\ShopCart\{Cart, CartItem};

$cartItem = CartItem::make(id: $product->id, name: $product->name, quantity: 1, price: $product->price);

Cart::add($cartItem);

$cartItem = CartItem::make($product->id, $product->name, 1, $product->price);

$cartItem->options['size'] = 'XL';
$cartItem->options['logo'] = 'Laravel Rocks';

Cart::add($cartItem);

// Array access
$size = $cartItem->options['size'];

// Or using Collection methods
$size = $cartItem->options->get('size');
$cartItem->options->put('color', 'blue');
$hasSize = $cartItem->options->has('size');

$cartItem = Cart::all()->where('id', '===', $id)->first();

$cartItem->quantity = 5;
$cartItem->options['size'] = '2XL';

Cart::update($cartItem);

Cart::remove(Cart::get($rowId));

Cart::forget();

$totalPrice = Cart::total('price');

$totalQuantity = Cart::total(property: 'quantity', numberFormat: false);

$subTotal = Cart::total('subtotal');

$totalTax = Cart::total('tax');

$cartItem = CartItem::make(id: 1, name: 'Product', quantity: 2, price: 100.00, tax: 8.5);

// Automatically computed properties
echo $cartItem->subTotal;    // 200.00 (quantity * price)
echo $cartItem->totalTax;    // 17.00 (subTotal * tax / 100)
echo $cartItem->totalPrice;  // 217.00 (subTotal + totalTax)

// Values update automatically when properties change
$cartItem->quantity = 3;
echo $cartItem->subTotal;    // 300.00 (automatically recalculated)

use TomShaw\ShopCart\{Cart, CartItem};

Cart::add(CartItem::make(tax: 6.250, ...));

$cartItem = Cart::get($rowId);

$boolean = Cart::has($rowId);

$cartItems = Cart::all(bool $toArray = false);

// Search for specific cart items
$cartItems = Cart::all()->where('id', '===', $productId);

// Check if cart is empty
$isEmpty = Cart::all()->isEmpty();
$isNotEmpty = Cart::all()->isNotEmpty();

// Filter, map, or use any Collection method
$total = Cart::all()->sum('totalPrice');
$firstItem = Cart::all()->first();

Cart::toArray();

Cart::toJson();

php artisan vendor:publish --provider="TomShaw\ShopCart\Providers\ShopCartServiceProvider" --tag=config