PHP code example of mateusjatenee / shoppingcart

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

    

mateusjatenee / shoppingcart example snippets


Cart::add('293ad', 'Product 1', 1, 9.99);

Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);

Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);

Cart::add($product, 1, ['size' => 'large']);

Cart::add($product, 1, ['size' => 'large']);

Cart::add([
  ['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00],
  ['id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => ['size' => 'large']]
]);

Cart::add([$product1, $product2]);

   
Cart::add($product, 1, ['size' => 'large'])->setFree();

### Cart::update()

To update an item in the cart, you'll first need the rowId of the item.
Next you can use the `update()` method to update it.

If you simply want to update the quantity, you'll pass the update method the rowId and the new quantity:


Cart::update($rowId, ['name' => 'Product 1']); // Will update the name

Cart::update($rowId, $product); // Will update the id, name and price


$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::remove($rowId);

$rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::get($rowId);

Cart::content();

Cart::instance('wishlist')->content();

Cart::destroy();

Cart::total();

Cart::total($decimals, $decimalSeperator, $thousandSeperator);

Cart::tax();

Cart::tax($decimals, $decimalSeperator, $thousandSeperator);

Cart::subtotal();

Cart::subtotal($decimals, $decimalSeperator, $thousandSeperator);

Cart::count();

$cart->search(function ($cartItem, $rowId) {
	return $cartItem->id === 1;
});

Cart::content()->count();

Cart::content()->groupBy('id');

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

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

Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, ['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();


// First we'll add the item to the cart.
$cartItem = Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']);

// Next we associate a model with the item.
Cart::associate($cartItem->rowId, 'Product');

// Or even easier, call the associate method on the CartItem!
$cartItem->associate('Product');

// You can even make it a one-liner
Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large'])->associate('Product');

// Now, when iterating over the content of the cart, you can access the model.
foreach(Cart::content() as $row) {
	echo 'You have ' . $row->qty . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.';
}
    
$discount = new Discount(5, [
  'qty' => 'min:2|max:10',
  'price' => 'min:5|max:20',
]);

$item = Cart::add('293ad', 'Laravel T-Shirt', 1, 100);
$item->setDiscount($discount);

$item->price // returns the price with the discount if validation passes or the regular price if it does not.  
   
$item = Cart::add('293ad', 'Laravel T-Shirt', 1, 100);

Cart::setTax($item->rowId, 10); // 10% percent 

$item->tax // 10
$item->priceTax // 110 (100 + 10)
$item->subtotal // 100 (quantity * price)
$item->total // 110 (quantity * priceTax)

// you can also set static taxes by passing a third argument as true. For instance, you want the tax to be 20, always.  

$item = Cart::add('293ad', 'Laravel T-Shirt', 1, 100);

Cart::setTax($item->rowId, 20, true); // 10% percent 

$item->tax // 20
$item->priceTax // 120 (100 + 20)
$item->subtotal // 100 (quantity * price)
$item->total // 120 (quantity * priceTax)


// Add some items in your Controller.
Cart::add('192ao12', 'Product 1', 1, 9.99);
Cart::add('1239ad0', 'Product 2', 2, 5.95, ['size' => 'large']);

// Display the content in a View.
<table>
   	<thead>
       	<tr>
           	<th>Product</th>
           	<th>Qty</th>
           	<th>Price</th>
           	<th>Subtotal</th>
       	</tr>
   	</thead>

   	<tbody>

   		 foreach(Cart::content() as $row) :