[英]Online shopping cart with Laravel
I am using Laravel and Moltin/Cart to build the cart system for an online store. 我正在使用Laravel和Moltin / Cart构建在线商店的购物车系统。 So far, I have managed to integrate the Cart system and continue with paypal.
到目前为止,我已经成功集成了Cart系统并继续使用Paypal。 Though, in my controller I have a constructor that prevents the user from viewing, adding or removing items from the cart unless he is authenticated.
但是,在我的控制器中,我有一个构造函数,除非用户经过验证,否则它可以防止用户查看,添加或删除购物车中的物品。
public function __construct() {
parent::__construct();
$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('auth', array('only'=>array('postAddtocart', 'getCart', 'getRemoveitem')));
}
In order to make paypal to work I had to add several hidden inputs in my view that get the necessary values and passing them to paypal's paying page, like so: 为了使Paypal正常工作,我必须在视图中添加一些隐藏的输入,以获取必要的值并将其传递到Paypal的付款页面,如下所示:
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="office@shop.com">
<input type="hidden" name="item_name" value="eCommerce Store Purchase">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="amount" value="{{ Cart::total() }}">
<input type="hidden" name="first_name" value="{{ Auth::user()->firstname }}">
<input type="hidden" name="last_name" value="{{ Auth::user()->lastname }}">
<input type="hidden" name="email" value="{{ Auth::user()->email }}">
{{ HTML::link('/', 'Continue Shopping', array('class'=>'btn btn-default')) }}
<input type="submit" value="Checkout with Paypal" class="btn btn-primary">
The problem is that the user shouldn't be logged in to view, add or remove items from the cart, but to be asked for a login when the submit button is clicked. 问题在于,不应单击该用户来查看,添加或删除购物车中的项目,而应在单击“提交”按钮时要求其登录。 If I remove the filters from my constructor then, I get a "Trying to get property of non-object" error because of the hidden inputs that make use of the Auth class.
如果我从构造函数中删除过滤器,则由于使用Auth类的隐藏输入,会出现“尝试获取非对象的属性”错误。 I have tried to add a blade if auth::check condition but that wasn't the solution.
我曾尝试在auth :: check条件下添加刀片,但这不是解决方案。 Any suggestions?
有什么建议么?
Answer 回答
This did the trick: 这达到了目的:
@if(!Auth::check())
{{ HTML::link('users/signin', 'Sign in to pay', array('class'=>'btn btn-primary')) }}
@else
<input type="hidden" name="first_name" value="{{ Auth::user()->firstname }}">
<input type="hidden" name="last_name" value="{{ Auth::user()->lastname }}">
<input type="hidden" name="email" value="{{ Auth::user()->email }}">
{{ HTML::link('/', 'Continue Shopping', array('class'=>'btn btn-default')) }}
<input type="submit" value="Checkout with Paypal" class="btn btn-primary">
@endif
Also, I removed the filters from the constructor and changed the redirection in the sign in function, so that it redirects to the previously accessed page. 另外,我从构造函数中删除了过滤器,并更改了登录功能中的重定向,以便将其重定向到以前访问的页面。
Using a session variable to hold the previously visited url 使用会话变量保存以前访问的URL
In order to go back to the previously visited page you need to use session::put
. 为了返回到先前访问的页面,您需要使用
session::put
。
So, in the getSignin function add: 因此,在getSignin函数中添加:
Session::put('previous_url', URL::previous());
And in the postSignin function you need to retrieve the previous_url variable, which is stored in the session, like so: 在postSignin函数中,您需要检索previous_url变量,该变量存储在会话中,如下所示:
if ( Session::has('previous_url') )
{
$url = Session::get('previous_url');
Session::forget('previous_url');
return Redirect::to($url);
}
如何将签出按钮替换为登录按钮,然后再次重定向到购物车签出呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.