2017 © Pedro Peláez
 

library cart

E-Commerce Cart Package

image

damjangkae/cart

E-Commerce Cart Package

  • Wednesday, April 11, 2018
  • by damjangkae
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Laravel Cart

A simple cart built for Laravel., (*1)

Installation

1) Download this repository and place in packages, (*2)

2) Add the path into composer.json, (*3)

composer require damjangkae/cart

3) Add these following lines to config/app.php. - At providers array:, (*4)

Damjangkae\Cart\CartServiceProvider::class
  • At aliases array:
'Cart' => Damjangkae\Cart\Facades\Cart::class

4) Do composer dump-autoload., (*5)

Done., (*6)


Overview

Look at one of the following topics to learn more about LaravelShoppingcart, (*7)

Usage

Here is the methods provided:, (*8)

Cart::add()

$book = Book::find(1);
Cart::add($book);

By the model Book must be implemented Damjangkae\Cart\Contracts\Buyable which required methods:, (*9)

public function getIdentifier();
public function getPrice(): float;

The function getIdentifier() expect model to return ID or something that unique your item., (*10)

Another optional way to use add()., (*11)

With optional parameters:, (*12)

Cart::add($book, 2, 100, ['author' => 'John Doe']);

or parameters in array as secondary argument:, (*13)

Cart::add($book, [
    'quantity' => 2,
    'price' => 100,
    'attributes' => ['author' => 'John Doe']
]);

Cart::update()

To update an item in the cart, you must specific the identifier., (*14)

$identifier = '9790404436093';
Cart::update($identifier, 2);

The existing data will be overwritten with new one by using update() which different with add() that rely on existing one. Example:, (*15)

Cart::add($book1, 2);
Cart::update($book1, 3);
// The total will be 3

Cart::add($book2, 2);
Cart::add($book2, 3);
// The total will be 5

Another way to use update()., (*16)

Same as you do with add()., (*17)

Cart::update($identifier, ['price' => 900]);

Cart::remove()

$identifier = '9790404436093';
Cart::remove($identifier);

Notice: Updating item quantity to 0 is share the same result with remove()., (*18)

Cart::find()

Easy as same as update() and remove(), (*19)

$identifier = '9790404436093';

Cart::find($identifier);

Damjangkae\Cart\Exceptions\ItemNotFoundException will be thrown if item not exists., (*20)

Cart::items()

Return collection of items in cart, (*21)

Cart::items();

Cart::destroy()

Delete the cart object., (*22)

Cart::destroy();

Cart::subtotal()

Return the subtotal price., (*23)

Cart::subtotal();

Cart::total()

Cart::subtotal();

Cart::count()

Return the item count by type. 2 books and 3 chairs will return 2, (*24)

Cart::count();

Cart::quantity()

Return the item count by all of quantity. 2 books and 3 chairs will return 5, (*25)

Cart::quantity();

Cart::isEmpty()

Return true if cart has no items., (*26)

Cart::isEmpty();

Cart::search()

Passed closure function and you do your magic., (*27)

Cart::search(function (CartItem $cartItem) {
    return $cartItem->price > 1000;
});

Notice: Result returned in collection., (*28)


Collections

Most of methods return as collection (Illuminate\Support\Collection) So you know what to do., (*29)

Cart::items()->first();
Cart::search(function (CartItem $cartItem) {
    return $cartItem->price > 1000;
})->count();

Please see laravel's collection for more info: https://laravel.com/docs/master/collections, (*30)


Instances

You may wish to have a multiple instance of the cart in same session, here is the solution., (*31)

$book1 = Book::find(1);
$book2 = Book::find(2);

Cart::instance('gift')->add($book1);
Cart::instance('wishlist')->add($book2);

Cart::instance('gift')->items()->first(); // book1
Cart::instance('wishlist')->items()->first(); // book2

Notice: If the instance name not set, the default will be default prepend with cart_. So If you set instance name as wishlist the session name will be cart_whishlist., (*32)


Conditions

Every e-commerce have a promotion so let the Cart do the magic behind the scene., (*33)

First: Create your condition class.

If your condition going to affect with total price make sure you implement Damjangkae\Cart\Conditions\TotalAffectable, but if your condition going to affect with items you must implement Damjangkae\Cart\Conditions\ItemAffectable., (*34)

Example: You'd like to have a 10% discount if customer has subtotal more than 500., (*35)

<?php

use Damjangkae\Cart\Cart;
use Damjangkae\Cart\Conditions\TotalAffectable;

