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 (including fees and discounts)
Cart::clear();

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

// Get subtotal (items only)
$subtotal = Cart::subtotal();

// Add fees
Cart::addFee('shipping', 10.00);
Cart::addFee('handling', 5.00);

// Check if fees exists
Cart::hasFee('shipping'); // true
Cart::hasFee('non-existant'); // false

// Remove fees
Cart::removeFee('shipping');

// Get all fees
$fees = Cart::getFees();

// Get a specific fee
$fee =  Cart::getFee('shipping');

// Get total fees
$totalFees = Cart::totalFees();

// Add discounts
Cart::addDiscount('SUMMER10', 10.00);
Cart::addDiscount('WELCOME5', 5.00);

// Check if discount exists
Cart::hasDiscount('SUMMER10'); // true
Cart::hasDiscount('non-existant'); // false

// Remove discounts
Cart::removeDiscount('SUMMER10');

// Get all discounts
$discounts = Cart::getDiscounts();

// Get a specific discount
$discount = Cart::getDiscount('SUMMER10');

// Get total discounts
$totalDiscounts = Cart::totalDiscounts();

// Get cart total (subtotal + fees - discounts)
$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'
    ]
]

// Adding fees
Cart::addFee('shipping', 10.00, 'optional description');
Cart::addFee('handling', 5.00);

// Adding discounts
Cart::addDiscount('SUMMER10', 10.00, 'optional description');
Cart::addDiscount('WELCOME5', 5.00);

// Removing fees/discounts
Cart::removeFee('shipping');
Cart::removeDiscount('SUMMER10');

// Getting all fees/discounts
$fees = Cart::getFees(); // Returns Collection of fees
$discounts = Cart::getDiscounts(); // Returns Collection of discounts

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