masterix21/laravel-cart

Cart features for any Laravel Project

1.0.6 2021-06-07 10:56 UTC

This package is auto-updated.

Last update: 2024-10-24 00:08:03 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require masterix21/laravel-cart

You can publish and run the migrations with:

php artisan vendor:publish --provider="Masterix21\LaravelCart\CartServiceProvider" --tag="laravel-cart-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="Masterix21\LaravelCart\CartServiceProvider" --tag="laravel-cart-config"

Usage

$product = YourProductModel::first();

//
// Add a product to cart
$cartItem = Cart::add(label: $product->name, item: $product, price: $product->price, quantity: 1);

//
// Change the cart item quantity
Cart::set($cartItem, 2); // or
$cartItem->setQuantity(2);

//
// Increase the cart item quantity
Cart::increase($cartItem); // to increase of 1
Cart::increase($cartItem, 5); // to increase of 5
$cartItem->increase(); // to increase of 1
$cartItem->increase(5); // to increase of 5

//
// Decrease the cart item quantity, and if the
// quantity is zero, removes the item from the cart.
Cart::decrease($cartItem); // to decrease of 1
Cart::decrease($cartItem, 5); // to decrease of 5
$cartItem->decrease(); // to decrease of 1
$cartItem->decrease(5); // to decrease of 5

// Retrieve all cart items
$items = Cart::items();

// Clear the cart
Cart::clear();

Views using Blade

Display the cart items counter that will auto-refresh on cart changes:

<!-- Automatic refresh the counter -->
<livewire:cart-counter />

<!-- Force the displayed value -->
<livewire:cart-counter :count="10" no-auto-refresh />

<!-- Customize the component using class argument like so -->
<livewire:cart-counter class="text-xs text-white rounded-full bg-red-700" />

<!-- Improve the result by its state -->
<livewire:cart-counter class="text-xs rounded-full" 
                       empty-class="bg-gray-100 text-gray-500" 
                       not-empty-class="bg-red-700 text-white" />

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.