I have a Spree-based program where customers are able to order products to be delivered on specified days. I am attempting to override the order creation logic to create multiple orders with the correct products.
The method that handles the form post looks like:
class OrderController < ApplicationController
include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Order
include Spree::Core::ControllerHelpers::Common
⋮
def process_order
params[:order].each do |index, order|
products = []
order[:product_id].each do |index, id|
products << Spree::Product.find(id) if id and not id.empty?
end
if products.any?
populator = Spree::OrderPopulator.new current_order(create_order_if_necessary: true), current_currency
current_order.update_attribute :delivery_date, Date.parse(order[:date])
products.each do |product|
variant = Spree::Variant.find_by product_id: product.id
populator.populate({ products: { product_id: product.id, variant_id: variant.id }, quantity: 1 })
end
end
end
redirect_to main_path
end
end
I pulled the code pretty much verbatim from the Spree source. An order is created and the order.products method works. When I view the order in the admin interface, however, there is a price, but no products. If I add a product through the admin interface, then it shows up with the count including the ones added programatically.
If I complete the order through the admin interface, all the products show up in the completed order.