Monday, April 15, 2019

how to integrate shopping cart system in laravel

In this Artical we cover many questions like:
  1. Installation shopping cart system in laravel
  2. Add class provider for  cart system in laravel
  3. How to Add Item in Cart
  4. How to Update Item in Cart
  5. How to Remove Item From Cart
  6. How to GET Item From Cart
  7. How to Get all Item From Cart
  8. How to Destroy all Item From Cart
  9. How to Find Total number of Cart
  10. How to Calculate amount of tax in the cart
  11. How to Calculate Subtotal of all items in cart
  12. How to Count all items in cart
how to integrate shopping cart system in laravel

Installation shopping cart system in laravel:

This Package install through composer How to install composer

composer require gloudemans/shoppingcart

How To Install Composer

Now you need to add class in config/app.php file

Add class provider:

Add a new line to the providers array: 

 Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class

 And optionally add a new line to the aliases array:

 'Cart' => Gloudemans\Shoppingcart\Facades\Cart::class,

 The shoppingcart gives you the following methods to use:

How to Add Item in Cart:

For Add item in cart wee use this code

Cart::add()

Cart::add('id', 'product name', quantity, price);

 Cart::add('1', 'test Product', 1, 500);

Cart::add('1', 'test Product', 1, 500, ['size' => 'large']);

 The add() method will return an CartItem instance of the item you just added to the cart.

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

When adding More than one items to the cart, the add() method will return an array of CartItems.

 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]);

How to Update Item in Cart:

Cart::update() $rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::update($rowId, 2);

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

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

How to Remove Item From Cart:

Cart::remove() $rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

Cart::remove($rowId);

How to GET Item From Cart:

Cart::get() $rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
Cart::get($rowId);

How to Get all Item From Cart:

Cart::content()

How to Destroy all Item From Cart:

Cart::destroy()

How to Find Total number of Cart:

Cart::total();

The method will automatically format the result

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

How to Calculate amount of tax in the cart:

Cart::tax();

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

How to Calculate Subtotal of all items in cart:

Cart::subtotal();

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

How to Count all items in cart:

If you want to know how many items there are in your cart.

Cart::count();

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

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

Example

Below is a simple example of how to list the cart content in a table in html laravel.

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

     <?php foreach(Cart::content() as $row) :?>

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

     <?php endforeach;?>

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

No comments:

Post a Comment