PHP code example of sumonwd / laravel-cart

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

    

sumonwd / laravel-cart example snippets


use Sumonwd\Cart\Facades\Cart;

// add(id, name, price, quantity = 1, attributes = [], asNewItem = false)
$rowId = Cart::add(1, 'Solitaire Ring', 1500.00, 2, [
    'size'  => '7',
    'image' => '/img/ring.png',
    'slug'  => 'solitaire-ring',
]);

// Adding the same id + attributes again increases the quantity.
// Pass asNewItem: true to always create a separate row.

Cart::update($rowId, 3);                 // new quantity (0 removes the row)
Cart::update($rowId, ['name' => 'New']); // or update fields
Cart::remove($rowId);
Cart::clear();

Cart::get($rowId);       // array|null
Cart::has($rowId);
Cart::content();         // Collection keyed by rowId
Cart::count();           // total quantity
Cart::countDistinct();   // number of rows
Cart::isEmpty();

Cart::setShipping(15);
Cart::subtotal();
Cart::discount();
Cart::tax();             // uses config('cart.tax_rate') on subtotal - discount
Cart::shipping();
Cart::total();

[
    'rowId' => '...', 'id' => 1, 'name' => 'Solitaire Ring',
    'price' => 1500.0, 'quantity' => 2, 'subtotal' => 3000.0,
    'attributes' => [...], 'image' => '/img/ring.png', 'slug' => 'solitaire-ring',
    'added_at' => '2026-09-25 10:00:00',
]

Cart::instance('saved')->add(2, 'Bracelet', 50.00);
Cart::instance('saved')->count();
Cart::instance()->count(); // back to "default"

$result = Cart::applyVoucher('WELCOME10');
// ['success' => true, 'message' => "Voucher 'WELCOME10' applied successfully!", 'voucher' => [...]]

Cart::getVouchers();
Cart::removeVoucher('WELCOME10'); // or removeVoucher() to remove all

'vouchers' => [
    'multiple' => false, // true allows stacking vouchers
    'codes' => [
        'WELCOME10' => ['percentage' => 10, 'max_discount' => 50],
        'FLAT20'    => ['discount' => 20, 'is_fixed' => true],
    ],
],

namespace App\Cart;

use App\Models\Voucher;
use Sumonwd\Cart\Contracts\VoucherResolverInterface;
use Sumonwd\Cart\Services\CartService;

class DatabaseVoucherResolver implements VoucherResolverInterface
{
    public function resolve(string $code, CartService $cart): ?array
    {
        $voucher = Voucher::active()->where('code', $code)->first();

        if (! $voucher || ! $voucher->isValid()) {
            return null;
        }

        return [
            'code' => $voucher->code,
            'percentage' => (float) $voucher->discount_percentage,
            'is_fixed' => false,
        ];
    }
}

use Sumonwd\Cart\Facades\Wishlist;

Wishlist::add($productId);                  // returns rowId
Wishlist::add($productId, $variantId, ['name' => 'Ring', 'price' => 99]);
Wishlist::toggle($productId);               // true = added, false = removed
Wishlist::has($productId);
Wishlist::remove($productIdOrRowId);
Wishlist::content();
Wishlist::count();
Wishlist::moveToCart($productIdOrRowId, 1);
Wishlist::clear();

'wishlist' => [
    'product_model' => App\Models\Product::class,
    'name_attribute' => 'name',
    'price_attribute' => 'price',
    'slug_attribute' => 'slug',
    'image_collection' => 'images', // Spatie Media Library models
    'image_attribute' => null,      // or a plain column, e.g. 'thumbnail'
],

$this->app->bind(
    \Sumonwd\Cart\Contracts\CartCalculatorInterface::class,
    \App\Cart\MyCalculator::class,
);

'flush' => [
    \Sumonwd\Cart\Services\CartService::class,
    \Sumonwd\Cart\Services\WishlistService::class,
],
bash
php artisan vendor:publish --tag=cart-config
php artisan vendor:publish --tag=cart-migrations
php artisan migrate