mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-23 15:41:24 +00:00
Updated readme with syntax highlighting
This commit is contained in:
228
README.md
228
README.md
@@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
Install the package through [Composer](http://getcomposer.org/). Edit your project's `composer.json` file by adding:
|
Install the package through [Composer](http://getcomposer.org/). Edit your project's `composer.json` file by adding:
|
||||||
|
|
||||||
"require": {
|
```php
|
||||||
"laravel/framework": "4.0.*",
|
"require": {
|
||||||
"gloudemans/shoppingcart": "dev-master"
|
"laravel/framework": "4.0.*",
|
||||||
}
|
"gloudemans/shoppingcart": "dev-master"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Next, run the Composer update command from the Terminal:
|
Next, run the Composer update command from the Terminal:
|
||||||
|
|
||||||
@@ -45,101 +47,117 @@ Cart::add('293ad', 'Product 1', 1, 9.99, array('size' => 'large'));
|
|||||||
|
|
||||||
**Cart::addBatch()**
|
**Cart::addBatch()**
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* Add multiple rows to the cart
|
/**
|
||||||
*
|
* Add multiple rows to the cart
|
||||||
* @param Array $items An array of items to add, use array keys corresponding to the 'add' method's parameters
|
*
|
||||||
*/
|
* @param Array $items An array of items to add, use array keys corresponding to the 'add' method's parameters
|
||||||
|
*/
|
||||||
|
|
||||||
Cart::addBatch(array(
|
Cart::addBatch(array(
|
||||||
array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00),
|
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'))
|
array('id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => array('size' => 'large'))
|
||||||
));
|
));
|
||||||
|
```
|
||||||
|
|
||||||
**Cart::update()**
|
**Cart::update()**
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* Update the quantity of one row of the cart
|
/**
|
||||||
*
|
* 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
|
* @param string $rowId The rowid of the item you want to update
|
||||||
* @return boolean
|
* @param integer|Array $attribute New quantity of the item|Array of attributes to update
|
||||||
*/
|
* @return boolean
|
||||||
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
*/
|
||||||
|
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
||||||
|
|
||||||
Cart::update($rowId, 2);
|
Cart::update($rowId, 2);
|
||||||
|
|
||||||
OR
|
OR
|
||||||
|
|
||||||
Cart::update($rowId, array('name' => 'Product 1'));
|
Cart::update($rowId, array('name' => 'Product 1'));
|
||||||
|
```
|
||||||
|
|
||||||
**Cart::remove()**
|
**Cart::remove()**
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* Remove a row from the cart
|
/**
|
||||||
*
|
* Remove a row from the cart
|
||||||
* @param string $rowId The rowid of the item
|
*
|
||||||
* @return boolean
|
* @param string $rowId The rowid of the item
|
||||||
*/
|
* @return boolean
|
||||||
|
*/
|
||||||
|
|
||||||
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
||||||
|
|
||||||
Cart::remove($rowId);
|
Cart::remove($rowId);
|
||||||
|
```
|
||||||
|
|
||||||
**Cart::get()**
|
**Cart::get()**
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* Get a row of the cart by its ID
|
/**
|
||||||
*
|
* Get a row of the cart by its ID
|
||||||
* @param string $rowId The ID of the row to fetch
|
*
|
||||||
* @return CartRowCollection
|
* @param string $rowId The ID of the row to fetch
|
||||||
*/
|
* @return CartRowCollection
|
||||||
|
*/
|
||||||
|
|
||||||
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
||||||
|
|
||||||
Cart::get($rowId);
|
Cart::get($rowId);
|
||||||
|
```
|
||||||
|
|
||||||
**Cart::content()**
|
**Cart::content()**
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* Get the cart content
|
/**
|
||||||
*
|
* Get the cart content
|
||||||
* @return CartCollection
|
*
|
||||||
*/
|
* @return CartCollection
|
||||||
|
*/
|
||||||
|
|
||||||
Cart::content();
|
Cart::content();
|
||||||
|
```
|
||||||
|
|
||||||
**Cart::destroy()**
|
**Cart::destroy()**
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* Empty the cart
|
/**
|
||||||
*
|
* Empty the cart
|
||||||
* @return boolean
|
*
|
||||||
*/
|
* @return boolean
|
||||||
|
*/
|
||||||
|
|
||||||
Cart::destroy();
|
Cart::destroy();
|
||||||
|
```
|
||||||
|
|
||||||
**Cart::total()**
|
**Cart::total()**
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* Get the price total
|
/**
|
||||||
*
|
* Get the price total
|
||||||
* @return float
|
*
|
||||||
*/
|
* @return float
|
||||||
|
*/
|
||||||
|
|
||||||
Cart::total();
|
Cart::total();
|
||||||
|
```
|
||||||
|
|
||||||
**Cart::count()**
|
**Cart::count()**
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* Get the number of items in the cart
|
/**
|
||||||
*
|
* 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
|
* @param boolean $totalItems Get all the items (when false, will return the number of rows)
|
||||||
*/
|
* @return int
|
||||||
|
*/
|
||||||
|
|
||||||
Cart::count(); // Total items
|
Cart::count(); // Total items
|
||||||
Cart::count(false); // Total rows
|
Cart::count(false); // Total rows
|
||||||
|
```
|
||||||
|
|
||||||
## Collections
|
## Collections
|
||||||
|
|
||||||
@@ -156,21 +174,23 @@ If you want to switch instances, you just call `Cart::instance('otherInstance')`
|
|||||||
|
|
||||||
So a little example:
|
So a little example:
|
||||||
|
|
||||||
Cart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99);
|
```php
|
||||||
|
Cart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99);
|
||||||
|
|
||||||
// Get the content of the 'shopping' cart
|
// Get the content of the 'shopping' cart
|
||||||
Cart::content();
|
Cart::content();
|
||||||
|
|
||||||
Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, array('size' => 'medium'));
|
Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, array('size' => 'medium'));
|
||||||
|
|
||||||
// Get the content of the 'wishlist' cart
|
// Get the content of the 'wishlist' cart
|
||||||
Cart::content();
|
Cart::content();
|
||||||
|
|
||||||
// If you want to get the content of the 'shopping' cart again...
|
// If you want to get the content of the 'shopping' cart again...
|
||||||
Cart::instance('shopping')->content();
|
Cart::instance('shopping')->content();
|
||||||
|
|
||||||
// And the count of the 'wishlist' cart again
|
// And the count of the 'wishlist' cart again
|
||||||
Cart::instance('wishlist')->count();
|
Cart::instance('wishlist')->count();
|
||||||
|
```
|
||||||
|
|
||||||
N.B. Keep in mind that the cart stays in the last set instance for as long as you don't set a different one during script execution.
|
N.B. Keep in mind that the cart stays in the last set instance for as long as you don't set a different one during script execution.
|
||||||
|
|
||||||
@@ -180,38 +200,40 @@ N.B.2 The default cart instance is called `main`, so when you're not using insta
|
|||||||
|
|
||||||
Below is a little example of how to list the cart content in a table:
|
Below is a little example of how to list the cart content in a table:
|
||||||
|
|
||||||
// Controller
|
```php
|
||||||
|
// Controller
|
||||||
|
|
||||||
Cart::add('192ao12', 'Product 1', 1, 9.99);
|
Cart::add('192ao12', 'Product 1', 1, 9.99);
|
||||||
Cart::add('1239ad0', 'Product 2', 2, 5.95, array('size' => 'large'));
|
Cart::add('1239ad0', 'Product 2', 2, 5.95, array('size' => 'large'));
|
||||||
|
|
||||||
// View
|
// View
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Product</th>
|
<th>Product</th>
|
||||||
<th>Qty</th>
|
<th>Qty</th>
|
||||||
<th>Item Price</th>
|
<th>Item Price</th>
|
||||||
<th>Subtotal</th>
|
<th>Subtotal</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
|
||||||
<tbody>
|
<tbody>
|
||||||
|
|
||||||
<?php foreach($cart as $row) :?>
|
<?php foreach($cart as $row) :?>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<p><strong><?php echo $row->name;?></strong></p>
|
<p><strong><?php echo $row->name;?></strong></p>
|
||||||
<p><?php echo ($row->options->has('size') ? $row->options->size : '');?></p>
|
<p><?php echo ($row->options->has('size') ? $row->options->size : '');?></p>
|
||||||
</td>
|
</td>
|
||||||
<td><input type="text" value="<?php echo $row->qty;?>"></td>
|
<td><input type="text" value="<?php echo $row->qty;?>"></td>
|
||||||
<td>$<?php echo $row->price;?></td>
|
<td>$<?php echo $row->price;?></td>
|
||||||
<td>$<?php echo $row->subtotal;?></td>
|
<td>$<?php echo $row->subtotal;?></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<?php endforeach;?>
|
<?php endforeach;?>
|
||||||
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user