PHP code example of andreilungeanu / simple-cart

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

    

andreilungeanu / simple-cart example snippets


use AndreiLungeanu\SimpleCart\Facades\SimpleCart;
use AndreiLungeanu\SimpleCart\Cart\DTOs\CartItemDTO; // Updated namespace

// Create a new cart instance (returns an object for chaining)
$cart = SimpleCart::create(userId: 'user-123', taxZone: 'RO');
$cartId = $cart->getId(); // Get the unique ID

// Chain methods on the cart object to modify it
$cart->addItem([ // Add item using an array
        'id' => 'prod_456',
        'name' => 'Wireless Mouse',
        'price' => 25.50,
        'quantity' => 1
     ])
     ->addItem(new CartItemDTO( // Add item using DTO
        id: 'prod_123',
        name: 'Laptop Pro',
        price: 1299.99,
        quantity: 1
     ))
     ->updateQuantity('prod_123', 2) // Update quantity
     ->removeItem('prod_456');

// Get calculated values using the Facade and cart ID
$subtotal = SimpleCart::subtotal($cartId);
$total = SimpleCart::total($cartId);
$itemCount = SimpleCart::itemCount($cartId);
echo "Item Count: " . $itemCount;
echo "Subtotal: " . $subtotal;
echo "Total: " . $total;

// Retrieve the cart object later
$loadedCart = SimpleCart::find($cartId); // Returns cart object or null
if ($loadedCart) {
    echo "Cart found: " . $loadedCart->getId();
    // $cartInstance = $loadedCart->getInstance(); // Get underlying data if needed
}

// Clear the cart's contents
$loadedCart?->clear();

// Delete the cart entirely via the Facade
SimpleCart::destroy($cartId);

use AndreiLungeanu\SimpleCart\Facades\SimpleCart;
use AndreiLungeanu\SimpleCart\Cart\DTOs\CartItemDTO; // Updated namespace

// Create cart with a specific tax zone
$cart = SimpleCart::create(taxZone: 'RO');

// Add items
$cart->addItem([
        'id' => 'book_abc',
        'name' => 'Programming Guide',
        'price' => 45.00,
        'quantity' => 1,
        'category' => 'books' // Uses 5% VAT in RO zone
     ])
     ->addItem([
        'id' => 'elec_xyz',
        'name' => 'Monitor',
        'price' => 300.00,
        'quantity' => 1 // Uses default 19% VAT in RO zone
     ]);

// Get tax amount using Facade and ID
$tax = SimpleCart::taxAmount($cart->getId());
echo "Tax Amount: " . $tax;

// Mark the cart as VAT exempt
$cart->setVatExempt(true);

// Get tax amount again
$taxAfterExempt = SimpleCart::taxAmount($cart->getId());
echo "Tax Amount (Exempt): " . $taxAfterExempt; // 0.00

// Unset VAT exemption
$cart->setVatExempt(false);

use AndreiLungeanu\SimpleCart\Facades\SimpleCart;

// Create cart and add items first...
$cart = SimpleCart::create(taxZone: 'RO');
// $cart->addItem(...);