class TenPercentOffIfSubtotalOver500Baht implements TotalAffectable
{
    public function allow(Cart $cart): bool
    {
        return true;
    }

    public function active(Cart $cart): bool
    {
        return $cart->subtotal > 500;
    }

    public function getDiscount(Cart $cart): float
    {
        return $cart->subtotal * .1;
    }
}

As you can see you must implement 3 methods which are:, (*36)

  • allow() expect you to return boolean that if true returned this condition is allowed to add to the cart. If false returned, Damjangkae\Cart\Exceptions\ConditionIsNotAllowToAddException will be thrown., (*37)

  • active() expect you to return boolean that if true returned this condition is active., (*38)

  • getDiscount() expect you to return float that will be the discount to the subtotal., (*39)

Example: You'd like to have give a free another book if customer has the book in the cart., (*40)

<?php

use Damjangkae\Cart\Cart;
use Damjangkae\Cart\CartItem;
use Damjangkae\Cart\Conditions\ItemAffectable;

class FreeAnotherBookIfTheBookInCart implements ItemAffectable
{
    public function allow(Cart $cart): bool
    {
        return true;
    }

    public function active(Cart $cart): bool
    {
        $book = App\Book::find(1);

        return $cart->items->has($book->getItentifier());
    }

    public function getItem(Cart $cart): CartItem
    {
        $anotherBook = App\Book::find(2);

        return new CartItem($anotherBook, 1, 0);
    }
}

The additional method getItem expect you to return Damjangkae\Cart\CartItem which the way to create is same with you do on Cart::add(). Make sure you set the price to 0 if you'd like to give it for free., (*41)

Last example: If you wish to have only 1 condition in the cart., (*42)

public function allow(Cart $cart): bool
{
    return $cart->conditions->count() == 0;
}

Sectond: Add your condition to cart.

You have to provide condition key like 10% off to identify your condition., (*43)

Cart::addCondition('10% off', new TenPercentOffIfSubtotalOver500Baht);

Final: See the magic with your own eyes.

Cart::add($book, 1, 1000);
Cart::addCondition('10% off', new TenPercentOffIfSubtotalOver500Baht);

Cart::subtotal(); // 1000
Cart::total(); // 900

Store in Database

You may wish to store data into database for example user like to shopping over machine., (*44)

Storing

\Cart::add($book1);
\Cart::addCondition('10% off', new TenPercentOffIfSubtotalOver500Baht);

\Cart::store('user_1');

Restoring

\Cart::restore('user_1');

\Cart::items()->first(); // $book1

Notice: After restored the record in database will be deleted, (*45)

Also work with instance

\Cart::instance('user_1')->add($book1);
\Cart::instance('user_2')->add($book2);

\Cart::instance('user_1')->store('test_1');
\Cart::instance('user_2')->store('test_2');

Exceptions

Exception Reason
ItemNotFoundException When trying to retrieve not exists item
ConditionIsNotAllowedToAddException When try to add condition which false returned at method allowed()
ConditionNotFoundException When trying to retrieve not exists condition
CartAlreadyStoredException When trying to stored cart with exists identofier
CartNotFoundInStoreException When trying to retrieve not exists cart in database

Events

Event (Class) Parameter
Damjangkae\Cart\Events\AddingItem Cart, CartItem
Damjangkae\Cart\Events\AddedItem Cart, CartItem
Damjangkae\Cart\Events\UpdatingItem Cart, CartItem, $parameters
Damjangkae\Cart\Events\UpdatedItem Cart, CartItem
Damjangkae\Cart\Events\RemovingItem Cart, CartItem
Damjangkae\Cart\Events\RemovedItem Cart, CartItem
Damjangkae\Cart\Events\AddingCondition Cart, CartCondition
Damjangkae\Cart\Events\AddedCondition Cart, CartCondition
Damjangkae\Cart\Events\RemovingCondition Cart, CartCondition
Damjangkae\Cart\Events\RemovedCondition Cart, CartCondition
Damjangkae\Cart\Events\RefreshedCondition Cart
Damjangkae\Cart\Events\DestroyingCart Cart
Damjangkae\Cart\Events\DestroyedCart Cart
Damjangkae\Cart\Events\StoringCart Cart, $identifier
Damjangkae\Cart\Events\StoredCart Cart, $identifier
Damjangkae\Cart\Events\RestoringCart $identifier
Damjangkae\Cart\Events\RestoredCart $identifier

The Versions

11/04 2018