PHP code example of shahadat / shoppingcart

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

    

shahadat / shoppingcart example snippets


Cart::add('123', 'Product Name', 1, 100.00);

Cart::add('123', 'Product Name', 1, 9.99, ['color' => 'blue', 'size' => 'XXL']);

Cart::add(['id' => '123', 'name' => 'Product Name', 'qty' => 1, 'price' => 9.99, 'options' => ['color' => 'blue', 'size' => 'XXL']]);

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

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

Cart::add([
  ['id' => '123', 'name' => 'Product Name', 'qty' => 1, 'price' => 100.00],
  ['id' => '123', 'name' => 'Product Name', 'qty' => 1, 'price' => 100.00, 'options' => ['size' => 'XXL']]
]);

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


$rowId = '2decca212ac0e35428b051aa0cc488ed';

Cart::update($rowId, 2); // Will update the quantity

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

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


$rowId = '2decca212ac0e35428b051aa0cc488ed';

Cart::remove($rowId);

$rowId = '2decca212ac0e35428b051aa0cc488ed';

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('123', 'Product Name', 1, 100.00);

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

Cart::instance('wishlist')->add('123', 'Product Name', 1, 100.00, ['size' => 'XXL']);

// 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('123', 'Product Name', 1, 100.00, ['size' => 'XXL']);

// 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('123', 'Product Name', 1, 100.00, ['size' => 'XXL'])->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.';
}


// Add some items in your Controller.
Cart::add('123', 'Product Name', 1, 100);
Cart::add('123', 'Product Name', 2, 99.99, ['size' => 'XXL']);

// 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)

       		<tr>
           		<td>
               		<p><strong>{{ $row->name }}</strong></p>
               		<p>{{ $row->options->has('size') ? $row->options->size : '' }}</p>
           		</td>
           		<td><input type="text" value="{{ $row->qty }}"></td>
           		<td>${{ $row->price }}</td>
           		<td>${{ $row->total }}</td>
       		</tr>

	   	@endforeach

   	</tbody>
   	
   	<tfoot>
   		<tr>
   			<td colspan="2">&nbsp;</td>
   			<td>Subtotal</td>
   			<td>{{ Cart::subtotal() }}</td>
   		</tr>
   		<tr>
   			<td colspan="2">&nbsp;</td>
   			<td>Tax</td>
   			<td>{{ Cart::tax() }}</td>
   		</tr>
   		<tr>
   			<td colspan="2">&nbsp;</td>
   			<td>Total</td>
   			<td>{{ Cart::total() }}</td>
   		</tr>
   	</tfoot>
</table>