【问题标题】:spree cart is empty after page refresh页面刷新后,购物车为空
【发布时间】:2016-03-13 06:24:06
【问题描述】:

我可以一次添加到购物车,但是刷新页面后,购物车是空的。我正在使用 Spree 2.4。

点击“添加到购物车”后,我在订单控制器#edit 中检查@order 的值:

[1] pry(#<Spree::OrdersController>)> @order
=> #<Spree::Order id: 5, number: "R017142307", item_total: #<BigDecimal:89a8e10,'0.5E1',9(27)>, total: #<BigDecimal:89a8d20,'0.5E1',9(27)>, state: "cart", adjustment_total: #<BigDecimal:89a8bb8,'0.0',9(27)>, user_id: 1, completed_at: nil, bill_address_id: nil, ship_address_id: nil, payment_total: #<BigDecimal:89a8898,'0.0',9(27)>, shipping_method_id: nil, shipment_state: nil, payment_state: nil, email: "spree@example.com", special_instructions: nil, created_at: "2016-03-13 05:41:55", updated_at: "2016-03-13 06:18:01", currency: "USD", last_ip_address: "192.168.1.108", created_by_id: 1, shipment_total: #<BigDecimal:897bc80,'0.0',9(27)>, additional_tax_total: #<BigDecimal:897bb90,'0.0',9(27)>, promo_total: #<BigDecimal:897baa0,'0.0',9(27)>, channel: "spree", included_tax_total: #<BigDecimal:897b938,'0.0',9(27)>, item_count: 1, approver_id: nil, approved_at: nil, confirmation_delivered: false, considered_risky: false, guest_token: "kRau34G-dOY0Sh06Mk0_4g", canceled_at: nil, canceler_id: nil, store_id: 1, state_lock_version: 0>

还有line_items:

[3] pry(#<Spree::OrdersController>)> @order.line_items
=> [#<Spree::LineItem id: 21, variant_id: 7, order_id: 5, quantity: 1, price: #<BigDecimal:88e8480,'0.5E1',9(27)>, created_at: "2016-03-13 06:18:01", updated_at: "2016-03-13 06:18:01", currency: "USD", cost_price: #<BigDecimal:88e78a0,'0.17E2',9(27)>, tax_category_id: 1, adjustment_total: #<BigDecimal:88e7738,'0.0',9(27)>, additional_tax_total: #<BigDecimal:88e7648,'0.0',9(27)>, promo_total: #<BigDecimal:88e7558,'0.0',9(27)>, included_tax_total: #<BigDecimal:88e7468,'0.0',9(27)>, pre_tax_amount: #<BigDecimal:88e7378,'0.0',9(27)>>]

视图加载并且购物车看起来正常。但是,我刷新后,它说购物车是空的..

@order 还是一样,但是 line_items 消失了:

[1] pry(#<Spree::OrdersController>)> @order
=> #<Spree::Order id: 5, number: "R017142307", item_total: #<BigDecimal:d3bfeb8,'0.5E1',9(27)>, total: #<BigDecimal:d3bfdc8,'0.5E1',9(27)>, state: "cart", adjustment_total: #<BigDecimal:d3bfc60,'0.0',9(27)>, user_id: 1, completed_at: nil, bill_address_id: nil, ship_address_id: nil, payment_total: #<BigDecimal:d3bf968,'0.0',9(27)>, shipping_method_id: nil, shipment_state: nil, payment_state: nil, email: "spree@example.com", special_instructions: nil, created_at: "2016-03-13 05:41:55", updated_at: "2016-03-13 06:18:01", currency: "USD", last_ip_address: "192.168.1.108", created_by_id: 1, shipment_total: #<BigDecimal:d3beb80,'0.0',9(27)>, additional_tax_total: #<BigDecimal:d3bea90,'0.0',9(27)>, promo_total: #<BigDecimal:d3be9a0,'0.0',9(27)>, channel: "spree", included_tax_total: #<BigDecimal:d3be838,'0.0',9(27)>, item_count: 1, approver_id: nil, approved_at: nil, confirmation_delivered: false, considered_risky: false, guest_token: "kRau34G-dOY0Sh06Mk0_4g", canceled_at: nil, canceler_id: nil, store_id: 1, state_lock_version: 0>

[2] pry(#<Spree::OrdersController>)> @order.line_items
=> []

我无法追踪 line_items 的填充位置。有人可以帮忙吗?谢谢。

编辑:

我在 GET "/cart" 中 POST 到 /orders/populate/ 后的日志中看到了 DELETE:

Started GET "/cart" for 192.168.1.108 at 2016-03-13 19:01:17 -0400
Cannot render console from 192.168.1.108! Allowed networks: 192.168.2.0/192.168.2.255, 127.0.0.0/127.255.255.255, ::1
Processing by Spree::OrdersController#edit as HTML
  Spree::Preference Load (0.4ms)  SELECT  "spree_preferences".* FROM "spree_preferences"  WHERE "spree_preferences"."key" = 'spree/frontend_configuration/locale' LIMIT 1
  Spree::User Load (0.7ms)  SELECT  "spree_users".* FROM "spree_users"  WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = 1  ORDER BY "spree_users"."id" ASC LIMIT 1
  Spree::Store Load (0.5ms)  SELECT  "spree_stores".* FROM "spree_stores"  WHERE (url like '%192.168.1.108%')  ORDER BY "spree_stores"."id" ASC LIMIT 1
  Spree::Store Load (0.5ms)  SELECT  "spree_stores".* FROM "spree_stores"  WHERE "spree_stores"."default" = 't'  ORDER BY "spree_stores"."id" ASC LIMIT 1
  Spree::Order Load (1.5ms)  SELECT  "spree_orders".* FROM "spree_orders"  WHERE "spree_orders"."completed_at" IS NULL AND "spree_orders"."currency" = 'USD' AND "spree_orders"."guest_token" = 'kRau34G-dOY0Sh06Mk0_4g' AND "spree_orders"."store_id" = 1 AND "spree_orders"."user_id" = 1 LIMIT 1
  Spree::Adjustment Load (1.4ms)  SELECT "spree_adjustments".* FROM "spree_adjustments"  WHERE "spree_adjustments"."adjustable_type" = 'Spree::Order' AND "spree_adjustments"."adjustable_id" IN (5)  ORDER BY spree_adjustments.created_at ASC
  Spree::Order Load (0.2ms)  SELECT "spree_orders".* FROM "spree_orders"  WHERE "spree_orders"."user_id" = ? AND "spree_orders"."completed_at" IS NULL AND (id != 5)  [["user_id", 1]]
  Spree::Order Load (0.6ms)  SELECT  "spree_orders".* FROM "spree_orders"  WHERE "spree_orders"."number" IS NULL LIMIT 1
   (0.2ms)  SELECT COUNT(*) FROM "spree_roles" INNER JOIN "spree_roles_users" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles_users"."user_id" = ? AND "spree_roles"."name" = 'admin'  [["user_id", 1]]
  Spree::LineItem Load (0.2ms)  SELECT "spree_line_items".* FROM "spree_line_items"  WHERE "spree_line_items"."order_id" = ?  ORDER BY spree_line_items.created_at ASC  [["order_id", 5]]
  Spree::Variant Load (0.5ms)  SELECT  "spree_variants".* FROM "spree_variants"  WHERE "spree_variants"."id" = ? LIMIT 1  [["id", 30]]
  Spree::Product Load (0.3ms)  SELECT  "spree_products".* FROM "spree_products"  WHERE "spree_products"."id" = ? LIMIT 1  [["id", 7]]
  Spree::SaleEvent Load (0.2ms)  SELECT "spree_sale_events".* FROM "spree_sale_events"  WHERE ((start_date <= '2016-03-13 23:01:17.395935' AND end_date >= '2016-03-13 23:01:17.396008') OR is_permanent = 't') AND "spree_sale_events"."is_active" = 't'
   (0.1ms)  begin transaction
  Spree::Adjustment Load (0.4ms)  SELECT "spree_adjustments".* FROM "spree_adjustments"  WHERE "spree_adjustments"."adjustable_id" = ? AND "spree_adjustments"."adjustable_type" = ?  [["adjustable_id", 36], ["adjustable_type", "Spree::LineItem"]]
  Spree::InventoryUnit Load (0.2ms)  SELECT "spree_inventory_units".* FROM "spree_inventory_units"  WHERE "spree_inventory_units"."line_item_id" = ?  [["line_item_id", 36]]
  SQL (1.2ms)  DELETE FROM "spree_line_items" WHERE "spree_line_items"."id" = ?  [["id", 36]]
  SQL (0.8ms)  UPDATE "spree_orders" SET "updated_at" = '2016-03-13 23:01:17.410798' WHERE "spree_orders"."id" = 5
   (12.6ms)  commit transaction
   (0.2ms)  SELECT COUNT(*) FROM "spree_roles" INNER JOIN "spree_roles_users" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles_users"."user_id" = ? AND "spree_roles"."name" = 'admin'  [["user_id", 1]]
  Spree::User Load (1.0ms)  SELECT  "spree_users".* FROM "spree_users"  WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = ? LIMIT 1  [["id", 1]]
Deface: 1 overrides found for 'spree/orders/edit'
Deface: 'move_cart_buttons' matched 0 times with '[data-hook='form-table-foot']'
  Rendered /usr/local/rvm/gems/ruby-2.2.1@mystore20151008/bundler/gems/spree_bootstrap_frontend-d20c03352917/spree_bootstrap/frontend/app/views/spree/shared/_error_messages.html.erb (1.1ms)
  Spree::Image Load (0.7ms)  SELECT "spree_assets".* FROM "spree_assets"  WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_id" = ? AND "spree_assets"."viewable_type" = ?  ORDER BY "spree_assets"."position" ASC  [["viewable_id", 30], ["viewable_type", "Spree::Variant"]]
  Spree::Variant Load (0.9ms)  SELECT  "spree_variants".* FROM "spree_variants"  WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = ? AND "spree_variants"."is_master" = 't' LIMIT 1  [["product_id", 7]]
  Spree::Image Exists (0.5ms)  SELECT  1 AS one FROM "spree_assets"  WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_id" = ? AND "spree_assets"."viewable_type" = ? LIMIT 1  [["viewable_id", 7], ["viewable_type", "Spree::Variant"]]
  Spree::Image Load (0.4ms)  SELECT  "spree_assets".* FROM "spree_assets"  WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_id" = ? AND "spree_assets"."viewable_type" = ?  ORDER BY "spree_assets"."position" ASC LIMIT 1  [["viewable_id", 7], ["viewable_type", "Spree::Variant"]]
  Spree::OptionValue Load (1.2ms)  SELECT "spree_option_values".* FROM "spree_option_values" INNER JOIN "spree_option_values_variants" ON "spree_option_values"."id" = "spree_option_values_variants"."option_value_id" WHERE "spree_option_values_variants"."variant_id" = ?  [["variant_id", 30]]
  Spree::OptionType Load (0.3ms)  SELECT  "spree_option_types".* FROM "spree_option_types"  WHERE "spree_option_types"."id" = ?  ORDER BY spree_option_types.position LIMIT 1  [["id", 1]]
  Spree::OptionType Load (0.2ms)  SELECT  "spree_option_types".* FROM "spree_option_types"  WHERE "spree_option_types"."id" = ?  ORDER BY spree_option_types.position LIMIT 1  [["id", 2]]
   (0.3ms)  SELECT SUM("spree_stock_items"."count_on_hand") AS sum_id FROM "spree_stock_items" INNER JOIN "spree_stock_locations" ON "spree_stock_locations"."id" = "spree_stock_items"."stock_location_id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_stock_items"."variant_id" = 30 AND "spree_stock_locations"."active" = 't'
  Rendered spree/orders/_line_item.html.erb (305.0ms)
  Rendered spree/orders/_form.html.erb (335.1ms)
  Spree::PaymentMethod Load (1.2ms)  SELECT "spree_payment_methods".* FROM "spree_payment_methods"  WHERE "spree_payment_methods"."deleted_at" IS NULL
  CACHE (0.0ms)  SELECT "spree_payment_methods".* FROM "spree_payment_methods"  WHERE "spree_payment_methods"."deleted_at" IS NULL
  Rendered spree/orders/edit.html.erb within spree/layouts/spree_application (463.7ms)
  Rendered spree/shared/_head.html.erb (8017.9ms)
Deface: 1 overrides found for 'spree/shared/_header'
Deface: 'move_link_to_cart_mobile' matched 1 times with 'div.navbar-brand-mobile'
Deface: [WARNING] No :original defined for 'move_link_to_cart_mobile', you should change its definition to include:
 :original => '1fe9fe305a7e977a4311678e40b0de8ac4fe953b' 
Deface: 4 overrides found for 'spree/shared/_nav_bar'
Deface: 'auth_shared_login_bar' matched 1 times with 'li#search-bar'
Deface: [ERROR] The original source for 'auth_shared_login_bar' has changed, this override should be reviewed to ensure it's still valid.
Deface: 'remove_search_bar' matched 1 times with 'li#search-bar'
Deface: [WARNING] No :original defined for 'remove_search_bar', you should change its definition to include:
 :original => 'c00d67dfc0496d21a351b48c41a728412cb5ed2c' 
Deface: 'move_link_to_cart' matched 1 times with 'ul#nav-bar'
Deface: [WARNING] No :original defined for 'move_link_to_cart', you should change its definition to include:
 :original => 'd32d3ed0d028db429111568a65baba0eeac8f4e0' 
Deface: 'add_country_flag_after_cart_link' matched 1 times with 'li#link-to-cart'
Deface: [WARNING] No :original defined for 'add_country_flag_after_cart_link', you should change its definition to include:
 :original => 'e9241f273349c0f080cef375ce5d2c26546eb870' 
  Rendered spree/shared/_nav_bar.html.erb (4.2ms)
  Rendered spree/shared/_header.html.erb (110.2ms)
  Rendered spree/shared/_footer_cart.html.erb (1712.9ms)
  Spree::Tracker Load (0.2ms)  SELECT  "spree_trackers".* FROM "spree_trackers"  WHERE "spree_trackers"."active" = 't' AND "spree_trackers"."environment" = 'development'  ORDER BY "spree_trackers"."id" ASC LIMIT 1
  Rendered /usr/local/rvm/gems/ruby-2.2.1@mystore20151008/bundler/gems/spree-dfd548b02fb3/frontend/app/views/spree/shared/_google_analytics.html.erb (11.8ms)
Completed 200 OK in 10696ms (Views: 10571.2ms | ActiveRecord: 35.1ms)


Started GET "/assets/spree/frontend/all.css" for 192.168.1.108 at 2016-03-13 19:01:28 -0400
Cannot render console from 192.168.1.108! Allowed networks: 192.168.2.0/192.168.2.255, 127.0.0.0/127.255.255.255, ::1


Started GET "/assets/spree/frontend/all.js" for 192.168.1.108 at 2016-03-13 19:01:28 -0400
Cannot render console from 192.168.1.108! Allowed networks: 192.168.2.0/192.168.2.255, 127.0.0.0/127.255.255.255, ::1


Started GET "/cart_link" for 192.168.1.108 at 2016-03-13 19:01:28 -0400
Cannot render console from 192.168.1.108! Allowed networks: 192.168.2.0/192.168.2.255, 127.0.0.0/127.255.255.255, ::1
Processing by Spree::StoreController#cart_link as */*
  Spree::Preference Load (0.5ms)  SELECT  "spree_preferences".* FROM "spree_preferences"  WHERE "spree_preferences"."key" = 'spree/frontend_configuration/locale' LIMIT 1
  Spree::User Load (2.1ms)  SELECT  "spree_users".* FROM "spree_users"  WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = 1  ORDER BY "spree_users"."id" ASC LIMIT 1
   (0.0ms)  SELECT COUNT(*) FROM "spree_roles" INNER JOIN "spree_roles_users" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles_users"."user_id" = ? AND "spree_roles"."name" = 'admin'  [["user_id", 1]]
  Spree::Store Load (0.4ms)  SELECT  "spree_stores".* FROM "spree_stores"  WHERE (url like '%192.168.1.108%')  ORDER BY "spree_stores"."id" ASC LIMIT 1
  Spree::Store Load (0.8ms)  SELECT  "spree_stores".* FROM "spree_stores"  WHERE "spree_stores"."default" = 't'  ORDER BY "spree_stores"."id" ASC LIMIT 1
  Spree::Order Load (0.4ms)  SELECT  "spree_orders".* FROM "spree_orders"  WHERE "spree_orders"."completed_at" IS NULL AND "spree_orders"."currency" = 'USD' AND "spree_orders"."guest_token" = 'kRau34G-dOY0Sh06Mk0_4g' AND "spree_orders"."store_id" = 1 AND "spree_orders"."user_id" = 1 LIMIT 1
  Rendered /usr/local/rvm/gems/ruby-2.2.1@mystore20151008/bundler/gems/spree-dfd548b02fb3/frontend/app/views/spree/shared/_link_to_cart.html.erb (12.4ms)
Completed 200 OK in 969ms (Views: 939.1ms | ActiveRecord: 4.2ms)


Started GET "/assets/icons/delete.png" for 192.168.1.108 at 2016-03-13 19:01:29 -0400
Cannot render console from 192.168.1.108! Allowed networks: 192.168.2.0/192.168.2.255, 127.0.0.0/127.255.255.255, ::1

【问题讨论】:

  • 我认为这不是你的错。这是狂欢问题。页面刷新时购物车变空。
  • 奇怪。您是否看到添加到购物车时正在运行的 sql 查询以及稍后在刷新时运行?将产品添加到购物车后,您能否立即在管理员中查看订单内容?
  • @Jakub 我可以在添加到购物车后立即在管理员中看到订单。它处于“购物车”状态,但是当我转到订单“购物车”链接时,它说订单是空的。我可以看到很多 sql 查询。我应该发布吗?
  • 你能看到通过`INSERT INTO "spree_line_items" ("cost_price", "created_at", "currency", "order_id", "price", "quantity", "tax_category_id", " updated_at", "variant_id") 值 (?, ?, ?, ?, ?, ?, ?, ?, ?) [["cost_price", 17.0], ["created_at", "2016-03-13 22:02 :09.040804"], ["currency", "USD"], ["order_id", 5], ["price", 55.0], ["quantity", 1], ["tax_category_id", 1], ["updated_at ", "2016-03-13 22:02:09.040804"], ["variant_id", 30]]` 查询?您可以在数据库客户端中直接选择吗?只是想知道问题出在哪里。是 DB 相关还是 Spree。
  • @Jakub 我认为这不是数据库问题,因为在我刷新购物车页面后有一条 DELETE 语句。出于某种原因,它正在删除订单项。

标签: ruby-on-rails spree


【解决方案1】:

似乎您正在使用gem 'spree_active_sale',这是造成这种行为的罪魁祸首。 它有一个 before_filter 从订单中销毁不活动的产品线项目。 见https://github.com/suryart/spree_active_sale/blob/master/app/controllers/spree/orders_controller_decorator.rb#L3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-04
    • 2014-08-02
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多