{"id":15027719,"url":"https://github.com/lombervid/shoppingcart","last_synced_at":"2025-04-09T20:22:14.469Z","repository":{"id":163306221,"uuid":"41842135","full_name":"lombervid/shoppingcart","owner":"lombervid","description":"A class for a simple PHP Shopping Cart","archived":false,"fork":false,"pushed_at":"2023-06-14T22:55:46.000Z","size":111,"stargazers_count":9,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-23T22:14:02.303Z","etag":null,"topics":["composer","php81","shoppingcart","shoppingcart-library"],"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/lombervid.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-03T04:37:47.000Z","updated_at":"2023-10-07T09:21:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"a6566906-ad36-4a6c-a49d-20e52b4f4ad8","html_url":"https://github.com/lombervid/shoppingcart","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lombervid%2Fshoppingcart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lombervid%2Fshoppingcart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lombervid%2Fshoppingcart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lombervid%2Fshoppingcart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lombervid","download_url":"https://codeload.github.com/lombervid/shoppingcart/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247801155,"owners_count":20998338,"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":["composer","php81","shoppingcart","shoppingcart-library"],"created_at":"2024-09-24T20:06:56.657Z","updated_at":"2025-04-09T20:22:14.456Z","avatar_url":"https://github.com/lombervid.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/lombervid/shoppingcart?display_name=tag\u0026sort=semver)](https://github.com/lombervid/shoppingcart/releases/latest)\n[![Packagist](https://img.shields.io/packagist/v/lombervid/shoppingcart)](https://packagist.org/packages/lombervid/shoppingcart)\n![PHP Version](https://poser.pugx.org/lombervid/shoppingcart/require/php)\n[![tests](https://github.com/lombervid/shoppingcart/actions/workflows/tests.yml/badge.svg)](https://github.com/lombervid/shoppingcart/actions/workflows/tests.yml?query=branch%3Amain)\n[![GitHub](https://img.shields.io/github/license/lombervid/shoppingcart)](https://github.com/lombervid/shoppingcart/blob/main/LICENSE)\n\n\u003c/div\u003e\n\n# ShoppingCart PHP Class\n\n**_ShoppingCart_** is a simple _PHP_ package that provides you with a simple shopping cart implementation stored in `session`.\n\n## Installation\n\n### Composer\n\nYou can install it using [composer](https://getcomposer.org/):\n\n```bash\ncomposer require lombervid/shoppingcart\n```\n\n## Usage\n\nCreate an instance of `ShoppingCart` class.\n\n```php\nuse Lombervid\\ShoppingCart\\ShoppingCart;\n\n$shoppingCart = new ShoppingCart();\n```\n\n### Add items\n\nYou can add items calling the method `add()` passing an `Item` instance as parameter.\n\n```php\nuse Lombervid\\ShoppingCart\\Item;\nuse Lombervid\\ShoppingCart\\ShoppingCart;\n\n$cart = new ShoppingCart();\n$cart-\u003eadd(new Item('1', 'Cake', 15.56));\n$cart-\u003eadd(new Item('15', 'Frappe', 5));\n\nforeach ($cart-\u003eitems() as $item) {\n\t// $item-\u003eid\n\t// $item-\u003ename\n}\n```\n\nat this point your `$cart-\u003eitems()` will look like this:\n\n```php\narray:2 [▼\n  1 =\u003e Lombervid\\ShoppingCart\\Item {#5 ▼\n    -id: \"1\"\n    -name: \"Cake\"\n    -price: 15.56\n  }\n  15 =\u003e Lombervid\\ShoppingCart\\Item {#6 ▼\n    -id: \"15\"\n    -name: \"Frappe\"\n    -price: 5.0\n  }\n]\n```\n\n### Add extra fields to your item\n\nYou can also add extra fields (such as price, name, etc) to your item. The `Item` constructor receives a parameter `fields` which is an `Array` with the following structure:\n\n```php\n[\n    'field_name'   =\u003e 'field_value',\n    'field_2_name' =\u003e 'field_2_value'\n]\n```\n\nwhen you provide the `$fields` param, each field of the array is added to your item.\n\n```php\n$fields = [\n\t'size'  =\u003e 'XL',\n\t'color' =\u003e 'blue'\n];\n\n$item = new Item('23', 'My Shirt', 2.5, fields: $fields);\n$cart-\u003eadd($item);\n```\n\nwith the above code your `$cart-\u003eitems()` will look line:\n\n```php\narray:1 [▼\n  23 =\u003e Lombervid\\ShoppingCart\\Item {#5 ▼\n    -id: \"23\"\n    -name: \"My Shirt\"\n    -price: 2.5\n    -qty: 1\n    -fields: array:2 [▼\n      \"size\" =\u003e \"XL\"\n      \"color\" =\u003e \"blue\"\n    ]\n  }\n]\n```\n\nThen you can access any extra field as if they were properties:\n\n```php\nforeach ($cart-\u003eitems() as $item) {\n    // $item-\u003esize\n    // $item-\u003ecolor\n}\n```\n\n### Remove items\n\nYou can remove an item from the cart calling the method `remove($id)` which receive item's `$id` as parameter.\n\n```php\n$cart-\u003eremove(23);\n```\n\n### Clear the cart\n\nYou can clear the cart calling the method `clear()` which removes all the items from the cart.\n\n```php\n$shoppingCart-\u003eclear();\n```\n\n## Advanced options\n\n### ShoppingCart\n\n#### Cart options\n\nIt is an `array` of options. The default value is:\n\n```php\n[\n    'name'     =\u003e 'shopping_cart',\n    'autosave' =\u003e true,\n    'tax'      =\u003e 0,\n    'shipping' =\u003e [\n        'amount' =\u003e 0,\n        'free'   =\u003e 0,\n    ],\n]\n```\n\n| Option            | Type         | Default         | Description                                              |\n| :---------------- | ------------ | --------------- | -------------------------------------------------------- |\n| `name`            | `string`     | `shopping_cart` | Cart's name. Used to save the cart in storage            |\n| `autosave`        | `bool`       | `true`          | If set to `true`, cart is saved when object is destroyed |\n| `tax`             | `int\\|float` | `0`             | Porcentaje to be used as tax (0 - 100)                   |\n| `shipping.amount` | `int\\|float` | `0`             | Shipping cost                                            |\n| `shipping.free`   | `int\\|float` | `0`             | Value after which shipping will be free. `0` to disable  |\n\n#### Constructor\n\n| Parameter  | Type               | Default                           | Required | Description    |\n| :--------- | ------------------ | --------------------------------- | -------- | -------------- |\n| `$options` | `array`            | See [Cart options](#cart-options) | `false`  | Cart options   |\n| `$storage` | `StorageInterface` | `NativeSessionStorage`            | `false`  | Storage driver |\n\n#### Methods\n\n| Name                                         | Description                                   |\n| :------------------------------------------- | --------------------------------------------- |\n| `add(Item $item, bool $append = true): void` | Add an item to the cart                       |\n| `remove(string $id): bool`                   | Remove an item from the cart                  |\n| `subtotal(): float`                          | Get subtotal                                  |\n| `shipping(): float`                          | Get shipping cost                             |\n| `tax(): float`                               | Get tax                                       |\n| `total(): float`                             | Get total                                     |\n| `inCart(string $id): bool`                   | Check if an item is in the cart               |\n| `items(): array`                             | Return the items in the cart                  |\n| `clear(): static`                            | Remove all items from the cart                |\n| `isEmpty(): bool`                            | Check if the cart is empty                    |\n| `totalItems(): int`                          | Return the total (distinct) items in the cart |\n| `save(): void`                               | Save items in the storage                     |\n| `toArray(): array`                           | Return items as `array`                       |\n\n### Item\n\n#### Constructor\n\n| Parameter   | Type     | Default | Required | Description        |\n| :---------- | -------- | ------- | -------- | ------------------ |\n| `$id`       | `string` | `N/A`   | `true`   | Identifier         |\n| `$name`     | `string` | `N/A`   | `true`   | Name / description |\n| `$price`    | `float`  | `N/A`   | `true`   | Price (`\u003e= 0`)     |\n| `$qty`      | `int`    | `1`     | `false`  | Quantity (`\u003e 0`)   |\n| `$fields`   | `array`  | `[]`    | `false`  | Extra fields       |\n| `$discount` | `float`  | `0`     | `false`  | Discount (`\u003e= 0`)  |\n\n#### Methods\n\n| Name                                              | Description                        |\n| :------------------------------------------------ | ---------------------------------- |\n| `add(int $qty): void`                             | Increase item's quantity by `$qty` |\n| `update(int $qty): void`                          | Update item's quantity to `$qty`   |\n| `get(string $name, mixed $default = null): mixed` | Get `$name` property/field         |\n| `hasDiscount(): bool`                             | Check if has a discount            |\n| `price(): float`                                  | Get item's price                   |\n| `total(): float`                                  | Get total                          |\n| `toArray(): array`                                | Return item as `array`             |\n\n## Contributing\n\nRefer to [CONTRIBUTING](./.github/CONTRIBUTING.md) for information.\n\n## License\n\n[MIT](https://github.com/lombervid/shoppingcart/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flombervid%2Fshoppingcart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flombervid%2Fshoppingcart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flombervid%2Fshoppingcart/lists"}