{"id":15042492,"url":"https://github.com/devanych/yii2-cart","last_synced_at":"2025-04-10T00:32:33.575Z","repository":{"id":56966288,"uuid":"136248824","full_name":"devanych/yii2-cart","owner":"devanych","description":"Shopping cart for Yii2","archived":false,"fork":false,"pushed_at":"2019-10-18T10:33:26.000Z","size":27,"stargazers_count":20,"open_issues_count":0,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T02:13:36.537Z","etag":null,"topics":["yii2","yii2-cart","yii2-extension","yii2-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":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devanych.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-06-06T00:11:33.000Z","updated_at":"2023-12-13T06:23:58.000Z","dependencies_parsed_at":"2022-08-21T06:10:23.458Z","dependency_job_id":null,"html_url":"https://github.com/devanych/yii2-cart","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fyii2-cart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fyii2-cart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fyii2-cart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devanych%2Fyii2-cart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devanych","download_url":"https://codeload.github.com/devanych/yii2-cart/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248135313,"owners_count":21053655,"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":["yii2","yii2-cart","yii2-extension","yii2-shopping-cart"],"created_at":"2024-09-24T20:47:23.518Z","updated_at":"2025-04-10T00:32:33.546Z","avatar_url":"https://github.com/devanych.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Yii2 shopping cart \n\nThis extension adds shopping cart for Yii framework 2.0\n\nGuide with a detailed description in Russian language [here](https://github.com/devanych/yii2-cart/blob/master/docs/guide-ru.md).\n\n## Installation\n\nThe preferred way to install this extension is through [Composer](https://getcomposer.org/download/)\n\nEither run\n\n```\nphp composer.phar require devanych/yii2-cart \"*\"\n```\n\nor add\n\n```\ndevanych/yii2-cart: \"*\"\n```\n\nto the `require` section of your `composer.json` file.\n\n## Configuration\n\nConfigure the `cart` component (default values are shown):\n\n```php\nreturn [\n    //...\n    'components' =\u003e [\n        //...\n        'cart' =\u003e [\n            'class' =\u003e 'devanych\\cart\\Cart',\n            'storageClass' =\u003e 'devanych\\cart\\storage\\SessionStorage',\n            'calculatorClass' =\u003e 'devanych\\cart\\calculators\\SimpleCalculator',\n            'params' =\u003e [\n                'key' =\u003e 'cart',\n                'expire' =\u003e 604800,\n                'productClass' =\u003e 'app\\model\\Product',\n                'productFieldId' =\u003e 'id',\n                'productFieldPrice' =\u003e 'price',\n            ],\n        ],\n    ]\n    //...\n];\n```\n\nIn addition to `devanych\\cart\\storage\\SessionStorage`, there is also `devanych\\cart\\storage\\CookieStorage` and `devanych\\cart\\storage\\DbSessionStorage`. It is possible to create your own storage, you need to implement the interface `devanych\\cart\\storage\\StorageInterface`.\n\n`DbSessionStorage` uses `SessionStorage` for unauthorized users and database for authorized.\n\n\u003e If you use the `devanych\\cart\\storage\\DbSessionStorage` as `storageClass` then you need to apply the following migration:\n\n```php\nphp yii migrate --migrationPath=@vendor/devanych/yii2-cart/migrations\n```\n\n`devanych\\cart\\calculators\\SimpleCalculator` produces the usual calculation of the total cost and total quantity of items in the cart. If you need to make a calculation with discounts or something else, you can create your own calculator by implementing the interface `devanych\\cart\\calculators\\CalculatorInterface`.\n\nSetting up the `params` array: \n\n* `key` - For Session and Cookie.\n\n* `expire` - Cookie life time.\n\n* `productClass` - Product class is an ActiveRecord model.\n\n* `productFieldId` - Name of the product model `id` field.\n\n* `productFieldPrice` - Name of the product model `price` field.\n\n#### Supporting multiple shopping carts to same website:\n\n```php\n//...\n'cart' =\u003e [\n    'class' =\u003e 'devanych\\cart\\Cart',\n    'storageClass' =\u003e 'devanych\\cart\\storage\\SessionStorage',\n    'calculatorClass' =\u003e 'devanych\\cart\\calculators\\SimpleCalculator',\n    'params' =\u003e [\n        'key' =\u003e 'cart',\n        'expire' =\u003e 604800,\n        'productClass' =\u003e 'app\\model\\Product',\n        'productFieldId' =\u003e 'id',\n        'productFieldPrice' =\u003e 'price',\n    ],\n],\n'favorite' =\u003e [\n    'class' =\u003e 'devanych\\cart\\Cart',\n    'storageClass' =\u003e 'devanych\\cart\\storage\\DbSessionStorage',\n    'calculatorClass' =\u003e 'devanych\\cart\\calculators\\SimpleCalculator',\n    'params' =\u003e [\n        'key' =\u003e 'favorite',\n        'expire' =\u003e 604800,\n        'productClass' =\u003e 'app\\models\\Product',\n        'productFieldId' =\u003e 'id',\n        'productFieldPrice' =\u003e 'price',\n    ],\n],\n//...\n```\n\n## Usage\n\nYou can get the shopping cart component anywhere in the app using `Yii::$app-\u003ecart`.\n\nUsing cart:\n\n```php\n// Product is an AR model\n$product = Product::findOne(1);\n\n// Get component of the cart\n$cart = \\Yii::$app-\u003ecart;\n\n// Add an item to the cart\n$cart-\u003eadd($product, $quantity);\n\n// Adding item quantity in the cart\n$cart-\u003eplus($product-\u003eid, $quantity);\n\n// Change item quantity in the cart\n$cart-\u003echange($product-\u003eid, $quantity);\n\n// Removes an items from the cart\n$cart-\u003eremove($product-\u003eid);\n\n// Removes all items from the cart\n$cart-\u003eclear();\n\n// Get all items from the cart\n$cart-\u003egetItems();\n\n// Get an item from the cart\n$cart-\u003egetItem($product-\u003eid);\n\n// Get ids array all items from the cart\n$cart-\u003egetItemIds();\n\n// Get total cost all items from the cart\n$cart-\u003egetTotalCost();\n\n// Get total count all items from the cart\n$cart-\u003egetTotalCount();\n```\n\n#### Using cart items:\n\n```php\n// Product is an AR model\n$product = Product::findOne(1);\n\n// Get component of the cart\n$cart = \\Yii::$app-\u003ecart;\n\n// Get an item from the cart\n$item = $cart-\u003egetItem($product-\u003eid);\n\n// Get the id of the item\n$item-\u003egetId();\n\n// Get the price of the item\n$item-\u003egetPrice();\n\n// Get the product, AR model\n$item-\u003egetProduct();\n\n// Get the cost of the item\n$item-\u003egetCost();\n\n// Get the quantity of the item\n$item-\u003egetQuantity();\n\n// Set the quantity of the item\n$item-\u003esetQuantity($quantity);\n```\n\n\u003e By using method `getProduct()`, you have access to all the properties and methods of the product.\n\n```php\n$product = $item-\u003egetProduct();\n\necho $product-\u003ename;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevanych%2Fyii2-cart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevanych%2Fyii2-cart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevanych%2Fyii2-cart/lists"}