{"id":15293914,"url":"https://github.com/ahmdadl/shoppingcart","last_synced_at":"2026-05-02T15:33:52.262Z","repository":{"id":56939502,"uuid":"286133948","full_name":"ahmdadl/shoppingcart","owner":"ahmdadl","description":"a simple yet powerful and highly customized laravel shopping cart","archived":false,"fork":false,"pushed_at":"2020-08-10T05:55:08.000Z","size":91,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-29T20:19:06.514Z","etag":null,"topics":["cart","ecommerce","laravel","laravel-framework","laravelecommercetool","laravelshoppingcart","multiinstance","shopping-cart"],"latest_commit_sha":null,"homepage":"http://spa.aboadeltestblog.epizy.com/","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/ahmdadl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-08T23:20:46.000Z","updated_at":"2020-08-10T06:26:57.000Z","dependencies_parsed_at":"2022-08-21T01:40:24.959Z","dependency_job_id":null,"html_url":"https://github.com/ahmdadl/shoppingcart","commit_stats":null,"previous_names":["ahmedadel35/shoppingcart","abo3adel/shoppingcart","ahmdadl/shoppingcart"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmdadl%2Fshoppingcart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmdadl%2Fshoppingcart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmdadl%2Fshoppingcart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmdadl%2Fshoppingcart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahmdadl","download_url":"https://codeload.github.com/ahmdadl/shoppingcart/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245294774,"owners_count":20591909,"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":["cart","ecommerce","laravel","laravel-framework","laravelecommercetool","laravelshoppingcart","multiinstance","shopping-cart"],"created_at":"2024-09-30T16:53:40.886Z","updated_at":"2026-05-02T15:33:47.219Z","avatar_url":"https://github.com/ahmdadl.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Shopping Cart\n\n---\n\na simple yet powerful and highly customized laravel shopping cart.\n\nHighly inspired by [LaravelShoppingcart](https://github.com/Crinsane/LaravelShoppingcart)\n\n[![Latest Stable Version](https://poser.pugx.org/abo3adel/shoppingcart/v)](//packagist.org/packages/abo3adel/shoppingcart) [![Total Downloads](https://poser.pugx.org/abo3adel/shoppingcart/downloads)](//packagist.org/packages/abo3adel/shoppingcart) [![Latest Unstable Version](https://poser.pugx.org/abo3adel/shoppingcart/v/unstable)](//packagist.org/packages/abo3adel/shoppingcart) [![License](https://poser.pugx.org/abo3adel/shoppingcart/license)](//packagist.org/packages/abo3adel/shoppingcart)\n\n#### Demo [shopping-cart](http://spa.aboadeltestblog.epizy.com/)\n\n## Features\n\n- Multiple cart instances\n- Multiple Buyable models\n- Discount \u0026\u0026 Tax\n- configure up to 2 colums in cart item\n- Guest cart items \u0026\u0026 user cart items\n- It`s more like the Laravel way\n\n## Requirements\n\n- PHP 7.1+ (tested in 7.2+)\n- Laravel 5.5\n\n### How It works\n\n**This package use sessions and database, it will save cart items into session for guests and then merge these items when user logIn and save it into database, while user is loggedIn items will be saved into database**\n\n## Installation\n\n1. require\n\n```php\ncomposer require abo3adel/shoppingcart\n```\n\n2. Import config file and migration and migrate\n\n```php\nphp artisan vendor:publish --tag=shoppingcart-all\n```\n\n3. configure before migrate\n\n```php\nupdate config/shoppingcart.php with your configration\nand then\nphp artisan migrate\n```\n\n4. Add a trait and interface (required) to the model that can be bought\n\n```php\n\u003c?php\n\nnamespace Abo3adel\\ShoppingCart\\Tests\\Model;\n\nuse Abo3adel\\ShoppingCart\\Contracts\\CanBeBought;\nuse Abo3adel\\ShoppingCart\\Traits\\Buyable;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass SpaceCraft extends Model implements CanBeBought\n{\n    use Buyable;\n\n    public function getPrice(): float\n    {\n        return $this-\u003eprice;\n    }\n\n    public function getDiscount(): float\n    {\n        return $this-\u003ediscount;\n    }\n\n    //\n}\n```\n\n## Overview\n\n- [Usage](#usage)\n- [Instances](#instances)\n- [Helpers](#helpers)\n- [Models](#models)\n- [Bonus](#bonus)\n- [Exceptions](#exceptions)\n- [Events](#events)\n- [Commands](#commands)\n- [Example](#example)\n- [Tests](#tests)\n- [Contribution](#contribution)\n- [License](#license)\n\n## Usage\n\nyou can use the facade or the helper function\n\n```php\nCart::add()\n\ncart()-\u003eadd()\n```\n\n### Cart::add()\n\n\u003e return the newly saved item\n\n```php\n// all options\nCart::add(\n    CanBeBought $buyable,\n    int $qty,\n    mixed $opt1, // see config/shoppingcart.php to change this\n    mixed $opt2, // see config/shoppingcart.php to change this\n    array $options\n)\n\n// only price and qty\nCart::add($buyable, $qty)\n\n// only price \u0026\u0026 qty \u0026\u0026 options array\n// use this if you do not use any of (opt1 or opt2)\nCart::add($buyable, $qty, ['weight' =\u003e 250])\n\n// if you do not use opt2\nCart::add($buyable, $qty, $opt1, ['weight' =\u003e 250])\n```\n\n### Cart::find()\n\n\u003e return the cart item if found || else null\n\n```php\n// by item ID\nCart::find($itemId)\n\n// by buyable model id\nCart::find(int $buyable_id, string $buyable_type)\nCart::find(25, App\\Book::class)\n```\n\n### Cart::update()\n\n\u003e return boolean\n\n```php\n// all args\nCart::add(\n    int $itemId,\n    int $qty,\n    mixed $opt1, // see config/shoppingcart.php to change this\n    mixed $opt2, // see config/shoppingcart.php to change this\n    array $options\n)\n\n// only qty\nCart::update($itemId, $qty)\n\n// only options\nCart::update($itemId, $options)\n\n// if no (opt1 || opt2)\nCart::update($itemId, $qty, $options)\n\n// if no opt2\nCart::update($itemId, $qty, $opt1, $options)\n```\n\n### Cart::delete()\n\n\u003e return boolean\n\n```php\nCart::delete(int $itemId)\n```\n\n### Cart::has()\n\n\u003e return boolean\n\n```php\n// by item id\nCart::has($itemId)\n\n// by buyable id\nCart::has($buyable_id, $buyable_type)\nCart::has(5, 'App\\Product')\n```\n\n### Cart::content()\n\n\u003e return collection of cart items for current instance.\n\u003e all collection methods allowed see [Collections](https://laravel.com/docs/7.x/collections)\n\n```php\nCart::content()\n\nCart::content()-\u003ecount() // get count\n\nCart::content()-\u003esearch() // search\n\nCart::content()-\u003eeach() // loop\n\n// or any collection method\n```\n\n### Cart::destroy()\n\ndelete all items for current instance\n\n\u003e count of deleted items\n\n```php\nCart::destroy()\n```\n\n## Instances\n\n**Cart stays in the last set instance if you don't set a different one**\n\n```php\nCart::instance()-\u003econtent() // default instance\n\nCart::instance('wishlist')-\u003econtent()\nCart::getInstance() // wishlist\nCart::destroy() // instance still wishlist\n\nCart::instance('compare')-\u003eadd($buyable, 2)\nCart::getInstance() // compare\n\nCart::instance()-\u003edelete(5)\nCart::getInstance() // default\n```\n\n## Helpers\n\n### Cart::total()\n\n\u003e get the sum of all items sub_total (qty \\* price)\n\n```php\nCart::total() // 5631.25\n\n// if you want the total to be formated\nCart::total(true)\nCart::total($formated, $decimals, $dec_point, $thousands_sep)\n```\n\n### Cart::totalPrice()\n\n\u003e the sum of all items price\n\n### Cart::totalQty()\n\n\u003e the sum of all items qty\n\n### Cart::subTotal()\n\n\u003e get the total minus tax percentage\n\n```php\nCart::subTotal() // 2516.32\n\nCart::subTotal(true) // 2,516.32\nCart::subTotal($formated, $decimals, $dec_point, $thousands_sep)\n```\n\n### Cart::setTax()\n\n\u003e set tax percentage for current instance only\n\u003e does not affect the configured value\n\n```php\nCart::setTax(25)-\u003esubTotal()\n```\n\n### Cart::getTax()\n\n\u003e retrieve current tax percentage\n\n### Cart::increments()\n\n\u003e increase item qty\n\n```php\nCart::increments($itemId, $numberToAdd)\n```\n\n### Cart::decrements()\n\n\u003e decrease item qty\n\n```php\nCart::decrements($itemId, $numberToAdd)\n```\n\n## Models\n\n### CartItem\n\n```php\n$cartItem = Cart::add($buyable, 5)\n\n// get subtotal (price * qty)\n$cartItem-\u003esub_total\n\n// get formated subtotal\n$cartItem-\u003esub_total()\n$cartItem-\u003esub_total($decimals, $dec_point, $thousands_sep)\n\n// increment item qty\n$cartItem-\u003eincrements($numberToAdd)\n\n// decrement item qty\n$cartItem-\u003edecrements($numberToSubstract)\n\n// access the buyable object\n$cartItem-\u003ebuyable\n```\n\n### Buyable\n\n```php\n// add to cart in the default instance\n$buyable-\u003eaddToCart($qty, $opt1, $opt2, $options)\n\n// remove from cart in the default instance\n$buyable-\u003eremoveFromCart()\n\n// get list of all cart items associated with this model\n$buyable-\u003eitems()\n\n// get the subTotal price after discount substract\n$buyable-\u003egetSubPrice()\n```\n\n## Bonus\n\n### Manage another user cart items\n\n\u003e Cart stays in the last set User if you don't set a different one\n\n```php\n// $admin is logged in\n// user here is admin\n\nCart::forUser($user)\n// user here is $user\nCart::add($buyable, $qty)\n\n// reset user and return to logged in admin\nCart::resetUser()\n\n// user here is $admin again\n```\n\n### Cart::checkBuyableStockAmount()\n\n\u003e this will delete cart items which buyable is out of stock\n\u003e and lower items qty if it exceeded buyable qty\n\n```php\n$buyable1-\u003eqty = 0 // out of stock (will be removed)\n\n$buyable2-\u003eqty = 5\n$cartItem-\u003eqty = 7 // this exceeded it`s buyable qty\n\n[\n    [$buyable1], // deleted items\n    [\n        'from' =\u003e 7,\n        'to' =\u003e 2,\n        'items' =\u003e [$buyable2] // updated items qty\n    ]\n] = Cart::checkBuyableStockAmount()\n```\n\n### Refresh Items Buyable Object\n\n\u003e only required in session\n\u003e this will update the buyable object with latest changes\n\u003e you can use this to check if item buyable still in stock\n\n```php\n// this initial values\n$buyable-\u003eqty // 4\n$item-\u003ebuyable-\u003eqty // 4\n\n// this after updating buyable qty\n$buyable-\u003eqty // 10\n$item-\u003ebuyable-\u003eqty // 4 // still not updated\n\nCart::refreshItemsBuyableObjects()\n\n$item-\u003ebuyable-\u003eqty // 10 // updated\n```\n\n## Exceptions\n\n| Exception                  | Reason                                                                  |\n| -------------------------- | ----------------------------------------------------------------------- |\n| InvalidModelException      | Provided buyable model is missing required attributes (getPrice() - id) |\n| ItemAlreadyExistsException | trying to add new item that was already added                           |\n| ItemNotFoundException      | trying to update or delete an item that is not found                    |\n\n## Events\n\n| Event                 | Fired                                       | Parameter    |\n| --------------------- | ------------------------------------------- | ------------ |\n| CartItemAdded         | When an item was added to the cart.         | the CartItem |\n| CartItemUpdated       | When an item was updated                    | the CartItem |\n| CartItemRemoved       | when an item was removed                    | the CartItem |\n| CartInstanceDestroyed | when all items for this instance is deleted | instance     |\n\n## Commands\n\n\u003e this will delete old cart item from database older than configured `deleteAfter` value\n\nSchedule the RemoveOldItemsCommand\n\n```php\nprotected function schedule(Schedule $schedule)\n{\n    $schedule-\u003ecommand('shoppingcart:destroy')-\u003edaily();\n}\n```\n\n## Example\n\n---\n\n```php\n$buyable = Product::find(1);\n\n$item = $buyable-\u003eaddToCart(10, 6, 32, ['height' =\u003e '340m']);\n// or $item = Cart::add($buyable, 10, 6, 32, ['height' =\u003e '340m'])\n\n$wishListItem = Cart::instance('wishlist')-\u003eadd($buyable, 0);\n$anotherWishListItem = Cart::instance('wishlist')-\u003eadd($anotherBuyable, 0);\n\necho $item-\u003esize; // 6 , opt1 =\u003e size\necho $item-\u003ecolor; // 32 , opt2 =\u003e color\n\n$item-\u003eincrements(3);\necho $item-\u003eqty; // 13\n\n// return to default instance and update item\nCart::instance()-\u003eupdate($item-\u003eid, ['height' =\u003e '260m']);\n\n// find updated item\n$item = Cart::find($item-\u003eid);\necho $item-\u003eoptions; // ['height' =\u003e '260m']\n\nforeach (Cart::content() as $item) {\n    echo $item-\u003esub_total; // 2653.14    not formated\n    echo $item_sub_total(); // 2,653.14  formated\n}\n\n// get all items subTotal\necho Cart::subTotal(); // 6532145.2\necho Cart::subTotal(true); // 6,532,145.20\n\necho Cart::instance('wishlist')-\u003econtent()-\u003ecount() // 2\n```\n\n## Tests\n\n```php\ncomposer test\n```\n\n## Contribution\n\nContributions are **welcome** and will be fully **credited**.\nsee [CONTRIBUTING.md](./CONTRIBUTING.md)\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmdadl%2Fshoppingcart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahmdadl%2Fshoppingcart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmdadl%2Fshoppingcart/lists"}