简体   繁体   中英

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

  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

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:

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

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:

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

Thanks for the pointers @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 .

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 } %>

3) Note that the local variable product is passed to the partial. This is important and so that the add to cart method is supplied with the product object.

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.

This should then allow you to add a product to cart straight from the product index page (eg a category page).

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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