lachezargrigorov/laravel-shopping-cart 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lachezargrigorov/laravel-shopping-cart

Composer 安装命令:

composer require lachezargrigorov/laravel-shopping-cart

包简介

An easy to use more advanced shopping cart for Laravel applications.

README 文档

README

Latest Stable Version Latest Unstable Version Software License Build Status Total Downloads

An easy to use more advanced shopping cart for Laravel applications.

Installation

Via Composer

$ composer require lachezargrigorov/laravel-shopping-cart

Laravel 5.5 and above uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider and Facade to the array in config/app.php

If you don't use auto-discovery, add the CartServiceProvider to the providers array in config/app.php

\Lachezargrigorov\Cart\CartServiceProvider::class,

If you want to use the Cart facade, add this to the aliases array in app.php:

'Cart' => \Lachezargrigorov\Cart\Facades\Cart::class,

Implement the Item interface in your product model. The Cart and Item uses getCartPrice method to calculate the totals.

use Illuminate\Database\Eloquent\Model;
use Lachezargrigorov\Cart\Iterfaces\Item;

class Product extends Model implements Item {}

Publish the package config to your local config with the publish command and configure them:

php artisan vendor:publish --provider="Lachezargrigorov\Cart\CartServiceProvider" --tag="config"

Set the item_class in local package config (cart.php)

"item_class" => \App\Product::class,

Legend

Elements

  • Cart : class Lachezargrigorov\Cart\Cart
  • Item : class Lachezargrigorov\Cart\Item
  • Condition : abstract class Lachezargrigorov\Cart\Condition;
  • ItemCondition : class Lachezargrigorov\Cart\ItemCondition extends Condition
  • CartCondition : class Lachezargrigorov\Cart\CartCondition extends Condition

Interfaces

  • Item : interface Lachezargrigorov\Cart\Interfaces\Item

Collections

  • LaravelCollection : class Illuminate\Support\Collection
  • Collection : class Lachezargrigorov\Cart\Collections\Collection extends LaravelCollection
  • ItemAttributesCollection : class Lachezargrigorov\Cart\Collections\ItemAttributesCollection extends Collection
  • ConditionAttributesCollection : class Lachezargrigorov\Cart\Collections\ConditionAttributesCollection extends Collection

Exceptions

  • CartException : class Lachezargrigorov\Cart\Exceptions\CartException

