A simple shopping cart implementation for Laravel

Overview

LaravelShoppingcart

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

A simple shoppingcart implementation for Laravel.

Installation

Install the package through Composer.

Run the Composer require command from the Terminal:

composer require gloudemans/shoppingcart

If you're using Laravel 5.5, this is all there is to do.

Should you still be on version 5.4 of Laravel, the final steps for you are to add the service provider of the package and alias the package. To do this open your config/app.php file.

Add a new line to the providers array:

Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class

And optionally add a new line to the aliases array:

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

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

As of version 2 of this package it's possibly to use dependency injection to inject an instance of the Cart class into your controller or other class

Overview

Look at one of the following topics to learn more about LaravelShoppingcart

Usage

The shoppingcart gives you the following methods to use:

Cart::add()

Adding an item to the cart is really simple, you just use the add() method, which accepts a variety of parameters.

In its most basic form you can specify the id, name, quantity, price of the product you'd like to add to the cart.

Cart::add('293ad', 'Product 1', 1, 9.99);

As an optional fifth parameter you can pass it options, so you can add multiple items with the same id, but with (for instance) a different size.

Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);

The add() method will return an CartItem instance of the item you just added to the cart.

Maybe you prefer to add the item using an array? As long as the array contains the required keys, you can pass it to the method. The options key is optional.

Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);

New in version 2 of the package is the possibility to work with the Buyable interface. The way this works is that you have a model implement the Buyable interface, which will make you implement a few methods so the package knows how to get the id, name and price from your model. This way you can just pass the add() method a model and the quantity and it will automatically add it to the cart.

As an added bonus it will automatically associate the model with the CartItem

Cart::add($product, 1, ['size' => 'large']);

As an optional third parameter you can add options.

Cart::add($product, 1, ['size' => 'large']);

Finally, you can also add multipe items to the cart at once. You can just pass the add() method an array of arrays, or an array of Buyables and they will be added to the cart.

When adding multiple items to the cart, the add() method will return an array of CartItems.

