PHP code example of jackiedo / shoppingcart

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

    

jackiedo / shoppingcart example snippets


...
"...
    "jackiedo/shoppingcart": "1.*"
},

...
'providers' => array(
    ...
    'Jackiedo\Shoppingcart\ShoopingcartServiceProvider',
),

'Cart'      => 'Jackiedo\Shoppingcart\Facades\Cart',

/**
 * Add a new item to the cart
 *
 * @param string|int    $id       ID of the item (such as product's id)
 * @param string        $title    Name of the item
 * @param int           $qty      Item qty to add to the cart
 * @param float         $price    Price of one item
 * @param array         $options  Array of additional options, such as 'size' or 'color'
 * @return Jackiedo\Shoppingcart\CartItem|null
 */
Cart::add( $id, $title, $quantity, $price [, $options = array()] );

$row = Cart::add(37, 'Item Title', 5, 100.00, ['color' => 'red', 'size' => 'M']);

// Collection CartItem: {
//    rawId    : '8a48aa7c8e5202841ddaf767bb4d10da'
//    id       : 37
//    title    : 'Item Title'
//    qty      : 5
//    price    : 100.00
//    subtotal : 500.00
//    options  : Collection CartItemOptions: {
//                      color : 'red'
//                      size  : 'M'
//                  }
// }

$rawId = $row->rawId(); // get rawId (8a48aa7c8e5202841ddaf767bb4d10da)
$rowQty = $row->qty; // 5
...

/**
 * Update the quantity of one row of the cart
 *
 * @param  string       $rawId       The rawId of the item you want to update
 * @param  int|array    $attribute   New quantity of the item|Array of attributes to update
 * @return Jackiedo\Shoppingcart\CartItem
 */
Cart::update(string $rawId, int $quantity);
Cart::update(string $rawId, array $arrtibutes);

$rawId = '8a48aa7c8e5202841ddaf767bb4d10da';

// Update title and options
$row = Cart::update($rawId, ['title' => 'New item name', 'options' => ['color' => 'yellow']]);

// or only update quantity
$row = Cart::update($rawId, 5);

/**
 * Get the cart content
 *
 * @return \Illuminate\Support\Collection
 */
Cart::all();
// or use alias
Cart::content();

$items = Cart::content();

// Collection $items: {
//     8a48aa7c8e5202841ddaf767bb4d10da: {
//         rawId: '8a48aa7c8e5202841ddaf767bb4d10da',
//         id: 37,
//         title: 'New item name',
//         qty: 5,
//         price: 100.00,
//         subtotal: 500.00,
//         options: {
//             'color': 'yellow',
//             'size': 'M'
//         }
//     },
//     4c48ajh68e5202841ed52767bb4d10fc: {
//         rawId: '4c48ajh68e5202841ed52767bb4d10fc',
//         id: 42,
//         title: 'Men T-Shirt Apolo',
//         qty: 1,
//         price: 1000.00,
//         subtotal: 1000.00,
//         options: {
//             'color': 'red',
//             'size': 'L'
//         }
//     }
//     ...
// }

/**
 * Get a row of the cart by its unique ID
 *
 * @param  string  $rawId  The ID of the row to fetch
 * @return Jackiedo\Shoppingcart\CartItem
 */
Cart::get(string $rawId);

$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');

// Collection $item: {
//    rawId    : '8a48aa7c8e5202841ddaf767bb4d10da'
//    id       : 37
//    title    : 'Item Title'
//    qty      : 5
//    price    : 100.00
//    subtotal : 500.00
//    options  : {
//        'color'   : 'red',
//        'size'    : 'M'
//    }
// }

/**
 * Remove a row from the cart
 *
 * @param  string  $rawId  The unique ID of the item
 * @return boolean
 */
Cart::remove(string $rawId);

Cart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

/**
 * Empty the cart
 *
 * @return boolean
 */
Cart::destroy();

Cart::destroy();

/**
 * Get the price total
 *
 * @return float
 */
Cart::total();

$total = Cart::total();

/**
 * Get the number of items in the cart
 *
 * @param  boolean  $totalItems  Get all the items (when false, will return the number of rows)
 * @return int
 */
Cart::count(false);
// or use alias
Cart::countRows();

Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(127, 'foobar', 15, 100.00, ['color' => 'green', 'size' => 'S']);
$rows = Cart::countRows(); // 2

/**
 * Get the number of items in the cart
 *
 * @param  boolean  $totalItems  Get all the items (when false, will return the number of rows)
 * @return int
 */
Cart::count($totalItems = true);

Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
$count = Cart::count(); // 11 (5+1+5)

/**
 * Search if the cart has a item
 *
 * @param  array  $search  An array with the item ID and optional options
 * @return Illuminate\Support\Collection;
 */
Cart::search(array $conditions);

$items = Cart::search(['options' => ['color' => 'red']]);
$items = Cart::search(['title' => 'Item name']);
$items = Cart::search(['id' => 10, 'options' => ['size' => 'L']]);

Cart::instance('shopping')->add('37', 'Product 1', 1, 9.99);

// Get the content of the 'shopping' cart
Cart::content();

Cart::instance('wishlist')->add('42', 'Product 2', 1, 19.95, array('size' => 'medium'));

// Get the content of the 'wishlist' cart
Cart::content();

// If you want to get the content of the 'shopping' cart again...
Cart::instance('shopping')->content();

// And the count of the 'wishlist' cart again
Cart::instance('wishlist')->count();

/**
 * Set the associated model
 *
 * @param  string    $modelName        The name of the model
 * @param  string    $modelNamespace   The namespace of the model
 * @return Jackiedo\Shoppingcart\Cart
 */
Cart::associate(string $modelName, string $modelNamespace = null);

Cart::associate('ShoppingProduct', 'App\Models');
$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->shopping_product->title; // $item->shopping_product is instance of 'App\Models\ShoppingProduct'

Event::on('cart.adding', function($attributes, $cart){
    // code
});