Sha256: 79600915e1b84ae76c468baff59f3d294581b5f73636501c982ab11a7f8ed22c
Contents?: true
Size: 1.84 KB
Versions: 5
Compression:
Stored size: 1.84 KB
Contents
module Spree class OrderPopulator attr_accessor :order, :currency attr_reader :errors def initialize(order, currency) @order = order @currency = currency @errors = ActiveModel::Errors.new(self) end # # Parameters can be passed using the following possible parameter configurations: # # * Single variant/quantity pairing # +:variants => { variant_id => quantity }+ # # * Multiple products at once # +:products => { product_id => variant_id, product_id => variant_id }, :quantity => quantity+ def populate(from_hash) from_hash[:products].each do |product_id,variant_id| attempt_cart_add(variant_id, from_hash[:quantity]) end if from_hash[:products] from_hash[:variants].each do |variant_id, quantity| attempt_cart_add(variant_id, quantity) end if from_hash[:variants] valid? end def valid? errors.empty? end private def attempt_cart_add(variant_id, quantity) quantity = quantity.to_i # 2,147,483,647 is crazy. # See issue #2695. if quantity > 2_147_483_647 errors.add(:base, Spree.t(:please_enter_reasonable_quantity, :scope => :order_populator)) return false end variant = Spree::Variant.find(variant_id) if quantity > 0 if check_stock_levels(variant, quantity) @order.contents.add(variant, quantity, currency) end end end def check_stock_levels(variant, quantity) display_name = %Q{#{variant.name}} display_name += %Q{ (#{variant.options_text})} unless variant.options_text.blank? if Stock::Quantifier.new(variant).can_supply? quantity true else errors.add(:base, Spree.t(:out_of_stock, :scope => :order_populator, :item => display_name.inspect)) return false end end end end
Version data entries
5 entries across 5 versions & 1 rubygems