PHPackages                             bertugfahriozer/ci4shoppingcart - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. bertugfahriozer/ci4shoppingcart

ActiveLibrary[Framework](/categories/framework)

bertugfahriozer/ci4shoppingcart
===============================

Codeigniter 4 Shopping Cart

1.1.2(11mo ago)5135MITPHPPHP ^7.4||^8.0CI passing

Since Oct 28Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/bertugfahriozer/ci4shoppingcart)[ Packagist](https://packagist.org/packages/bertugfahriozer/ci4shoppingcart)[ RSS](/packages/bertugfahriozer-ci4shoppingcart/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)DependenciesVersions (8)Used By (0)

Shopping Cart Class
===================

[](#shopping-cart-class)

The Cart Class permits items to be added to a session that stays active while a user is browsing your site. These items can be retrieved and displayed in a standard “shopping cart” format, allowing the user to update the quantity or remove items from the cart.

Please note that the Cart Class ONLY provides the core “cart” functionality. It does not provide shipping, credit card authorization, or other processing components.

- [Shopping Cart Class](#shopping-cart-class) - [Important](#important)
- [Using the Cart Class](#using-the-cart-class)
    - [Initializing the Shopping Cart Class](#initializing-the-shopping-cart-class)
        - [Important](#important-1)
        - [Composer installation](#composer-installation)
- [What is the Row ID](#what-is-a-row-id)
- [Class Reference](#class-reference)

🪴 Project Activity
==================

[](#-project-activity)

[![Alt](https://camo.githubusercontent.com/7076a1883add52239f9a305eff59193f50b0e56d29f5a46d62f96e6cc4b484be/68747470733a2f2f7265706f62656174732e6178696f6d2e636f2f6170692f656d6265642f646436346632373062643631616362346266653761393232333136633137656237636362396139332e737667 "Repobeats analytics image")](https://camo.githubusercontent.com/7076a1883add52239f9a305eff59193f50b0e56d29f5a46d62f96e6cc4b484be/68747470733a2f2f7265706f62656174732e6178696f6d2e636f2f6170692f656d6265642f646436346632373062643631616362346266653761393232333136633137656237636362396139332e737667)

[Using the Cart Class](#using-the-cart-class)
---------------------------------------------

[](#using-the-cart-class)

### [Initializing the Shopping Cart Class](#initializing-the-shopping-cart-class)

[](#initializing-the-shopping-cart-class)

#### Important

[](#important)

The Cart class utilizes CodeIgniter’s Session Class to save the cart information to a database, so before using the Cart class you must set up a database table as indicated in the Session Documentation, and set the session preferences in your .env file to utilize a database.

---

#### Composer installation

[](#composer-installation)

`composer require bertugfahriozer/ci4shoppingcart`

**When you install composer, you will see example directory. You can copy and paste your project or how do you want.**

To initialize the Shopping Cart Class in your controller constructor, use the `$cart=new Cart()` class:

```
use ci4shoppingcartLibrariesCart;

public $cart

public function __contruct(){
  $this->cart=new Cart();
}
```

Once loaded, the Cart object will be available using:

```
$this->cart
```

Note The Cart Class will load and initialize the Session Class automatically, so unless you are using sessions elsewhere in your application, you do not need to load the Session class.

### [Adding an Item to The Cart](#adding-an-item-to-the-cart)

[](#adding-an-item-to-the-cart)

To add an item to the shopping cart, simply pass an array with the product information to the `$this->cart->insert()` method, as shown below:

```
$data = array(
        'id'      => 'sku_123ABC',
        'qty'     => 1,
        'price'   => 39.95,
        'name'    => 'T-Shirt',
        'options' => array('Size' => 'L', 'Color' => 'Red')
);

$this->cart->insert($data);
```

Important The first four array indexes above (id, qty, price, and name) are **required**. If you omit any of them the data will not be saved to the cart. The fifth index (options) is optional. It is intended to be used in cases where your product has options associated with it. Use an array for options, as shown above.

The five reserved indexes are:

- **id** - Each product in your store must have a unique identifier. Typically this will be an “sku” or other such identifier.
- **qty** - The quantity being purchased.
- **price** - The price of the item.
- **name** - The name of the item.
- **options** - Any additional attributes that are needed to identify the product. These must be passed via an array.

In addition to the five indexes above, there are two reserved words: rowid and subtotal. These are used internally by the Cart class, so please do NOT use those words as index names when inserting data into the cart.

Your array may contain additional data. Anything you include in your array will be stored in the session. However, it is best to standardize your data among all your products in order to make displaying the information in a table easier.

```
$data = array(
        'id'      => 'sku_123ABC',
        'qty'     => 1,
        'price'   => 39.95,
        'name'    => 'T-Shirt',
        'coupon'         => 'XMAS-50OFF'
);

$this->cart->insert($data);
```

The `insert()` method will return the $rowid if you successfully insert a single item.

### [Adding Multiple Items to The Cart](#adding-multiple-items-to-the-cart)

[](#adding-multiple-items-to-the-cart)

By using a multi-dimensional array, as shown below, it is possible to add multiple products to the cart in one action. This is useful in cases where you wish to allow people to select from among several items on the same page.

```
$data = array(
        array(
                'id'      => 'sku_123ABC',
                'qty'     => 1,
                'price'   => 39.95,
                'name'    => 'T-Shirt',
                'options' => array('Size' => 'L', 'Color' => 'Red')
        ),
        array(
                'id'      => 'sku_567ZYX',
                'qty'     => 1,
                'price'   => 9.95,
                'name'    => 'Coffee Mug'
        ),
        array(
                'id'      => 'sku_965QRS',
                'qty'     => 1,
                'price'   => 29.95,
                'name'    => 'Shot Glass'
        )
);

$this->cart->insert($data);
```

### [Displaying the Cart](#displaying-the-cart)

[](#displaying-the-cart)

To display the cart you will create a view file with code similar to the one shown below.

```
