Shoppingcart 購物車

2018-02-24 15:53 更新

還是拿來主義編程思想,既然別人實現(xiàn)了,就不再造輪子了。

LaravelShoppingcart是Laravel下的一個購物車實現(xiàn),項目地址:

https://github.com/Crinsane/LaravelShoppingcart

文檔翻譯:

針對Laravel 4的簡單購物車實現(xiàn)。

安裝

通過composer安裝,編輯 composer.json 文件,如下:

Laravel4.2及以下

"require": {
"laravel/framework": "4.2.*",
"gloudemans/shoppingcart": "~1.2"
}

Laravel5

"require": {
"laravel/framework": "5.0.*",
"gloudemans/shoppingcart": "dev-master"
}

接著運行更新命令

composer update

添加以下代碼到 app/config/app.phpservice providers 數(shù)組里

'Gloudemans\Shoppingcart\ShoppingcartServiceProvider'

添加以下代碼到aliases數(shù)組中

'Cart' => 'Gloudemans\Shoppingcart\Facades\Cart',

完成以上工作就可以在你的項目中使用了。

使用

Shoppingcart提供了以下的可用方法:

Cart:add()

/**
* Add a row to the cart
* 向購物車添加新行
*
* @param string|Array $id Unique ID of the item|Item formated as array|Array of items
* -參數(shù) 字符串|數(shù)組 $id item的唯一id | Item格式化成數(shù)組 | items 數(shù)組
* @param string $name Name of the item
* -參數(shù) 字符串 $name item 名稱
* @param int $qty Item qty to add to the cart
* -param int $qty 添加到購物車的item的數(shù)量
* @param float $price Price of one item
* -param float $price item 價格
* @param Array $options Array of additional options, such as 'size' or 'color'
* @param Array $options 附加參數(shù)數(shù)組, 諸如'size' 或 'color'
*/

// Basic form 基礎表單
Cart::add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));

// Array form 數(shù)組表單
Cart::add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => array('size' => 'large')));

// Batch method 批量方法
Cart::add(array(
array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00),
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 rowid 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 rowid 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

Collections

顯而易見,Cart::content()Cart::get()返回一個集合,CartCollectionCartRowCollection。

這些集合擴展了原始的Laravel 4collection類,所以這個類的所有方法同樣的可以被使用在購物車類中。

實例

現(xiàn)在擴展包可以支持多個購物車實例,工作方式像這樣:

你可以使用Cart::instance(‘newInstance’)設置當前的購物車實例,此刻,有效的購物車實例是newInstance,所以你添加、移除或獲取購物車內(nèi)容,操作的都是newInstance。如果你需要選擇其它實例,你只需要調(diào)用Cart::instance(‘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();

備注1:如果你不重新設置,購物車永遠是最后一個實例

備注2:默認的購物車實例名稱是mian,所以Cart::content();等同于Cart::instance(‘main’)->content()。

Models

一個新特性是可以使為購物車的item關聯(lián)一個模型。假設在你的應用中有一個product模型,通過新的associate()方法,可以通知購物車里的項關聯(lián)到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.';
}

訪問模型的主鍵和關聯(lián)模型的名稱相同。associate()還有第二個參數(shù),是可選的,用于指定模型的命名空間。

異常錯誤

當發(fā)生錯誤時,購物車模塊將拋出異常。使用購物車模塊可以很容易的調(diào)試錯誤,或者基于異常的類型處理錯誤。購物車模塊可以拋出以下異常:

Exception Reason
ShoppingcartInstanceException When no instance is passed to the instance() method 沒有實例傳遞給instance()方法
ShoppingcartInvalidItemException When a new product misses one of it’s arguments (id,name, qty, price) 當新產(chǎn)品缺少一個參數(shù)
ShoppingcartInvalidPriceException When a non-numeric price is passed當非數(shù)值的價格被傳遞
ShoppingcartInvalidQtyException When a non-numeric quantity is passed當非數(shù)值的數(shù)量被傳遞
ShoppingcartInvalidRowIDException When the $rowId that got passed doesn’t exists in the current cart當當前的購物車并不存在被傳來的$roeid
ShoppingcartUnknownModelException When an unknown model is associated to a cart row當一個未知的模型被關聯(lián)到購物車的行

Events

下面是購物車監(jiān)聽的事件:

Event Fired
cart.add($item) When a single item is added當單個item被添加
cart.batch($items) When a batch of items is added當一批item被添加
cart.update($rowId) When an item in the cart is updated購物車單個item跟更新
cart.remove($rowId) When an item is removed from the cart當一個item被從購物車中移除
cart.destroy() When the cart is destroyed當購物車被清空

示例:

下面的示例展示了如果在table中呈現(xiàn)購物車的內(nèi)容

// 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>

原文地址:http://www.zhangxihai.cn/archives/189

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號