{"id":19684922,"url":"https://github.com/boolfalse/laravel-shopping-cart","last_synced_at":"2025-04-29T06:30:41.542Z","repository":{"id":62495934,"uuid":"162440259","full_name":"boolfalse/laravel-shopping-cart","owner":"boolfalse","description":"Laravel 5.7 Shopping Cart","archived":false,"fork":false,"pushed_at":"2018-12-21T15:24:06.000Z","size":33,"stargazers_count":4,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-13T15:51:55.553Z","etag":null,"topics":["laravel","shopping-cart"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/boolfalse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-19T13:19:44.000Z","updated_at":"2020-07-29T14:11:53.000Z","dependencies_parsed_at":"2022-11-02T09:32:16.999Z","dependency_job_id":null,"html_url":"https://github.com/boolfalse/laravel-shopping-cart","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boolfalse%2Flaravel-shopping-cart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boolfalse%2Flaravel-shopping-cart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boolfalse%2Flaravel-shopping-cart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boolfalse%2Flaravel-shopping-cart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boolfalse","download_url":"https://codeload.github.com/boolfalse/laravel-shopping-cart/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224151682,"owners_count":17264556,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","shopping-cart"],"created_at":"2024-11-11T18:19:20.697Z","updated_at":"2024-11-11T18:19:21.417Z","avatar_url":"https://github.com/boolfalse.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n## Laravel 5.7 Shopping Cart Simple Package\n\n### About:\n\nSimple shopping-cart implementation package for Laravel 5.7.\nWith this package you can integrate [Crinsane/LaravelShoppingcart](https://github.com/Crinsane/LaravelShoppingcart). Before uploading this package to the github, I actually just can't find a stable package to use that for Laravel 5.7 version.\nSo I've collected this one (see used resources below). I will glad to see any advices and suggestions.\n\n\n#### Implementation Example:\n[boolfalse/laravel-shopping-cart-demo](https://github.com/boolfalse/laravel-shopping-cart-demo)\n\n\n### Package Installation:\n\n```shell\ncomposer require boolfalse/laravel-shopping-cart\n```\n\nFor Laravel 5.7 you don't need to add service provider \u0026 alias:\n\u003cbr /\u003e\nAdd service provider to 'providers' array in config/app.php \u003cbr /\u003eBoolfalse\\LaravelShoppingCart\\LSCServiceProvider::class,\n\u003cbr /\u003e\nAdd alias to 'aliases' array in config/app.php \u003cbr /\u003e'Cart' =\u003e Boolfalse\\LaravelShoppingCart\\Facades\\Cart::class,\n\n```shell\nphp artisan vendor:publish --provider=\"Boolfalse\\LaravelShoppingCart\\LSCServiceProvider\" --tag=\"config\"\nphp artisan vendor:publish --provider=\"Boolfalse\\LaravelShoppingCart\\LSCServiceProvider\" --tag=\"migrations\"\n```\n\nNow you can do migrating, to create 'shoppingcart' table:\n```shell\nphp artisan migrate\n```\n\nYou can do custom changes in 'config/cart.php' file.\n\u003cbr /\u003e\u003cb\u003eNote: \u003c/b\u003e Don't forget to be sure that in config/app.php 'product_model' have right model namespace.\n\n\n## DOCUMENTATION\n\n\n## Overview\nLook at one of the following topics to learn more about LaravelShoppingcart\n\n* [Usage](#usage)\n* [Collections](#collections)\n* [Instances](#instances)\n* [Models](#models)\n* [Database](#database)\n* [Exceptions](#exceptions)\n* [Events](#events)\n* [Example](#example)\n\n## Usage\n\nThe shoppingcart gives you the following methods to use:\n\n### Cart::add()\n\nAdding an item to the cart is really simple, you just use the `add()` method, which accepts a variety of parameters.\n\nIn its most basic form you can specify the id, name, quantity, price of the product you'd like to add to the cart.\n\n```php\nCart::add('293ad', 'Product 1', 1, 9.99);\n```\n\nAs 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.\n\n```php\nCart::add('293ad', 'Product 1', 1, 9.99, ['size' =\u003e 'large']);\n```\n\n**The `add()` method will return an CartItem instance of the item you just added to the cart.**\n\nMaybe 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.\n\n```php\nCart::add(['id' =\u003e '293ad', 'name' =\u003e 'Product 1', 'qty' =\u003e 1, 'price' =\u003e 9.99, 'options' =\u003e ['size' =\u003e 'large']]);\n```\n\nNew 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. \nThis way you can just pass the `add()` method a model and the quantity and it will automatically add it to the cart. \n\n**As an added bonus it will automatically associate the model with the CartItem**\n\n```php\nCart::add($product, 1, ['size' =\u003e 'large']);\n```\nAs an optional third parameter you can add options.\n```php\nCart::add($product, 1, ['size' =\u003e 'large']);\n```\n\nFinally, you can also add multipe items to the cart at once.\nYou can just pass the `add()` method an array of arrays, or an array of Buyables and they will be added to the cart. \n\n**When adding multiple items to the cart, the `add()` method will return an array of CartItems.**\n\n```php\nCart::add([\n  ['id' =\u003e '293ad', 'name' =\u003e 'Product 1', 'qty' =\u003e 1, 'price' =\u003e 10.00],\n  ['id' =\u003e '4832k', 'name' =\u003e 'Product 2', 'qty' =\u003e 1, 'price' =\u003e 10.00, 'options' =\u003e ['size' =\u003e 'large']]\n]);\n\nCart::add([$product1, $product2]);\n\n```\n\n### Cart::update()\n\nTo update an item in the cart, you'll first need the rowId of the item.\nNext you can use the `update()` method to update it.\n\nIf you simply want to update the quantity, you'll pass the update method the rowId and the new quantity:\n\n```php\n$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';\n\nCart::update($rowId, 2); // Will update the quantity\n```\n\nIf 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.\n\n```php\nCart::update($rowId, ['name' =\u003e 'Product 1']); // Will update the name\n\nCart::update($rowId, $product); // Will update the id, name and price\n\n```\n\n### Cart::remove()\n\nTo 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.\n\n```php\n$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';\n\nCart::remove($rowId);\n```\n\n### Cart::get()\n\nIf 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.\n\n```php\n$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';\n\nCart::get($rowId);\n```\n\n### Cart::content()\n\nOf 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.\n\n```php\nCart::content();\n```\n\nThis method will return the content of the current cart instance, if you want the content of another instance, simply chain the calls.\n\n```php\nCart::instance('wishlist')-\u003econtent();\n```\n\n### Cart::destroy()\n\nIf 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.\n\n```php\nCart::destroy();\n```\n\n### Cart::total()\n\nThe `total()` method can be used to get the calculated total of all items in the cart, given there price and quantity.\n\n```php\nCart::total();\n```\n\nThe method will automatically format the result, which you can tweak using the three optional parameters\n\n```php\nCart::total($decimals, $decimalSeperator, $thousandSeperator);\n```\n\nYou can set the default number format in the config file.\n\n**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-\u003etotal`**\n\n### Cart::tax()\n\nThe `tax()` method can be used to get the calculated amount of tax for all items in the cart, given there price and quantity.\n\n```php\nCart::tax();\n```\n\nThe method will automatically format the result, which you can tweak using the three optional parameters\n\n```php\nCart::tax($decimals, $decimalSeperator, $thousandSeperator);\n```\n\nYou can set the default number format in the config file.\n\n**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-\u003etax`**\n\n### Cart::subtotal()\n\nThe `subtotal()` method can be used to get the total of all items in the cart, minus the total amount of tax. \n\n```php\nCart::subtotal();\n```\n\nThe method will automatically format the result, which you can tweak using the three optional parameters\n\n```php\nCart::subtotal($decimals, $decimalSeperator, $thousandSeperator);\n```\n\nYou can set the default number format in the config file.\n\n**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-\u003esubtotal`**\n\n### Cart::count()\n\nIf 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.\n\n```php\nCart::count();\n```\n\n### Cart::search()\n\nTo find an item in the cart, you can use the `search()` method.\n\n**This method was changed on version 2**\n\nBehind 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.\n\nIf you for instance want to find all items with an id of 1:\n\n```php\n$cart-\u003esearch(function ($cartItem, $rowId) {\n\treturn $cartItem-\u003eid === 1;\n});\n```\n\nAs 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.\n\n**The method will return a Collection containing all CartItems that where found**\n\nThis way of searching gives you total control over the search process and gives you the ability to create very precise and specific searches.\n\n## Collections\n\nOn 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.\n\nAs an example, you can quicky get the number of unique products in a cart:\n\n```php\nCart::content()-\u003ecount();\n```\n\nOr you can group the content by the id of the products:\n\n```php\nCart::content()-\u003egroupBy('id');\n```\n\n## Instances\n\nThe packages supports multiple instances of the cart. The way this works is like this:\n\nYou 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.\nIf you want to switch instances, you just call `Cart::instance('otherInstance')` again, and you're working with the `otherInstance` again.\n\nSo a little example:\n\n```php\nCart::instance('shopping')-\u003eadd('192ao12', 'Product 1', 1, 9.99);\n\n// Get the content of the 'shopping' cart\nCart::content();\n\nCart::instance('wishlist')-\u003eadd('sdjk922', 'Product 2', 1, 19.95, ['size' =\u003e 'medium']);\n\n// Get the content of the 'wishlist' cart\nCart::content();\n\n// If you want to get the content of the 'shopping' cart again\nCart::instance('shopping')-\u003econtent();\n\n// And the count of the 'wishlist' cart again\nCart::instance('wishlist')-\u003ecount();\n```\n\n**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\n**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')-\u003econtent()`.**\n\n## Models\n\nBecause 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. \n\nThat way you can access your model right from the `CartItem`!\n\nThe model can be accessed via the `model` property on the CartItem.\n\n**If your model implements the `Buyable` interface and you used your model to add the item to the cart, it will associate automatically.**\n\nHere is an example:\n\n```php\n\n// First we'll add the item to the cart.\n$cartItem = Cart::add('293ad', 'Product 1', 1, 9.99, ['size' =\u003e 'large']);\n\n// Next we associate a model with the item.\nCart::associate($cartItem-\u003erowId, 'Product');\n\n// Or even easier, call the associate method on the CartItem!\n$cartItem-\u003eassociate('Product');\n\n// You can even make it a one-liner\nCart::add('293ad', 'Product 1', 1, 9.99, ['size' =\u003e 'large'])-\u003eassociate('Product');\n\n// Now, when iterating over the content of the cart, you can access the model.\nforeach(Cart::content() as $row) {\n\techo 'You have ' . $row-\u003eqty . ' items of ' . $row-\u003emodel-\u003ename . ' with description: \"' . $row-\u003emodel-\u003edescription . '\" in your cart.';\n}\n```\n## Database\n\n- [Storing the cart](#save-cart-to-database)\n- [Restoring the cart](#retrieve-cart-from-database)\n\n### Storing the cart    \nTo 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.\n\n    Cart::store('username');\n    \n    // To store a cart instance named 'wishlist'\n    Cart::instance('wishlist')-\u003estore('username');\n\n### Restoring the cart\nIf 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.\n \n    Cart::restore('username');\n    \n    // To restore a cart instance named 'wishlist'\n    Cart::instance('wishlist')-\u003erestore('username');\n\n## Exceptions\n\nThe 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:\n\n| Exception                    | Reason                                                                             |\n| ---------------------------- | ---------------------------------------------------------------------------------- |\n| *CartAlreadyStoredException* | When trying to store a cart that was already stored using the specified identifier |\n| *InvalidRowIDException*      | When the rowId that got passed doesn't exists in the current cart instance         |\n| *UnknownModelException*      | When you try to associate an none existing model to a CartItem.                    |\n\n## Events\n\nThe cart also has events build in. There are five events available for you to listen for.\n\n| Event         | Fired                                    | Parameter                        |\n| ------------- | ---------------------------------------- | -------------------------------- |\n| cart.added    | When an item was added to the cart.      | The `CartItem` that was added.   |\n| cart.updated  | When an item in the cart was updated.    | The `CartItem` that was updated. |\n| cart.removed  | When an item is removed from the cart.   | The `CartItem` that was removed. |\n| cart.stored   | When the content of a cart was stored.   | -                                |\n| cart.restored | When the content of a cart was restored. | -                                |\n\n## Example\n\nBelow is a little example of how to list the cart content in a table:\n\n```php\n\n// Add some items in your Controller.\nCart::add('192ao12', 'Product 1', 1, 9.99);\nCart::add('1239ad0', 'Product 2', 2, 5.95, ['size' =\u003e 'large']);\n\n// Display the content in a View.\n\u003ctable\u003e\n   \t\u003cthead\u003e\n       \t\u003ctr\u003e\n           \t\u003cth\u003eProduct\u003c/th\u003e\n           \t\u003cth\u003eQty\u003c/th\u003e\n           \t\u003cth\u003ePrice\u003c/th\u003e\n           \t\u003cth\u003eSubtotal\u003c/th\u003e\n       \t\u003c/tr\u003e\n   \t\u003c/thead\u003e\n\n   \t\u003ctbody\u003e\n\n   \t\t\u003c?php foreach(Cart::content() as $row) :?\u003e\n\n       \t\t\u003ctr\u003e\n           \t\t\u003ctd\u003e\n               \t\t\u003cp\u003e\u003cstrong\u003e\u003c?php echo $row-\u003ename; ?\u003e\u003c/strong\u003e\u003c/p\u003e\n               \t\t\u003cp\u003e\u003c?php echo ($row-\u003eoptions-\u003ehas('size') ? $row-\u003eoptions-\u003esize : ''); ?\u003e\u003c/p\u003e\n           \t\t\u003c/td\u003e\n           \t\t\u003ctd\u003e\u003cinput type=\"text\" value=\"\u003c?php echo $row-\u003eqty; ?\u003e\"\u003e\u003c/td\u003e\n           \t\t\u003ctd\u003e$\u003c?php echo $row-\u003eprice; ?\u003e\u003c/td\u003e\n           \t\t\u003ctd\u003e$\u003c?php echo $row-\u003etotal; ?\u003e\u003c/td\u003e\n       \t\t\u003c/tr\u003e\n\n\t   \t\u003c?php endforeach;?\u003e\n\n   \t\u003c/tbody\u003e\n   \t\n   \t\u003ctfoot\u003e\n   \t\t\u003ctr\u003e\n   \t\t\t\u003ctd colspan=\"2\"\u003e\u0026nbsp;\u003c/td\u003e\n   \t\t\t\u003ctd\u003eSubtotal\u003c/td\u003e\n   \t\t\t\u003ctd\u003e\u003c?php echo Cart::subtotal(); ?\u003e\u003c/td\u003e\n   \t\t\u003c/tr\u003e\n   \t\t\u003ctr\u003e\n   \t\t\t\u003ctd colspan=\"2\"\u003e\u0026nbsp;\u003c/td\u003e\n   \t\t\t\u003ctd\u003eTax\u003c/td\u003e\n   \t\t\t\u003ctd\u003e\u003c?php echo Cart::tax(); ?\u003e\u003c/td\u003e\n   \t\t\u003c/tr\u003e\n   \t\t\u003ctr\u003e\n   \t\t\t\u003ctd colspan=\"2\"\u003e\u0026nbsp;\u003c/td\u003e\n   \t\t\t\u003ctd\u003eTotal\u003c/td\u003e\n   \t\t\t\u003ctd\u003e\u003c?php echo Cart::total(); ?\u003e\u003c/td\u003e\n   \t\t\u003c/tr\u003e\n   \t\u003c/tfoot\u003e\n\u003c/table\u003e\n```\n\n\n\n\n### Used Resources:\n\n* [Main Package](https://github.com/Crinsane/LaravelShoppingcart)\n* [Article](https://andremadarang.com/implementing-a-shopping-cart-in-laravel/)\n* [Online Demo](https://laravelecommerceexample.ca/cart)\n* [Fork and implementation example](https://github.com/drehimself/laravel-shopping-cart-example)\n* [Upgrading commit](https://github.com/drehimself/laravel-shopping-cart-example/commit/9bcc80a5a7a54e7823f49e386078680b9fb0ca76)\n\n\n### Developer E-mail:\n    boolfalse@mail.ru\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboolfalse%2Flaravel-shopping-cart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboolfalse%2Flaravel-shopping-cart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboolfalse%2Flaravel-shopping-cart/lists"}