PHP code example of darthsoup / shoppingcart

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

    

darthsoup / shoppingcart example snippets


/**
 * Add a Item to the cart.
 *
 * @param string|array $id Unique ID of the item|Item formatted as array|Array of items
 * @param string $name Name of the item
 * @param int $quantity Item quantity to add to the cart
 * @param float $price Price of one item
 * @param array $options Array of additional options, such as 'size' or 'color'
 */

// Basic form
Cart::add('1', 'Product One', 1, 9.99, ['option_key' => 'option_value']);

// Array form
Cart::add(['id' => 'mail1000', 'name' => 'Mail Package One', 'quantity' => 5, 'price' => 4.99, 'options' => []]);

// Batch method
Cart::add([
    ['id' => '15', 'name' => 'Hamburger', 'quantity' => 1, 'price' => 1.99],
    ['id' => '16', 'name' => 'Cheeseburger', 'quantity' => 1, 'price' => 2.49, 'options' => ['onion' => false]]
]);

$item = new \DarthSoup\Cart\Item('15', 'Hamburger', 1.99, ['onion' => false]);

Cart::add($item);

Cart::update('rowId', [
    'options' => ['foo' => 'bar']
]);

Cart::get('rowId');

Cart::content();

Cart::destroy();

Cart::remove('rowId');

Cart::total();

Cart::count();

$hamburger = Cart::add('15', 'Hamburger', 1, 1.99, ['onion' => false]);

Cart::addSubItem('99', 'Extra Bacon', 1, 0.99, [], $hamburger->getRowId());

 

Cart::associate(\App\Product::class)->add('15', 'Hamburger', 1, 9.99, ['extra_sauce' => true]);

$content = Cart::content();

foreach ($content as $row) {
	echo 'You have ' . $row->quantity . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.';
}