Pergunta

Those "Add to Cart" buttons all redirect to the cart page in addition to populating the order. This is fine in most cases, but there are a few buttons I would like to simply populate the order without redirecting the browser. For example, you would click the button, the number in your cart would increase, but you would stay on the same page. Is this possible? I've found where (I think) the redirect is happening. Maybe there's some sort of indicator I can pass with the button to trigger bypassing it?

From orders_controller_decorator

  def populate
Spree::Order.transaction do
  current_order(true).lock! # force-create and lock
  populator = Spree::OrderPopulator.new(current_order(true), current_currency)

  if populator.populate(params.slice(:products, :variants, :quantity, :product_test_types))
    current_order.ensure_updated_shipments

    respond_with(@order) do |format|
      Rails.logger.debug "** Successful populate"
      format.html { redirect_to cart_path }
    end
  else
    msg           = populator.errors.full_messages.join(" ")
    flash[:error] = msg
    Rails.logger.debug "** Unuccessful populate: [#{msg}]"
    redirect_to cart_path
  end
 end
end
Foi útil?

Solução

You have a couple of options:

  • What is done on my spree 1-3-stable site is that the add to cart button is calling an ajax method that populates the order, thus no redirect is necessary.

  • remove the redirect_to cart_path from you method and redirect back to the page you came from.

Edit: AJAX example in coffee

  data = $("#order_form").serialize();

  #add additional info if needed
  data= data + '&size=' + user_selected_size

  $.ajax
    type: 'POST'
    url:  $("#order_form").attr("action")
    data: data
    success: (result) ->
      #your success method for handeling what happens after the cart is populated
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top