Search code examples
ruby-on-railsspree

Ruby-On-Rails: Spree - Customer to add order notes


I am using Spree on Rails. My customers would like to add additional notes to their orders when finalizing their shopping carts. For example, they might want to add an internal reference number.

Is there a way to add an additional optional field that customers can enter, either associated with the entire order or with a line item within that order, that will appear in the order display and invoice?

Thank you


Solution

  • First add database column to spree_orders with new migration

    rake db:create migration AddIRNToSpreeOrders
    

    and in created migration file

    def change
      add_column :spree_orders, :irn, :string
    end
    

    Add validation if needed in app/models/spree/order_decorator.rb

    Spree::Order.class_eval do
      validates_length_of :irn, minimum: 2, maximum: 254, allow_blank: true
    end
    

    Permit your new field in controller's strong params app/controllers/spree/orders_controller_decorator.rb

    Spree::OrdersController.class_eval do
    
      private
    
      def order_params
        if params[:order]
          params[:order].permit(*permitted_order_attributes, :irn)
        end
      end
    end
    

    Add your new field in orders views by copy and override view file or by defacing https://guides.spreecommerce.org/developer/deface_overrides_tutorial.html