abo3adel/shoppingcart

一个简单但强大且高度定制的laravel购物车

v1.0.4 2020-08-10 05:50 UTC

This package is auto-updated.

Last update: 2024-09-10 14:58:08 UTC


README

一个简单但强大且高度定制的laravel购物车。

高度灵感来源于LaravelShoppingcart

Latest Stable Version Total Downloads Latest Unstable Version License

演示 shopping-cart

功能

  • 多个购物车实例
  • 多个可购买模型
  • 折扣 && 税费
  • 在购物车项目中配置最多2列
  • 访客购物车项目 && 用户购物车项目
  • 它更像Laravel的方式

要求

  • PHP 7.1+(在7.2+上测试过)
  • Laravel 5.5

工作原理

此包使用会话和数据库,它将购物车项目保存到会话中供访客使用,然后在用户登录时合并这些项目并将其保存到数据库中,当用户登录时,项目将保存到数据库中

安装

  1. require
composer require abo3adel/shoppingcart
  1. 导入配置文件和迁移并迁移
php artisan vendor:publish --tag=shoppingcart-all
  1. 在迁移之前进行配置
update config/shoppingcart.php with your configration
and then
php artisan migrate
  1. 将一个特性和接口(必需)添加到可购买的模型中
<?php

namespace Abo3adel\ShoppingCart\Tests\Model;

use Abo3adel\ShoppingCart\Contracts\CanBeBought;
use Abo3adel\ShoppingCart\Traits\Buyable;
use Illuminate\Database\Eloquent\Model;

class SpaceCraft extends Model implements CanBeBought
{
    use Buyable;

    public function getPrice(): float
    {
        return $this->price;
    }

    public function getDiscount(): float
    {
        return $this->discount;
    }

    //
}

概述

用法

您可以使用外观或辅助函数

Cart::add()

cart()->add()

Cart::add()

返回新保存的项目

// all options
Cart::add(
    CanBeBought $buyable,
    int $qty,
    mixed $opt1, // see config/shoppingcart.php to change this
    mixed $opt2, // see config/shoppingcart.php to change this
    array $options
)

// only price and qty
Cart::add($buyable, $qty)

// only price && qty && options array
// use this if you do not use any of (opt1 or opt2)
Cart::add($buyable, $qty, ['weight' => 250])

// if you do not use opt2
Cart::add($buyable, $qty, $opt1, ['weight' => 250])

Cart::find()

如果找到则返回购物车项目,否则返回null

// by item ID
Cart::find($itemId)

// by buyable model id
Cart::find(int $buyable_id, string $buyable_type)
Cart::find(25, App\Book::class)

Cart::update()

返回布尔值

// all args
Cart::add(
    int $itemId,
    int $qty,
    mixed $opt1, // see config/shoppingcart.php to change this
    mixed $opt2, // see config/shoppingcart.php to change this
    array $options
)

// only qty
Cart::update($itemId, $qty)

// only options
Cart::update($itemId, $options)

// if no (opt1 || opt2)
Cart::update($itemId, $qty, $options)

// if no opt2
Cart::update($itemId, $qty, $opt1, $options)

Cart::delete()

返回布尔值

Cart::delete(int $itemId)

Cart::has()

返回布尔值

// by item id
Cart::has($itemId)

// by buyable id
Cart::has($buyable_id, $buyable_type)
Cart::has(5, 'App\Product')

Cart::content()

返回当前实例的购物车项目集合。允许所有集合方法,请参阅Collections

Cart::content()

Cart::content()->count() // get count

Cart::content()->search() // search

Cart::content()->each() // loop

// or any collection method

Cart::destroy()

删除当前实例的所有项目

删除的项目数量

Cart::destroy()

实例

如果您没有设置不同的实例,购物车将保持在最后设置的实例中

Cart::instance()->content() // default instance

Cart::instance('wishlist')->content()
Cart::getInstance() // wishlist
Cart::destroy() // instance still wishlist

Cart::instance('compare')->add($buyable, 2)
Cart::getInstance() // compare

Cart::instance()->delete(5)
Cart::getInstance() // default

助手

Cart::total()

获取所有项目的小计(数量 * 价格)的总和

Cart::total() // 5631.25

