itemvirtual/ecommerce-cart

Shopping cart in Laravel

1.0.1 2024-07-02 12:05 UTC

This package is auto-updated.

Last update: 2024-11-02 12:54:40 UTC


README

Latest Version on Packagist Total Downloads

This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.

Installation

Install the package via composer:

composer require itemvirtual/ecommerce-cart

Publish config (with --force option to update)

php artisan vendor:publish --provider="Itemvirtual\EcommerceCart\EcommerceCartServiceProvider" --tag=config

You can change the config values for cart_session_name and taxes_included:

#ECOMMERCE_CART_SESSION_NAME="ecommerceCart"
ECOMMERCE_TAXES_INCLUDED=true
ECOMMERCE_CALCULATE_TOTALS=true

Add EcommerceCart to your config/app aliases array

'EcommerceCart' => Itemvirtual\EcommerceCart\Facades\EcommerceCart::class,

Usage

Use the EcommerceCart Facade

use Itemvirtual\EcommerceCart\Facades\EcommerceCart;

Add products to cart

EcommerceCart::addToCart([
    'id' => $Product->id,
    'title' => $Product->name,
    'price' => floatval($Product->price),
    'tax' => 21.0,
    'amount' => 1,
    'data' => [ // add your custom data here
        'tax_id' => 1
    ]
]);

Increment or decrement amount

EcommerceCart::incrementCartItem($Product->id);
EcommerceCart::decrementCartItem($Product->id);

Remove from cart

EcommerceCart::removeCartItem($Product->id);

Set tax

EcommerceCart::setTax($float);

The tax value is set for each item in the cart. To set a global tax value, use setCustomCartData()
It will not affect the calculation of totals on the cartItem, It only serves as data to calculate your totals.

EcommerceCart::setCustomCartData('tax', floatval($tax));

If you save the tax_id value, you can change it with updateCartItemsDataValue()

EcommerceCart::updateCartItemsDataValue($key, $value);
// EcommerceCart::updateCartItemsDataValue('tax_id', 1);

Set apply tax (p.ex if not European)

EcommerceCart::setApplyTax($boolean);

Get Totals

Custom Total calculations

If you need to calculate your Cart Totals, set ECOMMERCE_CALCULATE_TOTALS to false in your .env file

ECOMMERCE_CALCULATE_TOTALS=false

Create your service and use EcommerceCart::getItems() to get your cart contents

$cartItems = EcommerceCart::getItems();

Total calculations

EcommerceCart::getTotal();
EcommerceCart::getSubtotal();
// return taxes array
EcommerceCart::getTaxTotals();
// return total tax amount
EcommerceCart::getTaxTotalValue();

// Get total without shipping
EcommerceCart::getTotalWithoutShipping();

Shipping

Add Global shipping

$ShippingData = [
    'id' => $Shipping->id,
    'title' => $Shipping->title,
    'value' => $Shipping->value,
    'free_from' => $Shipping->free_from,
];
EcommerceCart::setShipping($ShippingData);

Remove Shipping data

EcommerceCart::removeShipping();

Get Shipping data

EcommerceCart::getShipping();

// Returns an object with 4 or 5 properties
{
  +"id": 3
  +"title": "Europe"
  +"value": 10
  +"free_from": 200
  +"free": true
}

Get Cart Items

$cartItems = EcommerceCart::getItems();

EcommerceCart::hasItems();
EcommerceCart::countItems();

Destroy Cart

EcommerceCart::destroyCart();

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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