PHP code example of lombervid / shoppingcart

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

    

lombervid / shoppingcart example snippets


use Lombervid\ShoppingCart\ShoppingCart;

$shoppingCart = new ShoppingCart();

use Lombervid\ShoppingCart\Item;
use Lombervid\ShoppingCart\ShoppingCart;

$cart = new ShoppingCart();
$cart->add(new Item('1', 'Cake', 15.56));
$cart->add(new Item('15', 'Frappe', 5));

foreach ($cart->items() as $item) {
	// $item->id
	// $item->name
}

array:2 [▼
  1 => Lombervid\ShoppingCart\Item {#5 ▼
    -id: "1"
    -name: "Cake"
    -price: 15.56
  }
  15 => Lombervid\ShoppingCart\Item {#6 ▼
    -id: "15"
    -name: "Frappe"
    -price: 5.0
  }
]

[
    'field_name'   => 'field_value',
    'field_2_name' => 'field_2_value'
]

$fields = [
	'size'  => 'XL',
	'color' => 'blue'
];

$item = new Item('23', 'My Shirt', 2.5, fields: $fields);
$cart->add($item);

array:1 [▼
  23 => Lombervid\ShoppingCart\Item {#5 ▼
    -id: "23"
    -name: "My Shirt"
    -price: 2.5
    -qty: 1
    -fields: array:2 [▼
      "size" => "XL"
      "color" => "blue"
    ]
  }
]

foreach ($cart->items() as $item) {
    // $item->size
    // $item->color
}

$cart->remove(23);

$shoppingCart->clear();

[
    'name'     => 'shopping_cart',
    'autosave' => true,
    'tax'      => 0,
    'shipping' => [
        'amount' => 0,
        'free'   => 0,
    ],
]