nat_bjorkgard / shopping_cart
laravel 4 的购物车
Requires
- php: >=5.3.0
- illuminate/support: ~4
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-24 15:02:10 UTC
README
为 Laravel 4 实现的一个简单的购物车。
安装
通过 Composer 安装该软件包。通过编辑项目的 composer.json
文件添加
"require": {
"laravel/framework": "4.1.*",
"nat_bjorkgard/shopping_cart": "dev-master"
}
接下来,从终端运行 Composer 更新命令
composer update
现在您只需要添加软件包的服务提供者和别名。为此,打开您的 app/config/app.php
文件。
向 service providers
数组中添加一行新内容
'Bjorkgard\Shoppingcart\ShoppingcartServiceProvider'
最后,向 aliases
数组中添加一行新内容
'Cart' => 'Bjorkgard\Shoppingcart\Facades\Cart',
现在您就可以开始在您的应用程序中使用购物车了。
概述
查看以下主题以了解有关购物车的更多信息
使用方法
购物车提供了以下方法供您使用
Cart::add()
/**
* Add a row to the cart
*
* @param string|Array $id Unique ID of the item|Item formatted as array|Array of items
* @param string $name Name of the item
* @param int $qty Item qty to add to the cart
* @param float $price Price of one item
* @param float $weight Weight of one item
* @param Array $options Array of additional options, such as 'size' or 'color'
*/
// Basic form
Cart::add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));
// Array form
Cart::add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'weight' => 250, 'options' => array('size' => 'large')));
// Batch method
Cart::add(array(
array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00, 'weight' => 12.5),
array('id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => array('size' => 'large'))
));
Cart::update()
/**
* Update the quantity of one row of the cart
*
* @param string $rowId The row id of the item you want to update
* @param integer|Array $attribute New quantity of the item|Array of attributes to update
* @return boolean
*/
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::update($rowId, 2);
OR
Cart::update($rowId, array('name' => 'Product 1'));
Cart::remove()
/**
* Remove a row from the cart
*
* @param string $rowId The row id of the item
* @return boolean
*/
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::remove($rowId);
Cart::get()
/**
* Get a row of the cart by its ID
*
* @param string $rowId The ID of the row to fetch
* @return CartRowCollection
*/
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::get($rowId);
Cart::content()
/**
* Get the cart content
*
* @return CartCollection
*/
Cart::content();
Cart::destroy()
/**
* Empty the cart
*
* @return boolean
*/
Cart::destroy();
Cart::total()
/**
* Get the price total
*
* @return float
*/
Cart::total();
Cart::count()
/**
* Get the number of items in the cart
*
* @param boolean $totalItems Get all the items (when false, will return the number of rows)
* @return int
*/
Cart::count(); // Total items
Cart::count(false); // Total rows
Cart::search()
/**
* Search if the cart has a item
*
* @param Array $search An array with the item ID and optional options
* @return Array|boolean
*/
Cart::search(array('id' => 1, 'options' => array('size' => 'L'))); // Returns an array of rowid(s) of found item(s) or false on failure
集合
如您所见,Cart::content()
和 Cart::get()
方法都返回一个集合,一个 CartCollection
和一个 CartRowCollection
。
这些集合扩展了 'native' Laravel 4 集合类,因此您也可以在购物车上使用您所知道的所有此类的其他方法。并且通过一些添加,可以轻松地处理您的购物车内容。
实例
现在该软件包也支持多个购物车实例。这种方式是这样的
您可以使用 Cart::instance('newInstance')
设置当前的购物车实例,此时,活动的购物车实例为 newInstance
,因此当您添加、删除或获取购物车的内容时,您将使用购物车的 newInstance
实例。如果您想切换实例,只需再次调用 Cart::instance('otherInstance')
即可,您将再次使用 otherInstance
。
以下是一个小例子
Cart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99);
// Get the content of the 'shopping' cart
Cart::content();
Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, array('size' => 'medium'));
// Get the content of the 'wishlist' cart
Cart::content();
// If you want to get the content of the 'shopping' cart again...
Cart::instance('shopping')->content();
// And the count of the 'wishlist' cart again
Cart::instance('wishlist')->count();
注意:请记住,在脚本执行期间,如果没有设置不同的实例,购物车将保持在最后设置的实例中。
注意2:默认的购物车实例称为 main
,因此当您不使用实例时,Cart::content();
与 Cart::instance('main')->content()
相同。
模型
新功能是关联模型与购物车中的项目。假设您在应用程序中有一个 Product
模型。通过新的 associate()
方法,您可以告诉购物车,购物车中的一个项目与 Product
模型相关联。
这样,您就可以直接从 CartRowCollection
访问您的模型了!
以下是一个示例
<?php
/**
* Let say we have a Product model that has a name and description.
*/
Cart::associate('Product')->add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));
$content = Cart::content();
foreach($content as $row)
{
echo 'You have ' . $row->qty . ' items of ' . $row->product->name . ' with description: "' . $row->product->description . '" in your cart.';
}
访问模型的键与您关联的模型名称相同(小写)。associate()
方法有一个可选的第二个参数,用于指定模型命名空间。
异常
当出现问题时代码包会抛出异常。这样,使用代码包进行调试或根据异常类型处理错误就更容易了。代码包可以抛出以下异常
异常 | 原因 |
---|---|
ShoppingcartInstanceException | 当未向 instance() 方法传递实例时 |
ShoppingcartInvalidItemException | 当新产品缺少其参数之一(id 、name 、qty 、price )时 |
ShoppingcartInvalidPriceException | 当传递非数值价格时 |
ShoppingcartInvalidWeightException | 当传递非数值重量时 |
ShoppingcartInvalidQtyException | 当传入非数字量时 |
ShoppingcartInvalidRowIDException | 当传入的 $rowId 不存在于当前购物车中时 |
ShoppingcartUnknownModelException | 当未知模型与购物车行相关联时 |
事件
购物车还内置了事件。有五个事件可供您监听。
事件 | 触发 |
---|---|
cart.add($item) | 当添加单个项目时 |
cart.batch($items) | 当添加一批项目时 |
cart.update($rowId) | 当购物车中的项目被更新时 |
cart.remove($rowId) | 当从购物车中移除项目时 |
cart.destroy() | 当购物车被销毁时 |
示例
以下是如何在表格中列出购物车内容的一个小示例
// Controller
Cart::add('192ao12', 'Product 1', 1, 9.99);
Cart::add('1239ad0', 'Product 2', 2, 5.95, array('size' => 'large'));
// View
<table>
<thead>
<tr>
<th>Product</th>
<th>Qty</th>
<th>Item Price</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<?php foreach($cart as $row) :?>
<tr>
<td>
<p><strong><?php echo $row->name;?></strong></p>
<p><?php echo ($row->options->has('size') ? $row->options->size : '');?></p>
</td>
<td><input type="text" value="<?php echo $row->qty;?>"></td>
<td>$<?php echo $row->price;?></td>
<td>$<?php echo $row->subtotal;?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>