PHP code example of nhanchaukp / codeigniter4-cart-library

1. Go to this page and download the library: Download nhanchaukp/codeigniter4-cart-library library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nhanchaukp / codeigniter4-cart-library example snippets


$psr4 = [
    'CodeIgniterCart' => ROOTPATH . 'vendor/nhanchaukp/codeigniter4-cart-library/src'

    // OTHER PSR4 ENTRIES
];

 // Call the cart service
 $cart = \Config\Services::cart();
 
 // Insert an array of values
 $cart->insert(array(
    'id'      => 'sku_1234ABCD',
    'qty'     => 1,
    'price'   => '19.56',
    'name'    => 'T-Shirt',
    'options' => array('Size' => 'L', 'Color' => 'Red')
));
 
 // Update an array of values
 $cart->update(array(
    'rowid'   => '4166b0e7fc8446e81e16883e9a812db8',
    'id'      => 'sku_1234ABCD',
    'qty'     => 3,
    'price'   => '24.89',
    'name'    => 'T-Shirt',
    'options' => array('Size' => 'L', 'Color' => 'Red')
));

// Get the total items. Formerly known as total_items()
$cart->totalItems();

// Find item
$cart->find($key, $value);
Ex: $cart->find('id', 'sku_1234ABCD');

// Format Vietnamese dong
$cart->formatVnd($number);

// Remove an item using its `rowid`
$cart->remove('4166b0e7fc8446e81e16883e9a812db8');
   
// Clear the shopping cart
$cart->destroy();

// Get the cart contents as an array
$cart->contents();