【问题标题】:Adding multiple line items on a single submit, Spree 3, RoR 5在一次提交中添加多个行项目,Spree 3,RoR 5
【发布时间】:2017-08-08 01:32:17
【问题描述】:

上下文信息:嗨,我有一个使用 Spree 3.2.0.rc1 gem 的 rails 5.0.3 应用程序,每个功能都运行良好,我只修改了一些视图。

目标:我的商店总共销售的款式少于 12 种,因此我希望有一种更快的方式让我的客户将商品添加到购物车中。

问题:如何修改购物车,以便我可以使用不同的提交按钮将多个订单项添加到购物车中

谢谢!

【问题讨论】:

    标签: ruby-on-rails spree


    【解决方案1】:

    为了实现您的目标,您可以在 checkout controller 中创建一个单独的操作,例如 checkout 并在那里定义您的逻辑。

    它看起来像下面给出的代码 sn-p

    def checkout
      order = current_order(create_order_if_necessary: true)
      errors = []
      Variant.all.each do |variant|
        begin
          order.contents.add(variant)
        rescue ActiveRecord::RecordInvalid => e
          errors << e.record.errors.full_messages.join(", ")
        end
      end
      if errors.present?
        flash[:error] = errors
        redirect_back_or_default(spree.root_path)
      else
        flash[:success] = 'All Products added'
        respond_with(order) do |format|
          format.html { redirect_to cart_path }
        end
      end
    end
    

    您仍然可以通过移动来重构此代码

      Variant.all.each do |variant|
        begin
          order.contents.add(variant)
        rescue ActiveRecord::RecordInvalid => e
          errors << e.record.errors.full_messages.join(", ")
        end
      end
    

    这个逻辑作为variant model中的类方法。

    如果您仍然遇到任何问题,请告诉我

    【讨论】:

    • 谢谢,这不是我的想法,但我相信这对未来会有帮助!我最终做的是修改单例页面,以便将其显示为每个产品的一部分,这样我就可以直接从主页添加任何产品的任何变体。
    猜你喜欢
    • 1970-01-01
    • 2015-09-18
    • 2019-05-11
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多