我有一个基于Spree的程序,客户可以订购在指定日期交付的产品。我试图覆盖订单创建逻辑以使用正确的产品创建多个订单。以编程方式创建的Spree订单商品不会在管理界面中显示
处理表单后的方法是这样的:
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
我从Spree source逐字拉到代码相当。订单已创建且order.products方法有效。但是,当我在管理界面中查看订单时,有价格,但没有产品。如果我通过管理界面添加产品,则会显示计数,包括以编程方式添加的计数。
如果我通过管理界面完成订单,则所有产品均以完成的订单显示。