Easily Add Products to Customer Carts from Admin Panel
As an admin of your e-commerce platform, you might want the ability to pre-populate a customer’s cart before they even log in to make a purchase. This can be particularly useful for special promotions, gift orders, or managing customer accounts. In this guide, I’ll walk you through how to implement this feature using Laravel and its default shopping cart functionality.
Understanding Customer Cart Management
In the Laravel Shop package, which is built into every fresh installation of Laravel 8.x, the customer’s cart is stored in a session by default. This means each customer has their own unique shopping cart that persists across sessions until they checkout or clear it manually.
The Cart facade provides methods to add items (add()), get the cart contents (all()), and calculate totals (total(), tax(), etc.). To leverage this for admin use, we need to modify the session-based system slightly.
Adding Products to a Customer’s Cart from Admin
To achieve this, we’ll need to modify the way the cart is handled. Since sessions are user-specific by nature, we can’t directly manipulate another user’s cart through the standard Cart facade. Instead, we’ll create a new model and use database storage for the shared cart.
First, let’s create a migration for our shared cart items:
Schema::create('shared_cart_items', function (Blueprint $table) {
$table->id();
$table->string('customer_id')->nullable();
$table->unsignedBigInteger('product_id');
$table->integer('quantity');
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
Next, generate the model and controller:
php artisan make:model SharedCartItem -mcr
Modifying Cart Storage
In your Laravel 8.x installation, navigate to config/shop.php and change the default storage configuration:
'storage' => [
'cart' => \Shop\Cart\CartManager::class,
],
Now, let’s create a new cart manager that uses our SharedCartItem model instead of the session-based system.
namespace Shop\Cart;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Shop\Models\SharedCartItem;
use Shop\Models\Product;
use Illuminate\Support\Facades\Auth;
class SharedCartManager extends CartBase
{
public function add(Product $product, int $quantity = 1): self
{
// Check if the user is logged in (required for admin use)
if (! Auth::check()) {
throw new ModelNotFoundException('User must be authenticated.');
}
return tap($this->cart)->addItem($product->id, $quantity);
}
protected function getCartItems(): Collection
{
// Fetch from the database instead of the session
return SharedCartItem::where('customer_id', Auth::id())->get();
}
}
Adding Items to a Customer’s Cart from Admin Panel
Now that our new cart manager is in place, we can add items to a customer’s cart from the admin panel. In your admin controller or route, simply use the add() method:
public function addToCart(Request $request)
{
// Assuming the request contains 'customer_id' and 'product_id'
$customer = User::findOrFail($request->input('customer_id'));
$product = Product::findOrFail($request->input('product_id'));
app(\Shop\Facades\Cart::class)->add($product, 1);
return redirect()->back()->with('success', 'Item added to the customer\'s cart.');
}
Conclusion
By leveraging Laravel’s built-in shopping cart functionality and creating a custom storage mechanism for shared cart items, we’ve successfully implemented a feature that allows admins to pre-populate customer carts.
Remember, this solution assumes you have an admin panel and proper authentication in place. The code snippets provided are meant to give you a starting point; be sure to adapt them to fit your specific application structure.
For more advanced customization options and detailed documentation on Laravel’s e-commerce features, I recommend checking out the official Laravel documentation or our in-depth guides at LaravelCompany.com.