Git Product home page Git Product logo

laravel-shopping-cart's Introduction

Laravel Shopping Cart

Shopping cart for Laravel Application.

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

Installation

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

or add the following line to your project's composer.json:

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

then

$ composer update

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

Overtrue\LaravelShoppingCart\ServiceProvider::class,

And add the follow line to the section aliases:

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

Usage

Add item to cart

Add a new item.

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

example:

$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.

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

example:

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

Get all items

Get all the items.

Collection ShoppingCart::all();

example:

$items = ShoppingCart::all();

Get item

Get the specified item.

Item ShoppingCart::get(string $rawId);

example:

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

Remove item

Remove the specified item by raw ID.

boolean ShoppingCart::remove(string $rawId);

example:

ShoppingCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

Destroy cart

Clean Shopping Cart.

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

example:

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

Total price

Returns the total of all items.

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

example:

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

Count rows

Return the number of rows.

int ShoppingCart::countRows();

example:

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

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

$totalItems : When false,will return the number of rows.

example:

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.

Collection ShoppingCart::search(array $conditions);

example:

$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.

Cart ShoppingCart::associate(string $modelName);

example:

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.

properties of Overtrue\LaravelShoppingCart\Item:

  • 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:

  • 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:

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

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

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

License

MIT

laravel-shopping-cart's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar jaychan avatar m809745357 avatar overtrue avatar qbhy avatar summerblue avatar uax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-shopping-cart's Issues

关于监听事件传入参数为空的BUG探讨

先看代码:

public function add($id, $name = null, $qty = null, $price = null, array $attributes = [])
    {
        $cart = $this->getCart();

        $this->event->fire('shopping_cart.adding', [$attributes, $cart]);

        $row = $this->addRow($id, $name, $qty, $price, $attributes);

        $this->event->fire('shopping_cart.added', [$attributes, $cart]);

        return $row;
    }

这一段是添加到购物车里面的代码,可以看到是先获取购物车,然后再添加到购物车,这时候触发added事件,传入的参数是$cart,可是这个是未添加入购物车之的,这样就导致传入的购物车实际上还是未添加状态的。应该要这样改:

$this->event->fire('shopping_cart.added', [$attributes, $this->getCart()]);

不知道这算是bug还有有意为之的feature?所有涉及到ed事件的应该都是存在这个问题的。

ShoppingCart::all(); return null

我$row = ShoppingCart::add();後 查看$row資訊 成功儲存
但我到別的controller ShoppingCart::all() 卻回傳空的

我根據$row的id下去 ShoppingCart::get('id'); 也是空的

composer 组件开发

在composer.json里面并没有看到你 require laravel 框架,但为何在代码里面却用到了laravel的组件。
另外,composer开发有相关的资料么,求推荐。我是雷俊,上回的线下聚会我也在。

Store in database and edit the order

I can get all the items and serialize that to store along with other order information.
I may need to edit the order to change the items or update the quantity or price. do you have any example of that? or is this package allow store and restore from database?

Update does not work during adding of the same item if quantity is different

Update does not work properly during adding of the same item if quantity is different

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

image

How to set cart name dynamically?

I want to set different carts for different vendors. How can I set a different cart name dynamically? Will have to call setName on each call? Please help.

About Specifies the associated model

我 dd ShoppingCart::associate('App\Models\Product') 的訊息
得到 #model: "App\Models\Product"
但我ShoppingCart::get('xxxxxxxx')取出購物車裡的商品時 裡面的 #mode: null
請問這個功能確實有作用嗎?

composer install error

两种安装方式都存在错误
composer require "overtrue/laravel-shopping-cart:1.0.*"
会提示 如下错误。 但是我本地已经安装了更高级的log-viewer 版本
image

二 更新composer.json 文件后,进行composer update 操作
会出现如下错误
image

尝试多次后仍无法解决此问题。

第二种方式出现的symfony问题可能是官方的。

Undefined type

In my CartController.php I am pasting "use Overtrue\LaravelShoppingCart\ShoppingCart;" and

public function index()
{
return ShoppingCart::content();
}

But the error I am getting: "Undefined type 'Overtrue\LaravelShoppingCart\ShoppingCart'.intelephense(1009)"

Creates duplicate item in the cart list

When I tried to click the add to cart button pointing to the CartContoller I have made then redirect back to the previous page the cart items becomes duplicate. Please help

类中的fire方法替换dispatch方法

Illuminate/Events/Dispatcher 类中的fire 方法 (在Larevel 5.4中不赞成使用,Larevel 5.8 已经被移除了)
应当使用它的替代方法 dispatch ,否则会报如下错误:
Oops! An unexpected error occurred: Call to undefined method Illuminate\Events\Dispatcher::fire()

public function add($id, $name = null, $qty = null, $price = null, array $attributes = [])
{
    $cart = $this->getCart();
    $this->event->dispatch('shopping_cart.adding', [$attributes, $cart]);
    $row = $this->addRow($id, $name, $qty, $price, $attributes);
    $this->event->dispatch('shopping_cart.added', [$attributes, $cart]);
    return $row;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.