Sha256: 4c6d849d1b83ff24b58507e4cea216ed0f2955a7d06413afd8ffde19a80e2c3f
Contents?: true
Size: 1.58 KB
Versions: 20
Compression:
Stored size: 1.58 KB
Contents
module Spree class Promotion module Actions class FreeShipping < Spree::PromotionAction def perform(payload = {}) order = payload[:order] promotion_code = payload[:promotion_code] results = order.shipments.map do |shipment| return false if promotion_credit_exists?(shipment) shipment.adjustments.create!( order: shipment.order, amount: compute_amount(shipment), source: self, promotion_code: promotion_code, label: label ) true end # Did we actually end up applying any adjustments? # If so, then this action should be classed as 'successful' results.any? { |r| r == true } end def label "#{Spree.t(:promotion)} (#{promotion.name})" end def compute_amount(shipment) shipment.cost * -1 end # Removes any adjustments generated by this action from the order's # shipments. # @param order [Spree::Order] the order to remove the action from. # @return [void] def remove_from(order) order.shipments.each do |shipment| shipment.adjustments.each do |adjustment| if adjustment.source == self shipment.adjustments.destroy(adjustment) end end end end private def promotion_credit_exists?(shipment) shipment.adjustments.where(source: self).exists? end end end end end
Version data entries
20 entries across 20 versions & 1 rubygems