ozgurkaragoz / basket

A stable basket for an e-commerce website

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shopping Basket Package

Build Status StyleCI Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License

The Lenius shopping basket composer package makes it easy to implement a shopping basket into your application and store the basket data using one of the numerous data stores provided. You can also inject your own data store if you would like your basket data to be stored elsewhere.

Usage

Below is a basic usage guide for this package.

Instantiating the basket

Before you begin, you will need to know which storage and identifier method you are going to use. The identifier is how you store which basket is for that user. So if you store your basket in the database, then you need a cookie (or some other way of storing an identifier) so we can link the user to a stored basket.

In this example we're going to use the cookie identifier and session for storage.

use Lenius\Basket\Basket;
use Lenius\Basket\Storage\Session;
use Lenius\Basket\Identifier\Cookie;

$basket = new Basket(new Session, new Cookie);

Inserting items into the basket

Inserting an item into the basket is easy. The required keys are id, name, price and quantity, although you can pass over any custom data that you like.

$basket->insert([
    'id'       => 'foo',
    'name'     => 'bar',
    'price'    => 100,
    'quantity' => 2,
    'weight' => 300
]);

Inserting items with options into the basket

Inserting an item into the basket is easy. The required keys are id, name, price and quantity, although you can pass over any custom data that you like. If option items contains price or weight there values are added to the total weight / price of the product.

$basket->insert([
    'id'       => 'foo',
    'name'     => 'bar',
    'price'    => 100,
    'quantity' => 2,
    'weight' => 300,
    'options'  => [
       [
        'name' => 'Size',
        'value' => 'L'
        'weight' => 50,
        'price' => 10
       ],
     ],
]);

Updating items in the basket

You can update items in your basket by updating any property on a basket item. For example, if you were within a basket loop then you can update a specific item using the below example.

foreach ($basket->contents() as $item) {
    $item->name = 'Foo';
    $item->quantity = 1;
}

Removing basket items

You can remove any items in your basket by using the remove() method on any basket item.

foreach ($basket->contents() as $item) {
    $item->remove();
}

Destroying/emptying the basket

You can completely empty/destroy the basket by using the destroy() method.

$basket->destroy()

Retrieve the basket contents

You can loop the basket contents by using the following method

$basket->contents();

About

A stable basket for an e-commerce website


Languages

Language:PHP 100.0%