Cart::add([
  ['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00],
  ['id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => ['size' => 'large']]
]);

Cart::add([$product1, $product2]);

Cart::update()

To update an item in the cart, you'll first need the rowId of the item. Next you can use the update() method to update it.

If you simply want to update the quantity, you'll pass the update method the rowId and the new quantity:

$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::update($rowId, 2); // Will update the quantity

If you want to update more attributes of the item, you can either pass the update method an array or a Buyable as the second parameter. This way you can update all information of the item with the given rowId.

Cart::update($rowId, ['name' => 'Product 1']); // Will update the name

Cart::update($rowId, $product); // Will update the id, name and price

Cart::remove()

To remove an item for the cart, you'll again need the rowId. This rowId you simply pass to the remove() method and it will remove the item from the cart.

$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::remove($rowId);

Cart::get()

If you want to get an item from the cart using its rowId, you can simply call the get() method on the cart and pass it the rowId.

$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::get($rowId);

Cart::content()

Of course you also want to get the carts content. This is where you'll use the content method. This method will return a Collection of CartItems which you can iterate over and show the content to your customers.

Cart::content();

This method will return the content of the current cart instance, if you want the content of another instance, simply chain the calls.

Cart::instance('wishlist')->content();

Cart::destroy()

If you want to completely remove the content of a cart, you can call the destroy method on the cart. This will remove all CartItems from the cart for the current cart instance.

Cart::destroy();

Cart::total()

The total() method can be used to get the calculated total of all items in the cart, given there price and quantity.

Cart::total();

The method will automatically format the result, which you can tweak using the three optional parameters

Cart::total($decimals, $decimalSeperator, $thousandSeperator);

You can set the default number format in the config file.

If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the total property $cart->total

Cart::tax()

The tax() method can be used to get the calculated amount of tax for all items in the cart, given there price and quantity.

Cart::tax();

The method will automatically format the result, which you can tweak using the three optional parameters

Cart::tax($decimals, $decimalSeperator, $thousandSeperator);

You can set the default number format in the config file.

If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the tax property $cart->tax

Cart::subtotal()

The subtotal() method can be used to get the total of all items in the cart, minus the total amount of tax.

Cart::subtotal();

The method will automatically format the result, which you can tweak using the three optional parameters

Cart::subtotal($decimals, $decimalSeperator, $thousandSeperator);

You can set the default number format in the config file.

If you're not using the Facade, but use dependency injection in your (for instance) Controller, you can also simply get the subtotal property $cart->subtotal

Cart::count()

If you want to know how many items there are in your cart, you can use the count() method. This method will return the total number of items in the cart. So if you've added 2 books and 1 shirt, it will return 3 items.

Cart::count();

Cart::search()

To find an item in the cart, you can use the search() method.

This method was changed on version 2

Behind the scenes, the method simply uses the filter method of the Laravel Collection class. This means you must pass it a Closure in which you'll specify you search terms.

If you for instance want to find all items with an id of 1:

$cart->search(function ($cartItem, $rowId) {
	return $cartItem->id === 1;
});

As you can see the Closure will receive two parameters. The first is the CartItem to perform the check against. The second parameter is the rowId of this CartItem.

The method will return a Collection containing all CartItems that where found

This way of searching gives you total control over the search process and gives you the ability to create very precise and specific searches.

Collections

On multiple instances the Cart will return to you a Collection. This is just a simple Laravel Collection, so all methods you can call on a Laravel Collection are also available on the result.

As an example, you can quicky get the number of unique products in a cart:

Cart::content()->count();

Or you can group the content by the id of the products:

Cart::content()->groupBy('id');

Instances

The packages supports multiple instances of the cart. The way this works is like this:

You can set the current instance of the cart by calling Cart::instance('newInstance'). From this moment, the active instance of the cart will be newInstance, so when you add, remove or get the content of the cart, you're work with the newInstance instance of the cart. If you want to switch instances, you just call Cart::instance('otherInstance') again, and you're working with the otherInstance again.

So a little example:

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, ['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();

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.2 The default cart instance is called default, so when you're not using instances,Cart::content(); is the same as Cart::instance('default')->content().

Models

Because it can be very convenient to be able to directly access a model from a CartItem is it possible to associate a model with the items in the cart. Let's say you have a Product model in your application. With the associate() method, you can tell the cart that an item in the cart, is associated to the Product model.

That way you can access your model right from the CartItem!

The model can be accessed via the model property on the CartItem.

If your model implements the Buyable interface and you used your model to add the item to the cart, it will associate automatically.

Here is an example:

// First we'll add the item to the cart.
$cartItem = Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);

// Next we associate a model with the item.
Cart::associate($cartItem->rowId, 'Product');

// Or even easier, call the associate method on the CartItem!
$cartItem->associate('Product');

// You can even make it a one-liner
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large'])->associate('Product');

// Now, when iterating over the content of the cart, you can access the model.
foreach(Cart::content() as $row) {
	echo 'You have ' . $row->qty . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.';
}

Database

Configuration

To save cart into the database so you can retrieve it later, the package needs to know which database connection to use and what the name of the table is. By default the package will use the default database connection and use a table named shoppingcart. If you want to change these options, you'll have to publish the config file.

php artisan vendor:publish --provider="Gloudemans\Shoppingcart\ShoppingcartServiceProvider" --tag="config"

This will give you a cart.php config file in which you can make the changes.

To make your life easy, the package also includes a ready to use migration which you can publish by running:

php artisan vendor:publish --provider="Gloudemans\Shoppingcart\ShoppingcartServiceProvider" --tag="migrations"

This will place a shoppingcart table's migration file into database/migrations directory. Now all you have to do is run php artisan migrate to migrate your database.

Storing the cart

To store your cart instance into the database, you have to call the store($identifier) method. Where $identifier is a random key, for instance the id or username of the user.

Cart::store('username');

// To store a cart instance named 'wishlist'
Cart::instance('wishlist')->store('username');

Restoring the cart

If you want to retrieve the cart from the database and restore it, all you have to do is call the restore($identifier) where $identifier is the key you specified for the store method.

Cart::restore('username');

// To restore a cart instance named 'wishlist'
Cart::instance('wishlist')->restore('username');

Exceptions

The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:

Exception Reason
CartAlreadyStoredException When trying to store a cart that was already stored using the specified identifier
InvalidRowIDException When the rowId that got passed doesn't exists in the current cart instance
UnknownModelException When you try to associate an none existing model to a CartItem.

Events

The cart also has events build in. There are five events available for you to listen for.

Event Fired Parameter
cart.added When an item was added to the cart. The CartItem that was added.
cart.updated When an item in the cart was updated. The CartItem that was updated.
cart.removed When an item is removed from the cart. The CartItem that was removed.
cart.stored When the content of a cart was stored. -
cart.restored When the content of a cart was restored. -

Example

Below is a little example of how to list the cart content in a table:

// Add some items in your Controller.
Cart::add('192ao12', 'Product 1', 1, 9.99);
Cart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);

// Display the content in a View.
<table>
   	<thead>
       	<tr>
           	<th>Product</th>
           	<th>Qty</th>
           	<th>Price</th>
           	<th>Subtotal</th>
       	</tr>
   	</thead>

   	<tbody>

   		<?php foreach(Cart::content() 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->total; ?></td>
       		</tr>

	   	<?php endforeach;?>

   	</tbody>
   	
   	<tfoot>
   		<tr>
   			<td colspan="2">&nbsp;</td>
   			<td>Subtotal</td>
   			<td><?php echo Cart::subtotal(); ?></td>
   		</tr>
   		<tr>
   			<td colspan="2">&nbsp;</td>
   			<td>Tax</td>
   			<td><?php echo Cart::tax(); ?></td>
   		</tr>
   		<tr>
   			<td colspan="2">&nbsp;</td>
   			<td>Total</td>
   			<td><?php echo Cart::total(); ?></td>
   		</tr>
   	</tfoot>
</table>
Comments
  • Gloudemans \ Shoppingcart \ Exceptions \ ShoppingcartInvalidRowIDException

    Gloudemans \ Shoppingcart \ Exceptions \ ShoppingcartInvalidRowIDException

    I'm getting the above message when trying to remove an item from my cart.

    < a href="/cart/remove/{{ $cart_item->rowid }}">Remove

    Which is wrapped in a foreach that comes from:

    $carts = Cart::content();

    And i'm adding content like so:

    $cartContents = Cart::add(array('id' => randomNumber(), 'name' => $input['product_name'], 'qty' => 1, 'price' => $price));

    But when I do Cart::remove($cart_item->rowid) i get the above error?

    Any idea's?

    opened by lstables 41
  • Cart gettin cleared on refresh

    Cart gettin cleared on refresh

    Hi! i am new with this, but i have a question:

    I saw when i restart, my Cart instance is getting cleared, is that normal? I mean, how can i do if I want to persist Cart instance between pages?

    opened by quetool 24
  • search() must be an instance of Closure

    search() must be an instance of Closure

    I have this code to delete an item

    $rowId = Cart::search(array('id' => Request::get('product_id'))); Cart::remove($rowId[0]);

    but it gives me this error

    "Argument 1 passed to Gloudemans\Shoppingcart\Cart::search() must be an instance of Closure, array given, called in "

    the code of deleting an item is not working, can someone guide or post it here the proper code to delete an item from the cart ?

    any help is highly appreciated.

    opened by stocksph 24
  • Cart::content() is empty when redirecting

    Cart::content() is empty when redirecting

    class CartController extends Controller
    {
    
    	public function add(Request $request)
    	{
    		//Cart::add('192ao12', 'Product 1', 1, 9.99);
    		//Cart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);
    		//Cart::destroy();
    		//
    		$id 			= strtotime("now");
    		$product 		= Product::find($request->product_id); 
    		$product_name 	= $request->product_name;
    		Cart::add($id, $product_name, 1, $product->price);
    		return redirect('/cart');
    		//dump(Cart::content());
    	}
    
    	public function cart()
    	{
    		return view('FrontEnd::cart.index');
    	}
    
    }
    

    when i dump in add() cart->content is showing, but when redirecting there are empty,

    how can i store it?

    sory for my englist

    opened by anggagewor 18
  • Failed to install on Laravel 5.8

    Failed to install on Laravel 5.8

    $ composer require gloudemans/shoppingcart Using version ^2.6 for gloudemans/shoppingcart ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages.

    Problem 1 - Installation request for gloudemans/shoppingcart ^2.6 -> satisfiable by gloudemans/shoppingcart[2.6.0]. - Conclusion: remove laravel/framework v5.8.10 - Conclusion: don't install laravel/framework v5.8.10 - gloudemans/shoppingcart 2.6.0 requires illuminate/events 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.|| 5.6. || 5.7.* -> satisfiable by illuminate/events[5.1.x-dev, 5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, 5.6.x-dev, 5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, v5.1.1, v5.1.13, v5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.41, v5.1.6, v5.1.8, v5.2.0, v5.2.19, v5.2.21, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.31, v5.2.32, v5.2.37, v5.2.43, v5.2.45, v5.2.6, v5.2.7, v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9, v5.5.0, v5.5.16, v5.5.17, v5.5.2, v5.5.28, v5.5.33, v5.5.34, v5.5.35, v5.5.36, v5.5.37, v5.5.39, v5.5.40, v5.5.41, v5.5.43, v5.5.44, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.39, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.26, v5.7.27, v5.7.28, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9]. - don't install illuminate/events 5.1.x-dev|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.2.x-dev|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.3.x-dev|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.4.x-dev|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.5.x-dev|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.6.x-dev|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.7.17|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.7.18|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.7.19|don't install laravel/framework v5.8.10 - don't install illuminate/events 5.7.x-dev|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.1|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.13|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.16|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.2|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.20|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.22|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.25|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.28|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.30|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.31|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.41|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.6|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.1.8|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.0|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.19|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.21|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.24|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.25|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.26|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.27|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.28|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.31|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.32|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.37|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.43|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.45|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.6|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.2.7|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.3.0|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.3.16|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.3.23|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.3.4|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.4.0|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.4.13|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.4.17|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.4.19|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.4.27|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.4.36|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.4.9|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.0|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.16|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.17|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.2|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.28|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.33|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.34|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.35|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.36|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.37|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.39|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.40|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.41|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.43|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.5.44|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.0|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.1|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.10|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.11|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.12|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.13|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.14|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.15|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.16|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.17|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.19|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.2|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.20|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.21|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.22|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.23|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.24|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.25|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.26|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.27|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.28|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.29|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.3|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.30|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.31|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.32|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.33|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.34|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.35|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.36|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.37|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.38|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.39|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.4|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.5|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.6|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.7|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.8|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.6.9|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.0|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.1|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.10|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.11|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.15|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.2|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.20|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.21|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.22|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.23|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.26|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.27|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.28|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.3|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.4|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.5|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.6|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.7|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.8|don't install laravel/framework v5.8.10 - don't install illuminate/events v5.7.9|don't install laravel/framework v5.8.10 - Installation request for laravel/framework (locked at v5.8.10, required as 5.8.*) -> satisfiable by laravel/framework[v5.8.10].

    Installation failed, reverting ./composer.json to its original content.

    opened by devzakir 16
  • Update the stored cart in database.

    Update the stored cart in database.

    How to update the stored cart. I want to update cart which has saved before when i have added single product into to the cart it save the cart but when i want to add more product into the same saved cart. It generate a error the identifier already exist.

    opened by shankhadevpadam 16
  • Cannot retrieve cart

    Cannot retrieve cart

    Hi! I have a problem in accessing the cart content with another function in my controller. The cart content was empty. My code goes like this:

    function addToCart(){
          $data = $_GET;
          $cart = Cart::destroy();
         $objId = $data['data']['objDetails']['objectId'];
            foreach ($data['data'] as $key => $value) {
                switch ($key) {
                    case 'photography':
                        $ids = explode(',', $value['id']);
                        foreach ($ids as $idKey => $idVal) {
                            $value['object_id'] = $objId;
                            $productDetails = $this->productRepo->getProductById($idVal);
    
                            $product = array(
                                    'id' => $idVal,
                                    'qty' => 1,
                                    'price' => $productDetails['price'],
                                    'name' => $productDetails['name'],
                                    'details' => $value
                                );
                            Cart::add($idVal, $productDetails['name'], 1, $productDetails['price'], array('photoComment'=>$value['photoComment'],'objectId'=> $objId, 'preferenceDate'=> $value['preference_date']));    
                        break;
                        default:
                             # code...
                        break;
                }
            }
         echo $this->getHtml();
      };
    
    function getHtml(){
             $html = "";
            $cartItems = Cart::content();
            $html .= "<strong>No. of Products on Cart : ".Cart::count()."</strong> <hr><div class='table-responsive'>";
                $html .= "<table class='table table-striped table-hover'>";
                    $html .= "<thead style='background-color:#edbfbf;'>";
                        $html .= "<tr>";
                            $html .= "<th>#</th>";
                            $html .= "<th>Product</th>";
                            $html .= "<th>Quantity</th>";
                            $html .= "<th>Price</th>";
                            $html .= "<th>Action</th>";
                        $html .= "</tr>";
                    $html .= "</tr>";
                    $html .= "</thead>";
                    $html .= "<tbody>";
                    foreach ($cartItems as $key => $value) {
                        $html .= "<tr id='".$key."'>";
                            $html .= "<td>".$count++."</td>";
                            $html .= "<td>".$value->name."</td>";
                            $html .= "<td>".$value->qty."</td>";
                            $html .= "<td>&#8369 ".$value->price()."</td>";
                            $html .= "<td><button class='btn btn-primary' onclick='removeItem(\"".$key."\")'><i class='fa fa-trash fa-lg'></button></td>";
                        $html .= "</tr>";
                    }
                    $html .= "</tbody>";
                $html .= "</table>";
            $html .= "</div>";
            
          return $html;
    }
    

    The cart contents can be displayed in my page but when I tried to delete the item using the row id, the cart was empty.

    Script to remove:

    function removeItem(id) 
    {
        $.ajax({
            url: '/remove-item', 
            type: 'POST', 
            data: { id: id },
            headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
            success: function(res) {
                if (res == 1) {
                    alert('succes');
                }
                else {
                    alert("Oops, there's an error in removing the item. Kindly contact the website admin.");
                }
            }
        });
    }
    
    function removeItem(Request $request){
    
     //I printed here the Cart contents but it was empty. 
    Cart::remove($request->all()[id]);
      echo 1; 
      exit;
    }
    
    opened by gladis26 16
  • Remove from cart throws an exception

    Remove from cart throws an exception

    The remove() function returns null rather than boolean true when removing items. After passing it a non existing rowid, it throws an Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidRowIDException exception with empty message rather than return false.

    opened by junkystu 16
  • Cart::associate

    Cart::associate

    I'm having an issue with storing the cart items to the DB. I data dumped the cart and the object is set but when I check the DB the item is not inserted. Here is a snippet of my code:

        $item = array(
            'id' => '293ad',
            'name' => $this->input['condition'],
            'qty' => 1,
            'price' => 24
        );
        //Add item ID to cart
        Cart::associate('cart')->add($item);
    
        $content = Cart::content();
    
        dd($content);
    

    and here is my my DB migration

       <?php
    
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateCartTable extends Migration {
    
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cart', function($t) {
            $t->string('id');
            $t->string('qty');
            $t->string('name');
            $t->string('price');
        });
    }
    
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('cart');
    }
    
    }
    

    screen shot 2014-07-18 at 4 48 28 pm

    opened by amilajack 15
  • [QUESTION] is there any way to add a product that has two prices?

    [QUESTION] is there any way to add a product that has two prices?

    For example:

    CD Player price one: 20 price two: 15

    The intention is to be able to buy the two products, at this time only the price of the product is updated and indicates that I own 2 products with the same price.

    Thanks :smile:

    opened by abr4xas 13
  • [Laravel 5.2] Class 'Cart' not found

    [Laravel 5.2] Class 'Cart' not found

    Hello,

    Version: Laravel 5.2

    I'm getting Class 'Cart' not found, when trying to use it in Controller, that has use Cart in it's header

    In config/app.php I have both Provider and Facade

    Provider: Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class,

    Facade: 'Cart' => Gloudemans\Shoppingcart\Facades\Cart::class,

    In routes I use 'web' middleware correctly.

    In composer.json I have

    "require": { "php": ">=5.5.9", "laravel/framework": "5.2.", "laravelcollective/html": "5.2.", "filp/whoops": "^2.0", "gloudemans/shoppingcart": "~1.3" },

    Any ideas on how to solve this issue?

    Thank you

    opened by dasaco 13
  • How to guests continue with same cart when they login

    How to guests continue with same cart when they login

    i want to make a shopping cart where if guest user adds items to cart and when he logs in all the guest cart items should be shifted from session to database ? how can i achieve that ?

    opened by CODE-AXION 3
  • API support

    API support

    Does the package work through API endpoints? I tried with Postman and Sanctum. Seems like Add to cart works and returns rowId properly but once contents of the cart is requested, nothing is returned. isSaved param is false when items are added.

        `"8aba6628cc50563aa69002b76af27473": {
            "rowId": "8aba6628cc50563aa69002b76af27473",
            "id": "123ABC",
            "name": "Hello product",
            "qty": 2,
            "price": 9.99,
            "options": [],
            "tax": "0.00",
            "isSaved": false,
            "subtotal": "19.98"
        }`
    
    opened by rpmcmurphy 2
  • Tax Queries

    Tax Queries

    Dear Sir, I need some help please.

    1. Can the tax be dynamic since not all products are charged with the same tax amount.
    2. I would like to display the tax amount in the cart row, can it be done?

    Thank you

    opened by stezam 1
  • SQL Server Drivers for PHP

    SQL Server Drivers for PHP

    I need to link SQL server with my PHP I have downloaded the drivers and placed them in the C:\xampp\php\ext file already

    updated the php.ini file as well:

    extension_dir="C:\xampp\php\ext"

    ;SQL Server Ext extension=php_pdo_sqlsrv_74_ts_x64.dll extension=php_sqlsrv_74_ts_x64.dll

    but I get this error:

    PHP Warning: PHP Startup: Unable to load dynamic library 'php_pdo_sqlsrv_74_ts_x64.dll' (tried: C:\xampp\php\ext\php_pdo_sqlsrv_74_ts_x64.dll (The specified module could not be found.), C:\xampp\php\ext\php_php_pdo_sqlsrv_74_ts_x64.dll.dll (The specified module could not be found.)) in Unknown on line 0

    PHP Warning: PHP Startup: Unable to load dynamic library 'php_sqlsrv_74_ts_x64.dll' (tried: C:\xampp\php\ext\php_sqlsrv_74_ts_x64.dll (The specified module could not be found.), C:\xampp\php\ext\php_php_sqlsrv_74_ts_x64.dll.dll (The specified module could not be found.)) in Unknown on line 0

    I am using PHP V 7.4.27 and my PHP Extension build is TS My OS is 64 bit Please HELP, it's driving me CRAAAZZZYYYYY!!!

    opened by KishwerWali54 0
