0
votes

I am running a spree app.

I am getting below error when I try to add any product in the cart.

undefined method `+' for nil:NilClass

This error comes only when I add option types and variants of the same product.

I am not sure what's exactly going wrong here, because I am not doing any changes in the code or something.

This is the extracted source it shows.

   if quantity.between?(1, 2_147_483_647)
    begin
      order.contents.add(variant, quantity, options)
    rescue ActiveRecord::RecordInvalid => e
      error = e.record.errors.full_messages.join(", ")
    end

Here's my order controller's populate function.

# Adds a new item to the order (creating a new order if none already exists)
def populate
  order    = current_order(create_order_if_necessary: true)
  variant  = Spree::Variant.find(params[:variant_id])
  quantity = params[:quantity].to_i
  options  = params[:options] || {}

  # 2,147,483,647 is crazy. See issue #2695.
  if quantity.between?(1, 2_147_483_647)
    begin
      order.contents.add(variant, quantity, options)
    rescue ActiveRecord::RecordInvalid => e
      error = e.record.errors.full_messages.join(", ")
    end
  else
    error = Spree.t(:please_enter_reasonable_quantity)
  end

  if error
    flash[:error] = error
    redirect_back_or_default(spree.root_path)
  else
    respond_with(order) do |format|
      format.html { redirect_to cart_path }
    end
  end
end

Please help me out here.

2
Could you please post the stacktrace? Especially, where does this error happen and the code with line number where it happens? - born4new
Are you sure variant, quantity, options are all non-nil values? - Anthony E
@AnthonyE I added a product, then options as select size and then I added two variants for it as 500 gms and 750gms and I increased their quantity in the stock. So possibly they are non-nil values - Suraj
@born4new app/controllers/spree/orders_controller.rb:49:in populate' ` this - Suraj
Helps us to help you: please post the add method of the Content model, specify which line is the line 49, and show us some code so that we can understand what's going on! - born4new

2 Answers

1
votes

You need to ensure the values of variant, quantity and options before sending them to spree.

The fact that you get this error could be considered as a bug on their side, since you'd expect a nice error message saying "variant is nil" or the like.

To fix your problem though, I'd check that these values are valid ones before sending them to spree.

0
votes

For future views about this issue.

Check if the Variant cost_currency attribute is the same that is configured in Spree. You can check it in a rails console doing:

Spree::Config.get(:currency)

Sometimes it happens when spree is initialized with some currency by default and then the default currency is changed.