定制 divineomega/laravel-extendable-basket 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

divineomega/laravel-extendable-basket

Composer 安装命令:

composer require divineomega/laravel-extendable-basket

包简介

🛒 Laravel Extendable Basket provides several abstract classes that implement basic ecommerce basket functionality

README 文档

README

Build Status Coverage Status StyleCI

Laravel Extendable Basket provides several abstract classes that implement basic ecommerce basket functionality. These classes must be extended by your application.

Installation

Just install the latest version using composer.

composer require jord-jd/laravel-extendable-basket

Compatibility

This package supports Laravel versions 5.5 through 10 and requires PHP 8.1 or higher.

Setup

You need to perform various setup steps in order to make use of this package.

Database tables

Two database tables are required to store basket and basket item data. By default these are called baskets and basket_items. This package provides database migrations to create these tables.

To run these migrations, enter the following artisan command.

php artisan migrate

Models

Now you need to create two related models, one to hold basket details and one to hold basket items details. Two example models are shown below. They can be modified or added to as necessary.

Create a Basket model.

# app/Basket.php

<?php
namespace App;

use JordJD\LaravelExtendableBasket\Models\Basket as BasketModel;

class Basket extends BasketModel
{
    public function items(): HasMany
    {
        return $this->hasMany('App\BasketItem');
    }
}

Create a BasketItem model.

# app/BasketItem.php

<?php
namespace App;

use JordJD\LaravelExtendableBasket\Models\BasketItem as BasketItemModel;

class BasketItem extends BasketItemModel
{
    public function basket(): BelongsTo
    {
        return $this->belongsTo('App\Basket');
    }
}

Basketable interface

Anything that can be placed in the basket provided by this library is considered 'basketable'. You can make any existing Eloquent model basketable simply by making it implement the Basketable interface.

For example, if you had a Product model, you can change it as follows.

# app/Product.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use JordJD\LaravelExtendableBasket\Interfaces\Basketable;

class Product extends Model implements Basketable {

    // ...

}

Note that any basketable models must have a getPrice() and a getName() method that returns a numeric price and textual name of the basketable, respectively.

Usage

This section describes the use of the basket and basket item functionality provided by this package. It assumes you have performed the required installation and setup, in the manner specified above.

Remember to use the basket and/or basket item models you have created, where necessary.

Getting the current basket

From anywhere in your application, you can get the current basket. If no basket currently exists in the session, one will be created.

$basket = Basket::getCurrent();

Getting a new basket

If you need to discard the current basket and create a new one, such as after a user has placed an order, you can use the getNew method.

$basket = Basket::getNew();

This method essentially doubles as emptying the basket.

Manual basket management

The static methods getCurrent and getNew store the current basket ID in the session. If you do not want this, you can manage baskets yourself, as you would with any Eloquent model.

// Get new basket
$basket = new Basket();

// Get current basket
$userBasketId = Auth::user()->basket_id;
$basket = Basket::find($userBasketId);

Add item(s) to the basket

After getting the current basket, you can easily add items to it using the basket's add method. You need to provide it with a quantity and any basketable model.

Optionally, you can also provide an array of basket item meta data, which could be used to store information about variations on a product.

$quantity = 5;
$product = Product::FindOrFail(1);

$basket->add($quantity, $product);
$basket->add($quantity, $product, ['colour' => 'red', 'size' => 'medium']);

Getting basket items

Getting items from the basket and the basketable model they contain can be easily done. See the example below.

foreach($basket->items as $item) {
    $product = $item->basketable;
    echo $product->name;
}

Changing the quantity of a basket item

Each basket item has a quantity associated with it. It is set when an item is added to the basket, but can be modified later via the setQuantity method.

$item = $basket->items->first();
$item->setQuantity($request->quantity);

Removing a basket item

Basket items can easily be removed simply by deleting them. See the following example.

$item = $basket->items->first();
$item->delete();

Getting the unit cost of a basket item

Getting the unit cost of a basket item just involves calling the getPrice method of the basketable model associated with the basket item. See the example below.

$item = $basket->items->first();
$item->basketable->getPrice();

Getting the line total of a basket item

The basket item class provides a getPrice method that gets the line total. This is simply the basketable's price multiplied by the basket item quantity. The example code below shows how to use this method.

$item = $basket->items->first();
$item->getPrice();

Getting the total number of items in the basket

There is a single method in the basket class that can sum up all the basket item quantities. Just call the getTotalNumberOfItems method, as follows.

$numItems = $basket->getTotalNumberOfItems();

Getting the basket subtotal

A getSubtotal method is provided in the basket class that provides the total of all items in the basket. See the following example.

$subtotal = $basket->getSubtotal()

If you wish to add delivery costs or discounts, you can create a new getTotal method in your basket class. This method can call the getSubtotal method, and then modify and return it, in whatever way you wish.

An example Basket class implementing this idea is shown below.

# app/Basket.php

<?php
namespace App;

use JordJD\LaravelExtendableBasket\Models\Basket as BasketModel;

class Basket extends BasketModel
{
    public function items()
    {
        return $this->hasMany('App\BasketItem');
    }

    public function getTotal()
    {
        $deliveryCost = 3.99;

        return $this->getSubtotal() + $deliveryCost;
    }
}

divineomega/laravel-extendable-basket 适用场景与选型建议

divineomega/laravel-extendable-basket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.6k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2017 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 divineomega/laravel-extendable-basket 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 0
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-only
  • 更新时间: 2017-09-21