将所选数量添加到购物车而不是仅更新 [英] Add Quantity Selected to Cart Instead of Just Updating

查看:110
本文介绍了将所选数量添加到购物车而不是仅更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含项目和数量选择表单的索引页面。


此表单适用于创建(又名第一次点击)。问题是,如果我再次单击它,它只是更新购物车,而不是将该数量添加到 line_item 。如果这是我唯一的问题,我可以自己解决它。



真正的问题是,我有另一种表单在结账前更新。它看起来像这样:





我希望这是主数量控制,所以无论如何,数量将用于更新。但是对于第一张图片,我希望这个数量加到 @line_item 的数量上,所以我不能只是制作一个只添加新旧数量的方法一起,这是我开始做的事情,直到我意识到我无法做到这一点。



我需要在控制器中做一个新的动作吗? >
解决此问题的方法是什么?

解决方案

b

问题是,如果再次单击它,它只是更新购物车,而不是将该数量添加到line_item



问题在于您的提交按钮 添加到购物车,它会将您的创建操作购物车,因此在购物车内创建一个新商品



我是否需要制作新的动作在控制器中?



我的答案是肯定的。您需要使用邮政路线请求(找到该物品)制作新的动作,然后在该动作中更新其数量。



这会是什么工作?



如果你看看你的按钮,或者我应该说 form 创建一个新项目,然后它就是您的表单中的路径或url部分,它会将其传递到控制器中的方法。如果您更改了它的网址,那么它会将它带到您的自定义方法。



修复

所有你需要的是一些js魔法,在用户点击你的添加到购物车按钮后,动态更改网址或你的表单。例如:

  $(document).on(click,your_button_class,function(){
$(this).closest(your_form_class)。attr(action,path_of_new_method);
});

您还必须通过添加此表单 隐藏字段或其他内容,然后在该控制器方法内使用该ID找到该项目以更新其数量。



注意:您需要调用此js在您的表单提交并且已经创建了一个新项目之后,否则它可能会给您带来麻烦。


I have an index page with items and a quantity select form.

The form works for create (a.k.a. first click). The problem is, if I click it again, it's just updating the cart instead of adding that quantity to the line_item. If this was my only problem I would be able to solve it myself.

The real problem is that I have another form that updates right before checkout. It looks like this:

I want this to be the master quantity control, so whatever goes in that form is what the quantity will be for update. But for the first images, I want that quantity to add to the quantity of the @line_item, so I can't just make a method that just adds the new and old quantities together, which is what I started doing until I realized I wouldn't be able to do that.

Do I need to make a new action in the controller?
What would be the work around for this?

解决方案

Looking at your question

The problem is, if I click it again, it's just updating the cart instead of adding that quantity to the line_item

The problem is your submit button Add to Cart which takes it to the create action of your cart and hence creating a new item inside your cart

Do I need to make a new action in the controller?

My answer would be yes. You need to make a new action with post route request(to find that item) and then update its quantity inside that action.

What would be the work around for this?

If you look at your button or rather i should say form for creating a new item then it's the path or url part in your form which takes it to your method inside your controller. If you change its url then it will take it to your custom method.

Fix

All you need is some js magic to dynamically change the url or your form after a user has clicked on your Add to Cart button. Something like:

$(document).on("click","your_button_class",function(){
  $(this).closest("your_form_class").attr("action","path_of_new_method");
});

You will also have to supply your items id by this form by adding a hidden field or something and then find that item inside controller method with that id to update its quantity.

Note: You need to call this js after your form is submitted and a new item is already created else it can trouble you.

这篇关于将所选数量添加到购物车而不是仅更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