// if you want the total to be formated
Cart::total(true)
Cart::total($formated, $decimals, $dec_point, $thousands_sep)

Cart::totalPrice()

所有项目价格的总额

Cart::totalQty()

所有项目数量的总和

Cart::subTotal()

获取减去税费百分比的总计

Cart::subTotal() // 2516.32

Cart::subTotal(true) // 2,516.32
Cart::subTotal($formated, $decimals, $dec_point, $thousands_sep)

Cart::setTax()

仅为当前实例设置税费百分比,不会影响配置的值

Cart::setTax(25)->subTotal()

Cart::getTax()

检索当前税费百分比

Cart::increments()

增加项目数量

Cart::increments($itemId, $numberToAdd)

Cart::decrements()

减少项目数量

Cart::decrements($itemId, $numberToAdd)

模型

CartItem

$cartItem = Cart::add($buyable, 5)

// get subtotal (price * qty)
$cartItem->sub_total

// get formated subtotal
$cartItem->sub_total()
$cartItem->sub_total($decimals, $dec_point, $thousands_sep)

// increment item qty
$cartItem->increments($numberToAdd)

// decrement item qty
$cartItem->decrements($numberToSubstract)

// access the buyable object
$cartItem->buyable

Buyable

// add to cart in the default instance
$buyable->addToCart($qty, $opt1, $opt2, $options)

// remove from cart in the default instance
$buyable->removeFromCart()

// get list of all cart items associated with this model
$buyable->items()

// get the subTotal price after discount substract
$buyable->getSubPrice()

奖金

管理其他用户的购物车项目

如果您没有设置不同的用户,购物车将保持在最后设置的User中

// $admin is logged in
// user here is admin

Cart::forUser($user)
// user here is $user
Cart::add($buyable, $qty)

// reset user and return to logged in admin
Cart::resetUser()

// user here is $admin again

Cart::checkBuyableStockAmount()

这将删除可购买库存不足的购物车项目,并降低超出可购买数量的项目数量

$buyable1->qty = 0 // out of stock (will be removed)

$buyable2->qty = 5
$cartItem->qty = 7 // this exceeded it`s buyable qty

[
    [$buyable1], // deleted items
    [
        'from' => 7,
        'to' => 2,
        'items' => [$buyable2] // updated items qty
    ]
] = Cart::checkBuyableStockAmount()

刷新可购买项目对象

仅在会话中需要,这将使用最新的更改更新可购买对象,您可以使用此功能检查项目是否仍然有库存

// this initial values
$buyable->qty // 4
$item->buyable->qty // 4

// this after updating buyable qty
$buyable->qty // 10
$item->buyable->qty // 4 // still not updated

Cart::refreshItemsBuyableObjects()

$item->buyable->qty // 10 // updated

异常

事件

命令

这将删除数据库中旧的购物车项目,这些项目超过配置的deleteAfter

安排RemoveOldItemsCommand

protected function schedule(Schedule $schedule)
{
    $schedule->command('shoppingcart:destroy')->daily();
}

示例

$buyable = Product::find(1);

$item = $buyable->addToCart(10, 6, 32, ['height' => '340m']);
// or $item = Cart::add($buyable, 10, 6, 32, ['height' => '340m'])

$wishListItem = Cart::instance('wishlist')->add($buyable, 0);
$anotherWishListItem = Cart::instance('wishlist')->add($anotherBuyable, 0);

echo $item->size; // 6 , opt1 => size
echo $item->color; // 32 , opt2 => color

$item->increments(3);
echo $item->qty; // 13

// return to default instance and update item
Cart::instance()->update($item->id, ['height' => '260m']);

// find updated item
$item = Cart::find($item->id);
echo $item->options; // ['height' => '260m']

foreach (Cart::content() as $item) {
    echo $item->sub_total; // 2653.14    not formated
    echo $item_sub_total(); // 2,653.14  formated
}

// get all items subTotal
echo Cart::subTotal(); // 6532145.2
echo Cart::subTotal(true); // 6,532,145.20

echo Cart::instance('wishlist')->content()->count() // 2

测试

composer test

贡献

贡献是欢迎的,并将完全获得认可。请参阅CONTRIBUTING.md

许可

此项目采用MIT许可证 - 请参阅LICENSE文件以获取详细信息