mathewberry / cart

此软件包已被放弃,不再维护。没有建议的替代软件包。

为 Laravel 设计的简单易用的购物车

0.2.2 2017-02-09 18:48 UTC

This package is auto-updated.

Last update: 2020-01-21 13:13:31 UTC


README

购物车组件包括一个完全可集成的购物车,具有许多丰富的功能。

版本 0.2.0 支持 Laravel 5.4

安装

通过 composer 安装,请使用以下命令

composer require mathewberry/cart

接下来,添加

Cart` => \Mathewberry\Cart\Facades\Cart::class

config/app.php

功能

  • 获取小计
  • 获取总计
  • 获取运费
  • 获取优惠券金额
  • 获取数量
  • 获取购物车总计
  • 添加产品
  • 添加优惠券
  • 检查购物车是否有产品
  • 更新购物车
  • 从购物车中移除项目
  • 清空购物车

使用方法

获取产品

$product = Cart::get($product_id);

数组结构

// Example
[
    "id" => 24,
    "price" => 99.99,
    "quantity" => 2,
    "options" => [...]
]

显示

{{ $product['options']['name'] }} <small>{{ $product['options']['model'] }}</small><br>
<img src="{{ $product['options']['image'] }}">
<a href="{{ route('products', ['id' => $product['id']]) }}"><br>

用法

获取所有产品

$products = Cart::content();

数组结构

// Example
[
    [
        "id" => 24,
        "price" => 99.99,
        "quantity" => 2,
        "options" => [...]
    ],
    [
        "id" => 34,
        "price" => 79.99,
        "quantity" => 1,
        "options" => [...]
    ]
]

获取小计

// Return the total with shipping or voucher
Cart::subtotal();

添加新产品

/* DATABASE ROW
 *
 * Id: 24
 * Name: 'My Product'
 * Model: 'AAA900'
 * Price: 99.99
 *
 */

$product = \App\Models\Product::select(['id', 'model', 'price'])->find(24);
$product_id = $product->id;
$price = $product->price;
$quantity = 1;

// You may add any options you wish.
$options = [
     'name' => $product->name,
     'image' => asset('images/my-product-image.jpg'),
     'model' => $product->model
];

Cart::add($product_id, $price, $quantity, $options)

获取总计

// Returns the total of the cart
Cart::total();

获取运费

// Returns the delivery cost
Cart::delivery();

设置优惠券

// Example Data
$id = 24;
$code = "MB25OFF"; // 25% Off
$discount = 25.00; // 25%
$is_fixed = false; // Percentage
$display = '-' . number_format($discount, 2) . '%'; // How it should be displayed to the customer.

Cart::voucher($id, $code, $discount, $is_fixed, $display);

获取优惠券

Cart::voucher();

结构

[
    "id" => $voucher_id, // Voucher id
    "code" => $voucher_code, // Voucher code
    "display" => $voucher_display, // Customer friendly, e.g: "25%"
    "is_fixed" => $voucher_fixed, // Percentage voucher or fixed price
    "discount" => $voucher_discount_value // Value of voucher
]

显示

Code: {{ Cart::voucher()['code'] }}<br>
Deducted: {{ Cart::voucher()['display'] }}

有产品

// Returns true or false whether or not the product exists in the cart.
Cart::has($product_id);

获取产品总计

// Calculates the product price times quantity.
Cart::sum($product_id);

获取购物车数量

// Return a list of all the products in the cart.
Cart::products();

更新购物车项目

// Add one to the current quantity
Cart::update($product_id);
// Custom quantity to add to existing quantity
Cart::update($product_id, $quantity);

从购物车中移除

// Remove a specific product from the cart.
Cart::remove($product_id);

清空购物车

// Clear the cart of ALL of it's data.
Cart::clear();