Created
May 24, 2016 07:35
-
-
Save 1990prashant/d944afd9d9970c11e5e2b0eaada877a3 to your computer and use it in GitHub Desktop.
Add new fields in order's line items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is the last file where you need to change | |
module Spree | |
OrderContents.class_eval do | |
def initialize(order) | |
@order = order | |
end | |
def add(variant, quantity = 1, options = {}, attributes_name) | |
timestamp = Time.current | |
line_item = add_to_line_item(variant, quantity, options, attributes_name) | |
options[:line_item_created] = true if timestamp <= line_item.created_at | |
after_add_or_remove(line_item, options) | |
end | |
def remove(variant, quantity = 1, options = {}, attributes_name) | |
line_item = remove_from_line_item(variant, quantity, options, attributes_name) | |
after_add_or_remove(line_item, options) | |
end | |
private | |
def add_to_line_item(variant, quantity, options = {}, attributes_name) | |
line_item = grab_line_item_by_variant(variant, false, options) | |
if line_item | |
line_item.quantity += quantity.to_i | |
line_item.currency = currency unless currency.nil? | |
else | |
opts = { currency: order.currency }.merge ActionController::Parameters.new(options). | |
permit(PermittedAttributes.line_item_attributes) | |
line_item = order.line_items.new(quantity: quantity, | |
attributes_name: attributes_name, | |
variant: variant, | |
options: opts) | |
end | |
line_item.target_shipment = options[:shipment] if options.has_key? :shipment | |
line_item.save! | |
line_item | |
end | |
def remove_from_line_item(variant, quantity, options = {}, attributes_name) | |
line_item = grab_line_item_by_variant(variant, true, options) | |
line_item.quantity -= quantity | |
line_item.target_shipment= options[:shipment] | |
if line_item.quantity.zero? | |
order.line_items.destroy(line_item) | |
else | |
line_item.save! | |
end | |
line_item | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#After adding permitted attributes you need to add that value into your orders controller | |
def populate | |
order = current_order(create_order_if_necessary: true) | |
variant = Spree::Variant.find(params[:variant_id]) | |
quantity = params[:quantity].to_i | |
options = params[:options] || {} | |
attributes_name = params[:attributes_name] | |
if quantity.between?(1, 2_147_483_647) | |
begin | |
order.contents.add(variant, quantity, options, attributes_name) | |
rescue ActiveRecord::RecordInvalid => e | |
error = e.record.errors.full_messages.join(", ") | |
end | |
else | |
error = Spree.t(:please_enter_reasonable_quantity) | |
end | |
if error | |
flash[:error] = error | |
redirect_back_or_default(spree.root_path) | |
else | |
respond_with(order) do |format| | |
format.html { redirect_to cart_path } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#After adding a field in database you need to permit this attribute to do that you need to add the following line in your | |
#config/initializer/spree.rb file | |
Spree::PermittedAttributes.line_item_attributes.push :attributes_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment