简体   繁体   English

Laravel 6.15 是否兼容任何购物车包?

[英]Is Laravel 6.15 compatible with any shopping cart package?

I am trying to develop a Laravel-based E-Commerce for a university project, nothing too fancy, it won't be released.我正在尝试为大学项目开发​​基于 Laravel 的电子商务,没什么特别的,不会发布。
Premise: I am a student and a beginner, I apologize for any errors of mine.前提:我是一名学生和初学者,对于我的任何错误,我深表歉意。 I am using Laravel 6.15.我正在使用 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).我制作了一个非常基本的 HTML+CSS 模板,并为电子商务构建了一个自定义数据库,加载了一些仅用于测试目的的记录:一切正常(显示产品,数据正确,没有错误)。 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;尝试使用Crinsane 的 Laravel Shopping Cart ,但意识到它仅适用于 Laravel 5.6 或更旧版本;
  • 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.更新到Hardevine 的 Shopping Cart ,因为如果你有 Laravel 5.7+,推荐使用它,但我无法将项目添加到购物车。
  • 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.切换包,与Treestoneit 的购物车一起使用(因为它说它适用于 Laravel 6),同样的问题,添加任何项目后购物车是空的。
  • Tried adding an item statically (no Request, no POST, just called Cart::add() with fixed parameters), no improvements.尝试静态添加项目(没有请求,没有 POST,只是使用固定参数调用 Cart::add()),没有改进。

My code is as follows (as of last update, so here I'm using Treestoneit's Cart).我的代码如下(截至上次更新,所以这里我使用的是 Treestoneit 的购物车)。

CartController.php购物车控制器.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产品.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) ($product 被正确检索,没有问题,这里它被发送到上面的 store() 函数)

cart.blade.php购物车.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: '[]'). (在切换到最新包之前,我使用 @foreach 语句逐项打印出整个购物车,但为了避免任何语法错误,我决定简化一下,这里 Cart::items() 应该只打印购物车内容,但它是空的:'[]')。

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. redirect() 有效:单击产品页面中的按钮后,我被重定向到购物车页面,但没有 success_message 并且购物车仍然是空的。
  • 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.我试图将整个 $product 作为参数传递给 store() 函数并直接使用 Cart::add($product, 1),但没有改进。

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.提前致谢。

we are creators of the TreeStoneIT Shopping Cart for Laravel.我们是 Laravel 的 TreeStoneIT 购物车的创建者。

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.很难确定,因为没有完整的来源来重现这个问题,我的猜测是具有Buyable特征的模型可能没有正确设置,但也可能是其他东西。

You can see a full working example here: https://github.com/treeStoneIT/laravel-commerce你可以在这里看到一个完整的工作示例: https : //github.com/treeStoneIT/laravel-commerce

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM