PHP code example of ashraam / laravel-simple-cart

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

    

ashraam / laravel-simple-cart example snippets


use Ashraam\LaravelSimpleCart\Facades\Cart;

// Add an item to cart
Cart::add(
    id: 'product-1',
    name:'Product Name',
    price: 29.99,
    quantity:2,
    options: ['size' => 'L'],
    meta: ['image' => 'https://example.com/product-1.jpg', 'category' => 'T-shirt']
);

// Update quantity
Cart::update('item-id', 3);

// Remove item
Cart::remove('item-id');

// Clear cart
Cart::clear();

// Get cart contents
$items = Cart::content();

// Get cart total
$total = Cart::total();

// Get number of items in cart
$count = Cart::count();

// Check if item exists in cart
if (Cart::has('item-id')) {
    // Item exists
}

// Get a specific item from cart
$itemId = md5($productId . serialize($options)); // Generate item ID
$item = Cart::get($itemId); // Returns the item or null if not found

[
    'id' => 'product-1',      // Original product ID
    'name' => 'Product Name', // Product name
    'price' => 29.99,        // Product price
    'quantity' => 2,         // Quantity in cart©©
    'options' => [           // Options (used with the product id to generate the unique item hash id)
        'size' => 'L'
    ],
    'meta' => [              // Additional meta data
        'image' => 'https://example.com/product-1.jpg',
        'category' => 'T-shirt'
    ]
]

return [
    'session_key' => 'laravel_simple_cart'
];
bash
php artisan vendor:publish --provider="Ashraam\LaravelSimpleCart\LaravelSimpleCartServiceProvider"