subbly / shoppingcart
Subbly 购物车
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-28 17:12:36 UTC
README
Subbly CMS 的简单购物车实现,fork自 gloudemans/shoppingcart.
安装
通过 Composer 安装此包。编辑项目的 composer.json 文件,添加以下内容:
Laravel 4.2 及以下版本
"require": { "laravel/framework": "4.2.*", "gloudemans/shoppingcart": "~1.2" }
接下来,从终端运行 Composer 更新命令
composer update
现在,您只需将包的服务提供者添加到您的应用程序中,并设置别名。为此,打开您的 app/config/app.php 文件。
向 service providers 数组添加新行
'Gloudemans\Shoppingcart\ShoppingcartServiceProvider'
最后,向 aliases 数组添加新行
'Cart' => 'Gloudemans\Shoppingcart\Facades\Cart',
现在,您已准备好开始在应用程序中使用购物车了。
概述
查看以下主题之一,以了解更多关于 LaravelShoppingcart 的信息
使用方法
购物车提供了以下方法供您使用
Cart::add()
/** * Add a row to the cart * * @param string|Array $id Unique ID of the item|Item formated 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 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, '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
集合
如您所见,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() 方法有一个可选的第二个参数,用于指定模型命名空间。
异常
如果出现错误,购物车包将抛出异常。这样,您可以更容易地使用购物车包调试代码,或根据异常类型处理错误。购物车包可以抛出以下异常
事件
购物车还内置了事件。目前有五个事件可供您监听。
示例
以下是如何在表中列出购物车内容的简单示例
// 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>