PHP code example of devbuddy / cart

1. Go to this page and download the library: Download devbuddy/cart 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/ */

    

devbuddy / cart example snippets


use Cart;

// Quick Usage with the Product Model Association & User session binding

$Product = Product::find($productId); // assuming you have a Product model with id, name, description & price

// add the product to cart
$rowid = Cart::insert(array(
    'id' => $Product->id,
    'name' => $Product->name,
    'price' => $Product->price,
    'quantity' => 4,
    'options' => array()
));

// update the item on cart
Cart::update($rowid,[
	'quantity' => 2,
	'price' => 98.67
]);

// delete an item on cart
Cart::remove($rowid);

// view the cart items
$items = Cart::contents();
foreach($items as $row) {

	echo $row['id']; // row ID
	echo $row['name'];
	echo $row['qty'];
	echo $row['price'];
	
}

// FOR FULL USAGE, SEE BELOW..

Cart::insert();
Cart::update();
Cart::total();
Cart::remove();
Cart::total_items();
Cart::contents();
Cart::get_item();
Cart::has_options();
Cart::product_options();
Cart::format_number();
Cart::destroy();


// Simplest form to add item on your cart
Cart::add(455, 'Sample Item', 100.99, 2, array());

// array format
Cart::insert(array(
    'id' => 456, // inique row ID
    'name' => 'Sample Item',
    'price' => 67.99,
    'quantity' => 4,
    'options' => array()
));

// add multiple items at one time
Cart::insert(array(
  array(
      'id' => 456,
      'name' => 'Sample Item 1',
      'price' => 67.99,
      'quantity' => 4,
      'options' => array()
  ),
  array(
      'id' => 568,
      'name' => 'Sample Item 2',
      'price' => 69.25,
      'quantity' => 4,
      'options' => array(
        'size' => 'L',
        'color' => 'blue'
      )
  ),
));
// NOTE:
// Please keep in mind that when adding an item on cart, the "id" should be unique as it serves as
// row identifier as well. If you provide same ID, it will assume the operation will be an update to its quantity
// to avoid cart item duplicates

$data = array(
        'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
        'qty'   => 3
);

Cart::update($data);

// Or a multi-dimensional array

$data = array(
        array(
                'rowid'   => 'b99ccdf16028f015540f341130b6d8ec',
                'qty'     => 3
        ),
        array(
                'rowid'   => 'xw82g9q3r495893iajdh473990rikw23',
                'qty'     => 4
        ),
        array(
                'rowid'   => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
                'qty'     => 2
        )
);

Cart::update($data);
//You may also update any property you have previously defined when inserting the item such as options, price or other custom fields.
$data = array(
        'rowid'  => 'b99ccdf16028f015540f341130b6d8ec',
        'qty'    => 1,
        'price'  => 49.95,
        'coupon' => NULL
);

$this->cart->update($data);

/**
 * removes an item on cart by item ID
 *
 * @param $rowid
 */

Cart::remove($rowid);



Cart::contents();