我尝试在订单标签下的Spree后端创建一个搜索栏,您可以在其中输入产品sku并查找包含该产品的所有订单。
我使用了deface来插入"搜索sku"领域。代码如下所示:
Deface::Override.new(:virtual_path => 'spree/admin/orders/index',
:name => 'search_by_sku',
:insert_bottom => "div.omega",
:text => "
<div class='field'>
<%= label_tag nil, Spree.t(:sku) %>
<%=f.text_field :line_items_cont %>
</div>
")
line_items_cont给了我一个错误。
line_items_cont是ransack用来搜索line_items的东西,它应该返回到结果中,但它不会。这是结果返回的结果。
#<Spree::Order id: 225400, number: "R454575501",
item_total: #<BigDecimal:7f83f7815998,'0.3194E3',18(18)>,
total: #<BigDecimal:7f83f78158f8,'0.3194E3',18(18)>,
state: "complete", adjustment_total: #<BigDecimal:7f83f7815808,'0.0',9(18)>,
user_id: nil, completed_at: "2014-01-30 07:34:38", bill_address_id: 173224,
ship_address_id: 171925, payment_total: #<BigDecimal:7f83f7815560,'0.3194E3',18(18)>,
shipping_method_id: nil, shipment_state: "shipped", payment_state: "paid",
email: "test@email.com", special_instructions: "", created_at: "2014-01-30 05:00:49",
updated_at: "2014-01-30 16:12:31", currency: "USD", last_ip_address: "removed",
yahoo_order_id: nil, legacy_fields: {}, legacy_order_id: nil, created_by_id: nil,
channel: "spree", approved_at: "2014-01-30 14:27:10">],
所以我想知道的是有没有办法将订单中包含的项目添加到返回的结果中?
答案 0 :(得分:3)
订单与SKU之间的关系如下:
所以,让我们看一下搜索方法。
首先我可以通过在order_id上使用简单搜索找到订单:
sandbox » Spree::Order.search(id_eq: 1).result.first.id
(0.1ms) SELECT COUNT(*) FROM "spree_orders" WHERE "spree_orders"."id" = 1
=> 1
太棒了。现在我需要通过line_item而不是订单来查找内容:
sandbox » Spree::Order.search(line_items_id_eq: 1).result.first.id
Spree::Order Load (0.5ms) SELECT "spree_orders".* FROM "spree_orders" LEFT OUTER JOIN "spree_line_items" ON "spree_line_items"."order_id" = "spree_orders"."id" WHERE "spree_line_items"."id" = 1 ORDER BY "spree_orders"."id" ASC LIMIT 1
=> 1
现在我想通过variant_id找到一些东西:
sandbox » Spree::Order.search(line_items_variant_id_eq: 1).result.first.id
Spree::Order Load (0.5ms) SELECT "spree_orders".* FROM "spree_orders" LEFT OUTER JOIN "spree_line_items" ON "spree_line_items"."order_id" = "spree_orders"."id" WHERE "spree_line_items"."variant_id" = 1 ORDER BY "spree_orders"."id" ASC LIMIT 1
=> 1
但不是通过variant_id搜索,而是想通过SKU搜索:
Spree::Order.search(line_items_variant_sku_eq: "ROR-00011").result.first.id
Spree::Order Load (0.5ms) SELECT "spree_orders".* FROM "spree_orders" LEFT OUTER JOIN "spree_line_items" ON "spree_line_items"."order_id" = "spree_orders"."id" LEFT OUTER JOIN "spree_variants" ON "spree_variants"."id" = "spree_line_items"."variant_id" AND "spree_variants"."deleted_at" IS NULL WHERE "spree_variants"."sku" = 'ROR-00011' ORDER BY "spree_orders"."id" ASC LIMIT 1
=> 1
所以,你可以使用:
<%= f.text_field :line_items_variant_sku_eq %>
得到你想要的东西。