Ad

Is Laravel 6.15 Compatible With Any Shopping Cart Package?

- 1 answer

I am trying to develop a Laravel-based E-Commerce for a university project, nothing too fancy, it won't be released.
Premise: I am a student and a beginner, I apologize for any errors of mine. I am using Laravel 6.15.

I have made a very basic HTML+CSS template and built a custom database for the e-comm, loaded up a few records just for testing purposes: everything works (products are displayed, data is correct, no errors). I have to build a cart in order to let the end user buy stuff, but despite trying several packages, I can't add items to my cart.

Here's what I tried:

  • Tried working with Crinsane's Laravel Shopping Cart, but realized it works only with Laravel 5.6 or older versions;
  • Updated to Hardevine's Shopping Cart, since it is recommended if you have Laravel 5.7+, but I can't add items to the cart.
  • Switched package, went with Treestoneit's Shopping Cart (since it says it's for Laravel 6), same problem, cart is empty after adding any item.
  • Tried adding an item statically (no Request, no POST, just called Cart::add() with fixed parameters), no improvements.

My code is as follows (as of last update, so here I'm using Treestoneit's Cart).

CartController.php

use Illuminate\Http\Request;
use App\Product;
use Treestoneit\ShoppingCart\Facades\Cart;
...
public function store(Request $request)
    {
    $product = Product::findOrFail($request->id);
    Cart::add($product, 1);
    return redirect()->route('cart.index')->with('success_message', 'Item added to cart successfully!');
}

product.blade.php

<form action="{{ route('cart.store', $product) }}" method="POST">
    {{ csrf_field() }}
    <button type="submit" class="btn btn-primary">Add to cart!</button>
</form>

($product is being correctly retrieved, no issues with that, here it's being sent to the store() function above)

cart.blade.php

...
{{ Cart::items() }}
...
@if (session()->has('success_message'))
    {{ session()->get('success_message') }}
@endif

@if (count($errors) > 0)
    @foreach ($errors->all() as $error)
       <p>{{ $error }}</p>
    @endforeach
@endif

(before switching to the latest package I was using a @foreach statement to print out the whole cart item by item, but decided to make it easy in order to avoid any grammar error, here Cart::items() should just print the cart content, but it's empty: '[]').

Notes:

  • The redirect() works: after clicking the button in the product page, I get redirected to the cart page, but there's no success_message and the cart is still empty.
  • Already tried with cache clearing
  • I tried to pass the whole $product as argument to the store() function and going directly with Cart::add($product, 1), but no improvements.

At this point I have no idea what I'm getting into, and I'm trying to understand whether I'm missing something or there's an actual problem. Can anyone help or suggest a compatible package? Thanks in advance.

Ad

Answer

we are creators of the TreeStoneIT Shopping Cart for Laravel.

It's hard to know for certain since there is no full source to reproduce the issue, my guess would be that the Model with Buyable trait might have not been setup correctly, but can as well be something else.

You can see a full working example here: https://github.com/treeStoneIT/laravel-commerce

Ad
source: stackoverflow.com
Ad