PHP code example of al-mamun-devops / laravel-shopping-cart

1. Go to this page and download the library: Download al-mamun-devops/laravel-shopping-cart 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/ */

    

al-mamun-devops / laravel-shopping-cart example snippets


return [
    'storage' => env('SHOPPINGCART_STORAGE', 'session'), // or 'file'
    'file_path' => storage_path('app/shopping_cart.json'),
];

use Cart;

// Add products
Cart::add(1, 'Laptop', 1000, 2);
Cart::add(2, 'Mouse', 300, 1);

// Update quantity
Cart::update(1, 5);

// Remove item
Cart::remove(2);

// Get all items
$items = Cart::all();

// Get total amount
$total = Cart::total();

// Get total count
$count = Cart::count();

// Clear cart
Cart::clear();



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Cart;

class CartController extends Controller
{
    public function index()
    {
        return response()->json(Cart::all());
    }

    public function add(Request $request)
    {
        Cart::add(
            $request->id,
            $request->name,
            $request->price,
            $request->quantity
        );

        return response()->json(['message' => 'Item added successfully']);
    }

    public function clear()
    {
        Cart::clear();
        return response()->json(['message' => 'Cart cleared']);
    }
}
bash
php artisan vendor:publish --tag=config --provider="AlMamunDevOps\ShoppingCart\ShoppingCartServiceProvider"

packages/
└── al-mamun-devops/
    └── laravel-shopping-cart/
        ├── composer.json
        ├── src/
        │   ├── ShoppingCartServiceProvider.php
        │   ├── Facades/
        │   │   └── Cart.php
        │   ├── Services/
        │   │   └── CartManager.php
        │   └── Helpers/
        │       └── StorageDriver.php
        ├── config/
        │   └── shoppingcart.php
        └── README.md