Releases(2.6.0)
Owner
Rob Gloudemans
Rob Gloudemans
Shopping Cart Implementation for Laravel Framework

Shopping Cart Implementation for Laravel Framework

darryl fernandez 1.2k Jan 4, 2023
Benefit PHP Shopping Cart Class (Simple Lightweight)

Benefit-PHP-Shopping-Cart-Class Benefit PHP Shopping Cart Class (Simple Lightweight) Table of Contents Initialization Get All Get Item Get Item Child

Osman Cakmak 8 Sep 6, 2022
AvoRed an Open Source Laravel Shopping Cart

AvoRed is commin up as a headless graphql version. AvoRed is a free open-source e-commerce platform written in PHP based on Laravel. Its an ingenuous

AvoRed Laravel E commerce 1.4k Dec 30, 2022
Laravel Shopping Cart Package

LaraCart - Laravel Shopping Cart Package (http://laracart.lukepolo.com) Features Coupons Session Based System Cross Device Support Multiple cart insta

Luke Policinski 516 Dec 10, 2022
AvoRed an Open Source Laravel Shopping Cart

AvoRed is commin up as a headless graphql version. AvoRed is a free open-source e-commerce platform written in PHP based on Laravel. Its an ingenuous

AvoRed Laravel E commerce 1.4k Dec 28, 2022
LiteCart - Free Shopping Cart Platform - Built with PHP, jQuery HTML 5 and CSS 3

LiteCart is a lightweight e-commerce platform for online merchants. Developed in PHP, HTML 5, and CSS 3. LiteCart is a registered trademark, property

LiteCart 153 Dec 28, 2022
A free shopping cart system. OpenCart is an open source PHP-based online e-commerce solution.

OpenCart is a free open source ecommerce platform for online merchants. OpenCart provides a professional and reliable foundation from which to build a successful online store.

OpenCart 6.6k Dec 31, 2022
PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

PrestaShop is an Open Source e-commerce web application, committed to providing the best shopping cart experience for both merchants and customers. It is written in PHP, is highly customizable, supports all the major payment services, is translated in many languages and localized for many countries, has a fully responsive design (both front and back office), etc. See all the available features.

PrestaShop 6.9k Dec 31, 2022
A robust session-based shopping bag for Laravel

Shopping Bag A robust session-based shopping bag for Laravel Go to documentation Documentation Documentation for Shopping Bag can be found in the docs

Laraware 30 Dec 13, 2021
Topshop offers its customers a modern shopping experience by bringing computers, appliances, clothing and many other items at their fingertips.

Topshop offers its customers a modern shopping experience by bringing computers, appliances, clothing and many other items at their fingertips. With just a few clicks, users can create an account, add products to their cart and place their order.

Abhijeet Pitumbur 2 Aug 8, 2022
The Online Shopping System in PHP using XAMPP as virtual Server.

Online shopping is a form of electronic commerce which allows consumers to directly buy goods or services from a seller over the Internet using a web browser or a mobile app.

Magesh Surya Ambikapathi 5 Sep 15, 2022
(Live Link) Extensive ecommerce site with vendors, mods & ability to add to cart without being logged in. Upgraded to Laravel 8.x

(Live Link) Extensive ecommerce site with vendors, mods & ability to add to cart without being logged in. Upgraded to Laravel 8.x

null 14 Dec 21, 2022
Magento 2 module to only allow checkout when the number of items in the cart are a multiple of X.

Cart Quantity Multiple - Magento 2 Module Introduction This module allows to limit checkout only when the contents of the cart are a multiple of X

ADVOCODO 3 Apr 7, 2022
Zen Cart® is a full-function e-commerce application for your website.

Zen Cart® - The Art of E-Commerce Zen Cart® was the first Open Source e-Commerce web application to be fully PA-DSS Certified. Zen Cart® v1.5.8 is an

Zen Cart 304 Jan 6, 2023
Magento 2 Share Cart extension Free

Mageplaza Share Cart Extension helps customers in sharing their shopping cart with friends and family as well. This is a supportive method to promote store’s conversion rate via the existing users, and this can significantly contribute to the revenue of the store.

Mageplaza 12 Jul 22, 2022
A simple shoppingcart implementation for Phalcon.

Phalcon Shopping Cart A simple shoppingcart implementation for Phalcon. Installation Install the package through Composer. Run the Composer require co

Sergey Mukhin 3 Oct 7, 2022
PHP implementation of Fowler's Money pattern.

Money PHP library to make working with money safer, easier, and fun! "If I had a dime for every time I've seen someone use FLOAT to store currency, I'

Money PHP 4.2k Jan 2, 2023
Example of Pre-Loader Implementation for Magento 2

Example of preloader Implements optimistic preloader for configurable product data without taking into account simple product status for: Price of con

EcomDev B.V. 10 Dec 12, 2021
Simple E-Comerce build use Laravel 5.6 require php version 5.6 or 7.4

Simple E-Comerce build use Laravel 5.6 require php version 5.6 or 7.4

Rangga Darmajati 3 Oct 7, 2021