Usage`

Cart

Item

Add or get Item

This method add or get an Item if exist.

  • $id : int - the id of the item (product) model
  • return : Item
Cart::item($id);  //quantity = 0 on create
Models loading process

For better performance models are lazy associated to the items on first '$item->model' call after init or item addition in single DB request so you don't need to add any extra data like name, price, etc.

Cart::item(1);
Cart::item(2)->addQuantity(1);

//models are not loaded yet

//models are lazy loaded here
Cart::item(1)->model;

//if item not exist already, add a new one and mark that models need to be loaded again on next "$item->model" call
Cart::item(3);
Cart::item(4);

//models are not loaded again

//models are lazy loaded here again
Cart::item(4)->model;
Remove Item

-return : removed Item

Cart::item($id)->remove(); 
Get items
  • return : LaravelCollection with Items
Cart::items(); 
Has item (Item)
  • id : int - the id of the item (product) model
  • return : bool
Cart::has($id); 
Get items count
  • return : int
Cart::count(); 
Remove items (Item)
  • ids : [] - array with ids
  • return : Cart
Cart::remove($ids);
Remove all cart items
  • return : Cart
Cart::empty();
Is empty for items
  • return : bool
Cart::isEmpty();
Get item keys
  • return : LaravelCollection with keys
Cart::keys();
Empty the item models

This will case the cart to reload the models on next model call.

  • return : Cart
Cart::emptyModels();

CartCondition

Add or get CartCondition

This method add or get an CartCondition if exist.

  • name : condition name
  • return : CartCondition
Cart::condition($name);

Add or set CartConditions as array

This will rewrite the existing conditions.

  • return : Cart
    Cart::setConditionAsArray([
               "name"  => "all sale1",
               "type"  => "all sale",
               "value" => "-10%",
           ]);

    //or as multidimensional array

  Cart::setConditionAsArray([
            [
                "name"  => "all sale1",
                "type"  => "all sale",
                "value" => "-10%",
            ],
            [
                "name"  => "all sale2",
                "type"  => "all sale",
                "value" => "+1",
            ],
        ]);
Get conditions
  • return : LaravelCollection with CartConditions
Cart::conditions(); 
Has condition
  • name : condition name
  • return : bool
Cart::hasCondition($name); 
Get conditions count
  • return : int
Cart::countConditions(); 
Remove conditions
  • names : array - array with names
  • return : Cart
Cart::removeConditions($names);
Remove all cart conditions
  • return : Cart
Cart::emptyConditions();
Is empty for conditions
  • return : bool
Cart::isEmptyConditions();
Get condition keys (names)
  • return : LaravelCollection with keys
Cart::keysOfConditions();

Total methods

Get total quantity
  • return : int
Cart::totalQuantity();
Get cart subtotal without applied ItemConditions and CartConditions
  • return : double
Cart::subtotalWithoutConditions();
Get cart subtotal without applied CartConditions
  • return : double
Cart::subtotal();
Get cart total with applied ItemConditions and CartConditions
  • return : double
Cart::total();

Item

Properties

  • id : int
  • quantity : int
  • attributes : ItemAttributesCollection
  • conditions : LaravelCollection with ItemConditions
  • model : Illuminate\Database\Eloquent\Model
Set quantity
  • return : Item
Cart::item($id)->quantity(1); 
Add quantity (current quantity + added quantity)
  • return : Item
Cart::item($id)->addQuantity(1); 
Get quantity
  • return : int
Cart::item($id)->quantity; 
Set attributes
  • return : Item
Cart::item($id)->attributes(["size" => "L", "color" => "blue"]); 
Get attributes
  • return : ItemAttributesCollection
$attributesCollection = Cart::item($id)->attributes;
$itemSize = $attributesCollection->size;
$itemColor = $attributesCollection->color;

 //or

Cart::item($id)->attributes->size;
Cart::item($id)->attributes->color;

//or using LaravelCollection methods

Cart::item($id)->attributes->has("size");
Cart::item($id)->attributes->get("size");
Cart::item($id)->attributes->each(function($value, $key){
    ...
});
Empty attributes (delete all attributes)
  • return : Item
Cart::item($id)->emptyAttributes(); 
Add or get ItemCondition
  • name : string - condition name
  • return : ItemCondition
Cart::item($id)->condition($name); 

Add or set ItemConditions as array

This will rewrite the existing conditions.

  • return : Item
    Cart::item(1)->setConditionAsArray([
               "name"  => "item sale1",
               "type"  => "item sale",
               "value" => "-10%",
           ]);

    //or as multidimensional array

  Cart::item(1)->setConditionAsArray([
            [
                "name"  => "item sale1",
                "type"  => "item sale",
                "value" => "-10%",
            ],
            [
                "name"  => "item sale2",
                "type"  => "item sale",
                "value" => "+1",
            ],
        ]);
Get conditions
  • return : LaravelCollection with ItemConditions
Cart::item($id)->conditions(); 
Remove ItemCondition

return : removed ItemCondition

Cart::item($id)->condition($name)->remove(); 
Has condition
  • name : string - condition name
  • return : bool
Cart::item($id)->hasCondition($name); 
Does item contain any conditions
  • return : bool
Cart::item($id)->isEmptyConditions(); 
Empty conditions (remove all conditions)
  • return : Item
Cart::item($id)->emptyConditions(); 
Get model
  • return : Illuminate\Database\Eloquent\Model
Cart::item($id)->model; 

Price methods

Get price without applied conditions

-return : double

Cart::item($id)->priceWithoutConditions(); 
Get price sum without applied conditions (price * quantity)

-return : double

Cart::item($id)->priceSumWithoutConditions(); 
Get price with applied conditions

-return : double

Cart::item($id)->price(); 
Get price sum with applied conditions (price * quantity)

-return : double

Cart::item($id)->priceSum(); 

Set helper

  • [] : array - quantity : int (not required), add_quantity : int (not required), attributes : array (not required), conditions : array (not required)
  • return : Item
  Cart::item(1)->set([
            "quantity" => 1,
            //"add_quantity" => 2,
            "attributes" => [
                "size" => "S",
            ],
            "conditions" => [
                [
                    "name"  => "whole sale",
                    "type"  => "all sale",
                    "value" => "10%",
                    "attributes" => ["some" => "attribute"]
                ], [
                    "name"  => "item sale",
                    "type"  => "item sale",
                    "value" => "-1",
                ]
            ]

        ]);
              
    // equel to
    
   Cart::item(1)->quantity(1)/*->addQuantity(2)*/->attributes(["size" => "S"])->condition('whole sale')->type("all sale")->value("10%")->attributes(["some" => "attribute"]);
   Cart::item(1)->condition("item sale")->type("item sale")->value("-1");

Condition (CartCondition and ItemCondition)

Properties

  • name : string - condition name and it's collection key are always the same
  • type : string
  • value : string - [+-]?[0-9]+(\.[0-9]+)?[%]? examples: +10%, 10%, 10.22% -10%, +10.11, -10.80
  • attributes : ConditionAttributesCollection
Set name

This will change the key in collection too.

  • name : string
  • return : CartCondition | ItemCondition
// CartCondition 

//this will create a new condition with name and key = "all sale"
Cart::condition("all sale"); 

//this will change the name and the key in collection too
Cart::condition("all sale")->name("friday sale");

//now this condition is accessible with the new key (name)
Cart::condition("friday sale");

// ItemCondition 

//this will create a new item condition with name and key = "all sale"
Cart::item(1)->condition("all sale"); 

//this will change the name and the key in collection too
Cart::item(1)->condition("all sale")->name("friday sale");

//now this item condition is accessible with the new key
Cart::item(1)->condition("friday sale");
Get name
  • return : string
// CartCondition 

Cart::condition("all sale")->name;

// ItemCondition 

Cart::item(1)->condition("item sale")->name;
Set type
  • type : string
  • return : CartCondition | ItemCondition
// CartCondition 

Cart::condition("all sale")->type($type);

// ItemCondition 

Cart::item(1)->condition("item sale")->type($type);
Get type
  • return : string
// CartCondition 

Cart::condition("all sale")->type;

// ItemCondition 

Cart::item(1)->condition("item sale")->type;
Set value
  • value : string
  • return : CartCondition | ItemCondition
// CartCondition 

Cart::condition("all sale")->value($value);

// ItemCondition 

Cart::item(1)->condition("item sale")->value($value);
Get value
  • return : string
// CartCondition 

Cart::condition("all sale")->value;

// ItemCondition 

Cart::item(1)->condition("item sale")->value;
Set attributes

Merge existing attributes.

  • attributes : array
  • return : CartCondition | ItemCondition
// CartCondition 

$attributes = ["some_attribute" => "attribute"];
Cart::condition("all sale")->attributes($attributes);

// ItemCondition 

Cart::item(1)->condition("item sale")->attributes($attributes);
Get attributes
  • return : ConditionAttributesCollection
// CartCondition 

Cart::condition("all sale")->attributes;
Cart::condition("all sale")->attributes->some_attribute;

// ItemCondition 

Cart::item(1)->condition("item sale")->attributes;
Cart::item(1)->condition("item sale")->attributes->some_attribute;

//or useing any LaravelCollection method
Cart::item(1)->condition("item sale")->attributes->get("some_attribute");
Empty attributes (delete all attributes)
  • return : CartCondition | ItemCondition
// CartCondition 

Cart::condition("all sale")->emptyAttributes();

// ItemCondition 

Cart::item(1)->condition("item sale")->emptyAttributes();

Set helper

  • [] : array - name : string (not required), type : string (not required), value : string (not required), attributes : array (not required)
  • return : CartCondition | ItemCondition
// CartCondition 

Cart::condition("all sale")->set([
    "name" => "sale", 
    "type" => "sale", 
    "value" => "-10%",
    "attributes" => [
        "size" => "M"
    ]
]);

// ItemCondition 

Cart::item(1)->condition("all sale")->set([
    "name" => "sale", 
    "type" => "sale", 
    "value" => "-10%",
    "attributes" => [
        "size" => "M"
    ]
]);

Testing

$ composer test

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING, ISSUE_TEMPLATE, PULL_REQUEST_TEMPLATE and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email lachezar@grigorov.website instead of using the issue tracker.

Credits

License

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

lachezargrigorov/laravel-shopping-cart 适用场景与选型建议

lachezargrigorov/laravel-shopping-cart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cart」 「laravel」 「shopping cart」 「lachezargrigorov」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lachezargrigorov/laravel-shopping-cart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lachezargrigorov/laravel-shopping-cart 我们能提供哪些服务?
定制开发 / 二次开发

基于 lachezargrigorov/laravel-shopping-cart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-06