简体   繁体   English

添加到购物车目录中的按钮(Ruby on Rails,Spree gem)

[英]Add to cart buttons in catalogue (Ruby on Rails, Spree gem)

How can i add add to cart button to every product in catalogue? 如何为目录中的每个产品添加添加到购物车按钮? I created link 我创建了链接

  <%= link_to fast_cart_path do %>
     Add To Cart
  <% end %>

And action, copy of OrdersController#update 和动作,OrdersController#update的副本

  def fast_cart
    populator = Spree::OrderPopulator.new(current_order(true), current_currency)
    if populator.populate(params.slice(:products, :variants, :quantity))
      current_order.create_proposed_shipments if current_order.shipments.any?

      fire_event('spree.cart.add')
      fire_event('spree.order.contents_changed')
      respond_with(@order) do |format|
        format.html { redirect_to cart_path }
      end
    else
      flash[:error] = populator.errors.full_messages.join(" ")
      redirect_to :back
    end
  end

I don't have any error, but for some reason product not adding to cart. 我没有任何错误,但由于某种原因产品没有添加到购物车。

If you look at the code in the Spree::OrderPopulator: https://github.com/spree/spree/blob/v2.0.4/core/app/models/spree/order_populator.rb#L20 如果你看一下Spree :: OrderPopulator中的代码: https//github.com/spree/spree/blob/v2.0.4/core/app/models/spree/order_populator.rb#L20

You'll see that it takes in variables from the hash passed in, and tries to add anything passed in from products or variants in the hash. 您将看到它从传入的哈希中获取变量,并尝试添加从哈希中的产品或变体传入的任何内容。

Your code above takes in 您上面的代码接受了

params.slice(:products, :variants, :quantity)

Which is correct, however, the link you've specified doesn't add any products or variants to the params. 但是,这是正确的,您指定的链接不会向参数添加任何产品或变体。 Therefore, it tries to add nothing, succeeds in add nothing, and continues on. 因此,它试图添加任何内容,成功添加任何内容,并继续。

You should take a look at the code in Spree which updates the order: 您应该查看Spree中更新订单的代码:

https://github.com/spree/spree/blob/v2.0.4/frontend/app/views/spree/orders/edit.html.erb#L14 https://github.com/spree/spree/blob/v2.0.4/frontend/app/views/spree/orders/edit.html.erb#L14

or this code which adds new products to the cart: 或此代码将新产品添加到购物车:

https://github.com/spree/spree/blob/v2.0.4/frontend/app/views/spree/products/_cart_form.html.erb https://github.com/spree/spree/blob/v2.0.4/frontend/app/views/spree/products/_cart_form.html.erb

If you pry those open and see what's happening there, you should have a better idea of how to add products to the cart. 如果您撬开那些打开并看到那里发生的事情,您应该更好地了解如何将产品添加到购物车。


The other option would be to look at the Spree API and add a Line Item to your order using this call: 另一种选择是查看Spree API并使用此调用向您的订单添加订单项:

http://api.spreecommerce.com/v1/order/line_items/#creating-a-line-item http://api.spreecommerce.com/v1/order/line_items/#creating-a-line-item

Thanks for the pointers @gmacdougall. 感谢指针@gmacdougall。 Just wanted to add how I solved it: 只想添加我如何解决它:

1) Duplicate the partial _cart_form.html.erb (find it in the frontend/app/views/spree/products folder and rename it something like _quick_cart_form.html.erb . This new file should still be placed in the same directory as _cart_form.html.erb . 1)复制部分_cart_form.html.erb (在frontend/app/views/spree/products文件夹中找到它并将其重命名为_quick_cart_form.html.erb 。这个新文件仍应放在与_cart_form.html.erb相同的目录中_cart_form.html.erb

2) Include this partial in your _products.html.erb partial (found in frontend/app/views/spree/shared - this partial loops through products and displays them) around line 35. The include code should look like <%= render :partial => 'spree/products/quick_cart_form', locals: { product: product } %> 2)在第_products.html.erb部分(在frontend/app/views/spree/shared - 这部分循环通过产品并显示它们)中包含此部分。包含代码应该看起来像<%= render :partial => 'spree/products/quick_cart_form', locals: { product: product } %>

3) Note that the local variable product is passed to the partial. 3)注意局部变量product被传递给partial。 This is important and so that the add to cart method is supplied with the product object. 这很重要,因此add to cart方法随产品对象一起提供。

4) Rename all instances of @product in your new _quick_cart_form.html.erb partial to just product (matching the supplied local variable), and hide/customise any divs you want in this new partial. 4)将新_quick_cart_form.html.erb@product所有实例重命名为product (仅匹配提供的局部变量),并隐藏/自定义此新部分中所需的任何div。

This should then allow you to add a product to cart straight from the product index page (eg a category page). 这应该允许您直接从产品索引页面(例如类别页面)将产品添加到购物车。

` `

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

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