5 ways to implement shopping cart in laravel

1- shoppingcart

 

Laravel 5

"require": {
    "laravel/framework": "5.0.*",
    "gloudemans/shoppingcart": "~1.3"
}

Next, run the Composer update command from the Terminal:

composer update

Now all you have to do is add the service provider of the package and alias the package. To do this open your app/config/app.php file.

Add a new line to the service providers array:

'Gloudemans\Shoppingcart\ShoppingcartServiceProvider'

And finally add a new line to the aliases array:

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

Now you’re ready to start using the shoppingcart in your application.

 

Usage

The shoppingcart gives you the following methods to use:

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

Recommended :  Create an Ajax-powered contact form with jquery

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares