2017 © Pedro Peláez
 

library laravel-shopping-cart

Shopping cart for Laravel Application.

image

overtrue/laravel-shopping-cart

Shopping cart for Laravel Application.

  • Thursday, April 26, 2018
  • by overtrue
  • Repository
  • 18 Watchers
  • 235 Stars
  • 3,738 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 54 Forks
  • 1 Open issues
  • 8 Versions
  • 3 % Grown

The README.md

Laravel Shopping Cart

Shopping cart for Laravel Application., (*1)

Build Status Latest Stable Version Latest Unstable Version Scrutinizer Code Quality Code Coverage Total Downloads License, (*2)

Installation

$ composer require "overtrue/laravel-shopping-cart:~2.0"

or add the following line to your project's composer.json:, (*3)

"require": {
    "overtrue/laravel-shopping-cart": "~2.0"
}

then, (*4)

$ composer update

After completion of the above, add the follow line to the section providers of config/app.php:, (*5)

Overtrue\LaravelShoppingCart\ServiceProvider::class,

And add the follow line to the section aliases:, (*6)

'ShoppingCart'      => Overtrue\LaravelShoppingCart\Facade::class,

Usage

Add item to cart

Add a new item., (*7)

Item | null ShoppingCart::add(
                    string | int $id,
                    string $name,
                    int $quantity,
                    int | float $price
                    [, array $attributes = []]
                 );

example:, (*8)

$row = ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
// Item:
//    id       => 37
//    name     => 'Item name'
//    qty      => 5
//    price    => 100.00
//    color    => 'red'
//    size     => 'M'
//    total    => 500.00
//    __raw_id => '8a48aa7c8e5202841ddaf767bb4d10da'
$rawId = $row->rawId();// get __raw_id
$row->qty; // 5
...

Update item

Update the specified item., (*9)

Item ShoppingCart::update(string $rawId, int $quantity);
Item ShoppingCart::update(string $rawId, array $arrtibutes);

example:, (*10)

ShoppingCart::update('8a48aa7c8e5202841ddaf767bb4d10da', ['name' => 'New item name']);
// or only update quantity
ShoppingCart::update('8a48aa7c8e5202841ddaf767bb4d10da', 5);

Get all items

Get all the items., (*11)

Collection ShoppingCart::all();

example:, (*12)

$items = ShoppingCart::all();

Get item

Get the specified item., (*13)

Item ShoppingCart::get(string $rawId);

example:, (*14)

$item = ShoppingCart::get('8a48aa7c8e5202841ddaf767bb4d10da');

Remove item

Remove the specified item by raw ID., (*15)

boolean ShoppingCart::remove(string $rawId);

example:, (*16)

ShoppingCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

Destroy cart

Clean Shopping Cart., (*17)

boolean ShoppingCart::destroy();
boolean ShoppingCart::clean(); // alias of destroy();

example:, (*18)

ShoppingCart::destroy();// or ShoppingCart::clean();

Total price

Returns the total of all items., (*19)

int | float ShoppingCart::total(); // alias of totalPrice();
int | float ShoppingCart::totalPrice();

example:, (*20)

$total = ShoppingCart::total();
// or
$total = ShoppingCart::totalPrice();

Count rows

Return the number of rows., (*21)

int ShoppingCart::countRows();

example:, (*22)

ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(127, 'foobar', 15, 100.00, ['color' => 'green', 'size' => 'S']);
$rows = ShoppingCart::countRows(); // 2

Count quantity

Returns the quantity of all items, (*23)

int ShoppingCart::count($totalItems = true);

$totalItems : When false,will return the number of rows., (*24)

example:, (*25)

ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
$count = ShoppingCart::count(); // 11 (5+1+5)

Search items

Search items by property., (*26)

Collection ShoppingCart::search(array $conditions);

example:, (*27)

$items = ShoppingCart::search(['color' => 'red']);
$items = ShoppingCart::search(['name' => 'Item name']);
$items = ShoppingCart::search(['qty' => 10]);

Check empty

bool ShoppingCart::isEmpty();

Specifies the associated model

Specifies the associated model of item before you add items to cart., (*28)

Cart ShoppingCart::associate(string $modelName);

example:, (*29)

ShoppingCart::associate('App\Models\Product');

ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);

$item = ShoppingCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->product->name; // $item->product is instanceof 'App\Models\Product'

The Collection And Item

Collection and Overtrue\LaravelShoppingCart\Item are instanceof Illuminate\Support\Collection, Usage Refer to:Collections - Laravel doc., (*30)

properties of Overtrue\LaravelShoppingCart\Item:, (*31)

  • id - your goods item ID.
  • name - Name of item.
  • qty - Quantity of item.
  • price - Unit price of item.
  • total - Total price of item.
  • __raw_id - Unique ID of row.
  • __model - Name of item associated Model.
  • ... custom attributes.

And methods:, (*32)

  • rawId() - Return the raw ID of item.

Events

Event Name Parameters
shopping_cart.adding ($attributes, $cart);
shopping_cart.added ($attributes, $cart);
shopping_cart.updating ($row, $cart);
shopping_cart.updated ($row, $cart);
shopping_cart.removing ($row, $cart);
shopping_cart.removed ($row, $cart);
shopping_cart.destroying ($cart);
shopping_cart.destroyed ($cart);

You can easily handle these events, for example:, (*33)

Event::listen('shopping_cart.adding', function($attributes, $cart){
    // code
});

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?, (*34)

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》, (*35)

License

MIT, (*36)

The Versions

26/04 2018

dev-master

9999999-dev

Shopping cart for Laravel Application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Avatar overtrue

laravel shopping shopping cart

03/07 2017

2.0.0

2.0.0.0

Shopping cart for Laravel Application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Avatar overtrue

laravel shopping shopping cart

29/01 2017

1.0.5

1.0.5.0

Shopping cart for Laravel Application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Avatar overtrue

laravel shopping shopping cart

13/11 2016

1.0.4

1.0.4.0

Shopping cart for Laravel Application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Avatar overtrue

laravel shopping shopping cart

15/06 2016

1.0.3

1.0.3.0

Shopping cart for Laravel Application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Avatar overtrue

laravel shopping shopping cart

25/09 2015

1.0.2

1.0.2.0

Shopping cart for Laravel Application.

  Sources   Download

MIT

The Development Requires

by Avatar overtrue

laravel shopping shoppingcart

14/08 2015

1.0.1

1.0.1.0

Shopping cart for Laravel Application.

  Sources   Download

MIT

The Development Requires

by Avatar overtrue

laravel shopping shoppingcart

01/06 2015

1.0.0

1.0.0.0

Shopping cart for Laravel Application.

  Sources   Download

MIT

The Development Requires

by Avatar overtrue

laravel shopping shoppingcart