solidus icon indicating copy to clipboard operation
solidus copied to clipboard

Order creation with line items appears to fail

Open tmtrademarked opened this issue 4 years ago • 1 comments

In our store, we're using a service to create orders via the OrderImporter. If we provide line items during the order creation, we get an ActiveRecord exception.

Solidus Version: 3.0.1

To Reproduce In a Rails console, you can produce this as follows. The variant ID obviously has to be a variant that exists in the store. (This was just one from my store that happened to produce this)

line_items_attributes = [{variant_id: 22225, quantity: 1}]
[
  {
    :variant_id => 22225,
      :quantity => 1
  }
]
[8] pry(main)> order_params = { store_id: Spree::Store.default.id, line_items_attributes: line_items_attributes }

{
               :store_id => 1,
  :line_items_attributes => [
    {
      :variant_id => 22225,
        :quantity => 1
    }
  ]
}
[9] pry(main)> Spree::Core::Importer::Order.import(nil, order_params)
/// LOTS OF AR NOISE, REDACTED FOR BREVITY
  TRANSACTION (0.9ms)  ROLLBACK
ActiveRecord::ActiveRecordError: cannot update a new record
from /Users/tom.wilson/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/activerecord-6.1.4.1/lib/active_record/persistence.rb:665:in `update_columns'

Current behavior The order creation fails with an ActiveRecord error that results in the entire transaction rolling back. It appears to be related to creating new shipments which are not persisted during the Order::Import process before they are modified.

Expected behavior The order creation should succeed, even with initial items provided.

Additional context I've worked around this locally by adding a decorator for Spree::Order and overriding create_proposed_shipments as follows:

def create_proposed_shipments
  super.tap { save! }
end

tmtrademarked avatar Sep 08 '21 19:09 tmtrademarked

Hey @tmtrademarked! Thanks for reaching out. The root cause of this issue was discovered post 3.0 launch and resolved in 3.0.2 with #4098. @waiting-for-dev left a great commit comment explaining what happened and why if you are interested! Since you are running 3.0.1, it should be a straightforward upgrade without any issues. To be sure though, please see the change log for 3.0.2 to make sure the other minor changes will not affect your app!

cpfergus1 avatar Sep 13 '21 23:09 cpfergus1

Closing, as it looks it's been fixed on 3.0 and we're no longer supporting 2.11

waiting-for-dev avatar Sep 06 '22 08:09 waiting-for-dev