devbuddy/cart

一个简单的购物车类,用于laravel从零开始构建电商网站。无需配置,只需下载即可使用。您可以非常容易地根据需求自定义功能,无需麻烦。

v1.0.7 2020-01-14 13:23 UTC

This package is auto-updated.

Last update: 2024-09-16 20:57:02 UTC


README

用于管理购物车的Laravel包。

通过以下方式安装包:composer require devbuddy/cart

安装成功后,将包配置到您的项目中。

添加需要使用购物车的地方

use Cart;

Laravel 5 & 6 购物车

为Laravel框架实现的购物车

安装

通过 Composer 安装包。

对于Laravel 5.1~:composer require "devbuddy/cart"

配置

  1. 打开 config/app.php 并将此行添加到您的 Service Providers 数组中。注意:如果您使用的是laravel 5.5或更高版本,它将自动通过其自动发现添加。

快速使用示例

// Quick Usage with the Product Model Association & User session binding

$Product = Product::find($productId); // assuming you have a Product model with id, name, description & price

// add the product to cart
$rowid = Cart::insert(array(
    'id' => $Product->id,
    'name' => $Product->name,
    'price' => $Product->price,
    'quantity' => 4,
    'options' => array()
));

// update the item on cart
Cart::update($rowid,[
	'quantity' => 2,
	'price' => 98.67
]);

// delete an item on cart
Cart::remove($rowid);

// view the cart items
$items = Cart::contents();
foreach($items as $row) {

	echo $row['id']; // row ID
	echo $row['name'];
	echo $row['qty'];
	echo $row['price'];
	
}

// FOR FULL USAGE, SEE BELOW..

##什么是行ID?行ID是在将项目添加到购物车时由购物车代码生成的唯一标识符。创建唯一ID的原因是为了管理具有不同选项的相同产品。

例如,假设有人购买了两个相同的产品(相同的商品ID),但尺寸不同。由于是同一件衬衫,两个尺寸的产品ID(和其他属性)将是相同的。唯一的区别是尺寸。因此,购物车必须有一种方法来识别这种差异,以便可以独立管理两种尺寸的衬衫。它通过基于产品ID及其关联的任何选项创建唯一的“行ID”来实现。

方法!

Cart::insert();
Cart::update();
Cart::total();
Cart::remove();
Cart::total_items();
Cart::contents();
Cart::get_item();
Cart::has_options();
Cart::product_options();
Cart::format_number();
Cart::destroy();

下面有更多示例

在购物车中添加项目:Cart::insert()

您有几种方法可以在购物车中添加项目,如下所示

// Simplest form to add item on your cart
Cart::add(455, 'Sample Item', 100.99, 2, array());

// array format
Cart::insert(array(
    'id' => 456, // inique row ID
    'name' => 'Sample Item',
    'price' => 67.99,
    'quantity' => 4,
    'options' => array()
));

// add multiple items at one time
Cart::insert(array(
  array(
      'id' => 456,
      'name' => 'Sample Item 1',
      'price' => 67.99,
      'quantity' => 4,
      'options' => array()
  ),
  array(
      'id' => 568,
      'name' => 'Sample Item 2',
      'price' => 69.25,
      'quantity' => 4,
      'options' => array(
        'size' => 'L',
        'color' => 'blue'
      )
  ),
));
// NOTE:
// Please keep in mind that when adding an item on cart, the "id" should be unique as it serves as
// row identifier as well. If you provide same ID, it will assume the operation will be an update to its quantity
// to avoid cart item duplicates

更新购物车中的项目:Cart::update()

更新购物车中的项目非常简单

$data = array(
        'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
        'qty'   => 3
);

Cart::update($data);

// Or a multi-dimensional array

$data = array(
        array(
                'rowid'   => 'b99ccdf16028f015540f341130b6d8ec',
                'qty'     => 3
        ),
        array(
                'rowid'   => 'xw82g9q3r495893iajdh473990rikw23',
                'qty'     => 4
        ),
        array(
                'rowid'   => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
                'qty'     => 2
        )
);

Cart::update($data);
//You may also update any property you have previously defined when inserting the item such as options, price or other custom fields.
$data = array(
        'rowid'  => 'b99ccdf16028f015540f341130b6d8ec',
        'qty'    => 1,
        'price'  => 49.95,
        'coupon' => NULL
);

$this->cart->update($data);

从购物车中删除项目:Cart::remove()

从购物车中删除项目非常简单

/**
 * removes an item on cart by item ID
 *
 * @param $rowid
 */

Cart::remove($rowid);

获取购物车中的项目:Cart::contents()

Cart::contents();

对于如何进行自定义数据库存储仍然感到困惑吗?或者也许在做多个购物车实例?通过git联系我。

许可证

Laravel购物车是开源软件,根据MIT许可证授权。

免责声明

本软件按“原样”提供,任何明示或暗示的保证,包括但不限于适销性和针对特定目的的适用性保证,均被排除在外。在任何情况下,作者或任何贡献者均不对任何直接、间接、偶然、特殊、示范性或后果性的损害(包括但不限于替代商品或服务的采购;使用、数据或利润的损失;或业务中断)承担责任,无论此类损害是由于何种原因或何种理论责任,即使已通知此类损害的可能性。