// Set the desired shipping method
$cart->setShippingMethod('express', ['vat_;

// Check if free shipping was applied
if (SimpleCart::isFreeShippingApplied($cart->getId())) {
    echo "Free shipping applied!";
}

// To remove shipping selection, set method to an empty string or null
$cart->setShippingMethod('', []);

use AndreiLungeanu\SimpleCart\Facades\SimpleCart;
use AndreiLungeanu\SimpleCart\Cart\DTOs\DiscountDTO; // Updated namespace

// Create cart and add items first...
$cart = SimpleCart::create();
// $cart->addItem(...);

// Apply discounts
$cart->applyDiscount([ // Apply using array
        'code' => 'WELCOME10',
        'type' => 'fixed',
        'value' => 10.00, // Use 'value'
     ])
     ->applyDiscount(new DiscountDTO( // Apply using DTO
        code: 'SUMMER20',
        type: 'percentage',
        value: 20.0 // 20%
     ));

// Get calculated discount amount using Facade and ID
$discountAmount = SimpleCart::discountAmount($cart->getId());
echo "Discount Amount: " . $discountAmount;

// Get total including discounts
$total = SimpleCart::total($cart->getId());
echo "Total: " . $total;

// Remove a discount by its code
$cart->removeDiscount('WELCOME10');

// Recalculate total
$totalAfterRemove = SimpleCart::total($cart->getId());
echo "Total after removing WELCOME10: " . $totalAfterRemove;

use AndreiLungeanu\SimpleCart\Facades\SimpleCart;
use AndreiLungeanu\SimpleCart\Cart\DTOs\ExtraCostDTO; // Updated namespace

// Create cart and add items first...
$cart = SimpleCart::create(taxZone: 'RO');
// $cart->addItem(...);

// Add extra costs
$cart->addExtraCost([ // Add using array
        'name' => 'Handling Fee',
        'amount' => 5.00,
        'type' => 'fixed'
     ])
     ->addExtraCost(new ExtraCostDTO( // Add using DTO
        name: 'Insurance',
        amount: 1.5, // 1.5% of subtotal
        type: 'percentage'
     ))
     ->addExtraCost([ // Add with specific VAT details
        'name' => 'Gift Wrapping',
        'amount' => 7.50,
        'type' => 'fixed',
        'vatRate' => 0.19,
        'vatIncluded' => true
     ]);

// Get total extra costs amount using Facade and ID
$extraCostsTotal = SimpleCart::extraCostsTotal($cart->getId());
echo "Extra Costs Total: " . $extraCostsTotal;

// Get total including extra costs
$total = SimpleCart::total($cart->getId());
echo "Total: " . $total;

// Remove an extra cost by name
$cart->removeExtraCost('Handling Fee');

// Recalculate total
$totalAfterRemove = SimpleCart::total($cart->getId());
echo "Total after removing Handling Fee: " . $totalAfterRemove;



use AndreiLungeanu\SimpleCart\Services\DefaultShippingProvider; // Correct - outside Cart domain
use AndreiLungeanu\SimpleCart\Services\DefaultTaxProvider; // Correct - outside Cart domain
use AndreiLungeanu\SimpleCart\Cart\Services\Persistence\DatabaseCartRepository; // Updated namespace

return [
    // Persistence settings
    'storage' => [
        // 'driver' => 'database', // Only database driver is currently implemented via CartRepository binding
        'repository' => DatabaseCartRepository::class, // Specify the repository implementation
        'ttl' => env('CART_TTL', 30 * 24 * 60 * 60), // 30 days in seconds (Note: TTL logic needs implementation in repository/cleanup job)
    ],

    // Tax calculation settings
    'tax' => [
        'provider' => DefaultTaxProvider::class, // Implement custom provider if needed
        'default_zone' => env('CART_DEFAULT_TAX_ZONE', 'US'), // Default tax zone if not specified via create()
        'settings' => [
            'zones' => [
                // Example: United States configuration
                'US' => [
                    'name' => 'United States',
                    'default_rate' => env('CART_US_TAX_RATE', 0.0725), // Default rate for US
                    // 'apply_to_shipping' => false, // This logic is now handled within CartCalculator
                    'rates_by_category' => [ // Category-specific rates
                        'digital' => 0.0,
                        'food' => 0.03,
                    ],
                ],
                // Example: Romania configuration
                'RO' => [
                    'name' => 'Romania',
                    'default_rate' => env('CART_RO_TAX_RATE', 0.19),
                    // 'apply_to_shipping' => true,
                    'rates_by_category' => [
                        'books' => 0.05,
                        'food' => 0.09,
                    ],
                ],
                // Add more zones as needed...
            ],
        ],
    ],

    // Shipping calculation settings
    'shipping' => [
        'provider' => DefaultShippingProvider::class, // Implement custom provider if needed
        'settings' => [
            'free_shipping_threshold' => env('CART_FREE_SHIPPING_THRESHOLD', 100.00), // Subtotal threshold for free shipping
            'methods' => [
                // Example: Standard shipping method
                'standard' => [
                    'cost' => env('CART_STANDARD_SHIPPING_COST', 5.99),
                    'name' => 'Standard Shipping',
                    // 'vat_
bash
php artisan vendor:publish --tag="simple-cart-config"
bash
php artisan vendor:publish --tag="simple-cart-migrations"
php artisan migrate