卡玛罗 / 购物车
由卡玛罗·兰伯特增强的购物车包来自 CodeIgniter
Requires
- php: >=5.4.0
- illuminate/support: ~4
This package is auto-updated.
Last update: 2024-09-29 03:41:41 UTC
README
Laravel 购物车包基于 CodeIgniter 购物车库进行编码。从 CodeIgniter 到 Laravel,购物车 composer 包允许将商品添加到用户浏览网站时保持活跃的会话中。这些商品可以以标准的“购物车”格式检索和显示,使用户可以更新数量或从购物车中删除商品。请注意,购物车 composer 包仅提供核心“购物车”功能。它不提供运输、信用卡授权或其他处理组件。
- 所使用的类最初由 EllisLab 开发团队编写,然后当我需要同样
- 的类在 laravel 中使用时,我决定修改这个类并使其成为 Laravel 的 composer 包。
- 我还计划向这个类添加更多功能,例如
-
- 税收
-
- 优惠券
-
- 删除多个商品
-
- 检查商品是否存在
-
- 购物车小计
-
- 购物车数量:购物车中商品的总数
-
- 购物车 seIdentity:当我们在同一会话中使用不同的购物车标识符时,购物车标识符。
-
- 购物车 getIdentity:购物车标识符。
简介
购物车包允许将商品添加到会话中,该会话在用户浏览您的网站时保持活跃。这些商品可以以标准的“购物车”格式检索和显示,允许用户更新数量或从购物车中删除商品。
请注意,购物车包仅提供核心“购物车”方法。它不提供运输、信用卡授权或其他处理组件。
通过 Composer 安装此包。将以下内容添加到您的 composer.json
文件中:
"require-dev": { "kamaro/cart": "dev-master" }
接下来,运行 composer install --dev
下载它。
最后,将服务提供者添加到 app/config/app.php
中的 providers
数组。
'providers' => array( // ... 'Kamaro\Cart\CartServiceProvider' ) 'aliases' => array( // ... 'Cart'=> 'Kamaro\Cart\Facades\Cart' )
这样就完成了!运行 php artisan
查看三个新的 Guard 命令
要将商品添加到购物车,只需将包含商品信息的数组传递给 ```php cart->insert() 方法,如下所示
$data = array( 'id' => 'sku_123ABC', 'qty' => 1, 'price' => 39.95, 'name' => 'T-Shirt', 'options' => array('Size' => 'L', 'Color' => 'Red') ); cart::insert($data);
重要:上面提到的前四个数组索引(id、qty、price 和 name)是必需的。如果您省略任何其中一个,数据将不会保存到购物车中。第五个索引(options)是可选的。它旨在用于与您的产品关联的选项的情况。使用上面所示的数组作为选项。五个保留索引是
id - 您的商店中的每个产品都必须有一个唯一的标识符。这通常将是一个“sku”或其他此类标识符。qty - 购买的数量。price - 商品的单价。name - 商品的名称。options - 任何需要识别产品的附加属性。这些必须通过数组传递。除了上面的五个索引之外,还有两个保留词:rowid 和 subtotal。这些是由购物车类内部使用的,所以请勿将这些词用作向购物车中插入数据的索引名称。
您的数组可能包含其他数据。您在数组中包含的任何内容都将存储在会话中。然而,最好在您的所有产品中标准化数据,以便更容易在表中显示信息。
insert() 方法会在成功插入单个项目时返回 $rowid。
将多个商品添加到购物车
通过使用如下所示的多维数组,可以在一个动作中向购物车添加多个产品。这在您希望允许人们从同一页面上选择多个商品的情况下非常有用。
$data = array( array( 'id' => 'sku_123ABC', 'qty' => 1, 'price' => 39.95, 'name' => 'T-Shirt', 'options' => array('Size' => 'L', 'Color' => 'Red') ), array( 'id' => 'sku_567ZYX', 'qty' => 1, 'price' => 9.95, 'name' => 'Coffee Mug' ), array( 'id' => 'sku_965QRS', 'qty' => 1, 'price' => 29.95, 'name' => 'Shot Glass' ) ); cart::insert($data);
显示购物车
要显示购物车,您需要创建一个类似于以下代码的视图文件。 请注意,此示例使用表单辅助工具。
{{ Form::open(array('url' => 'ath/to/route/to/update')) }} <table cellpadding="6" cellspacing="1" style="width:100%" border="0"> <tr> <th>QTY</th> <th>Item Description</th> <th style="text-align:right">Item Price</th> <th style="text-align:right">Sub-Total</th> </tr> <?php $i = 1; ?> @foreach ($this->cart->contents() as $items): {{Form::text($i.'[rowid]', $items['rowid'])}} <tr> <td> {{Form::text(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5'))}}</td> <td> {{$items['name']}} @if (cart::has_options($items['rowid']) == TRUE): <p> @foreach (cart::product_options($items['rowid']) as $option_name => $option_value): <strong>{{$option_name}}:</strong> {{$option_value}} <br /> @endforeach; ?> </p> @endif; </td> <td style="text-align:right">{{cart::format_number($items['price']);}}</td> <td style="text-align:right">{{cart::format_number($items['subtotal']);}}</td> </tr> <?php $i++; ?> @endforeach; <tr> <td colspan="2"> </td> <td class="right"><strong>Total</strong></td> <td class="right">{{cart::format_number(cart::total())}}</td> </tr> </table> <p>{{Form::submit('Update your cart')}}</p> {{ Form::close() }}
更新购物车
要更新购物车中的信息,您必须传递一个包含行 ID 和数量的数组到
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);
行 ID 是什么?
行 ID 是在商品添加到购物车时由购物车代码生成的唯一标识符。创建唯一 ID 的原因是为了能够管理具有不同选项的相同产品。
例如,假设有人购买了两个相同的产品(相同的产品 ID),但尺寸不同。由于是同一件衬衫,这两个尺寸的产品 ID(以及其他属性)将相同。唯一的区别在于尺寸。因此,购物车必须有一种方法来识别这种差异,以便独立管理这两种尺寸的衬衫。它是通过根据产品 ID 和任何相关选项创建唯一的“行 ID”来实现的。
在几乎所有情况下,更新购物车将是用户通过“查看购物车”页面进行的,因此作为开发者,您不太可能需要担心“行 ID”,除了确保您的“查看购物车”页面包含此信息在隐藏的表单字段中,并确保在更新表单提交时将其传递给更新方法。请查看上面的“查看购物车”页面的构建以获取更多信息。
方法参考
cart::insert(); //Permits you to add items to the shopping cart, as outlined above.
cart::update(); //Permits you to update items in the shopping cart, as outlined above.
cart::total(); //Displays the total amount in the cart.
cart::total_items(); //Displays the total number of items in the cart.
cart::contents(); //Returns an array containing everything in the cart.
//Returns TRUE (boolean) if a particular row in the cart contains options. This method is designed to //be used in a loop with ```php cart->contents(), since you must pass the rowid to this method, //as shown in the Displaying the Cart example above. cart::has_options(rowid);
cart::product_options(rowid); // Returns an array of options for a particular product. This method is designed to be used in a loop with
cart::contents(); // since you must pass the rowid to this method, as shown in the Displaying the Cart example above.
cart::destroy();//Permits you to destroy the cart. This method will likely be called when you are finished processing the customer's order.