Sha256: fe1c7b2b0af18c1ed8bc17c81e99dccea2973561889ec98e171fbbdc8402f091

Contents?: true

Size: 1.29 KB

Versions: 30

Compression:

Stored size: 1.29 KB

Contents

module Spree
  class ShipmentHandler
    class << self
      def factory(shipment)
        # Do we have a specialized shipping-method-specific handler? e.g:
        # Given shipment.shipping_method = Spree::ShippingMethod::DigitalDownload
        # do we have Spree::ShipmentHandler::DigitalDownload?
        if sm_handler = "Spree::ShipmentHandler::#{shipment.shipping_method.name.split('::').last}".safe_constantize
          sm_handler.new(shipment)
        else
          new(shipment)
        end
      end
    end

    def initialize(shipment)
      @shipment = shipment
    end

    def perform
      @shipment.inventory_units.each &:ship!
      @shipment.process_order_payments if Spree::Config[:auto_capture_on_dispatch]
      @shipment.touch :shipped_at
      update_order_shipment_state
      send_shipped_email
    end

    protected

    def send_shipped_email
      # you can overwrite this method in your application / extension to send out the confirmation email
      # or use `spree_emails` gem
      # YourEmailVendor.deliver_shipment_notification_email(@shipment.id)
    end

    def update_order_shipment_state
      order = @shipment.order

      new_state = OrderUpdater.new(order).update_shipment_state
      order.update_columns(shipment_state: new_state, updated_at: Time.current)
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
spree_core-4.8.3 app/models/spree/shipment_handler.rb
spree_core-4.8.2 app/models/spree/shipment_handler.rb
spree_core-4.7.3 app/models/spree/shipment_handler.rb
spree_core-4.6.6 app/models/spree/shipment_handler.rb
spree_core-4.5.5 app/models/spree/shipment_handler.rb
spree_core-4.3.3 app/models/spree/shipment_handler.rb
spree_core-4.4.1 app/models/spree/shipment_handler.rb
spree_core-4.5.4 app/models/spree/shipment_handler.rb
spree_core-4.6.5 app/models/spree/shipment_handler.rb
spree_core-4.7.2 app/models/spree/shipment_handler.rb
spree_core-4.7.1 app/models/spree/shipment_handler.rb
spree_core-4.6.4 app/models/spree/shipment_handler.rb
spree_core-4.7.0 app/models/spree/shipment_handler.rb
spree_core-4.6.3 app/models/spree/shipment_handler.rb
spree_core-4.6.2 app/models/spree/shipment_handler.rb
spree_core-4.6.1 app/models/spree/shipment_handler.rb
spree_core-4.6.0 app/models/spree/shipment_handler.rb
spree_core-4.5.3 app/models/spree/shipment_handler.rb
spree_core-4.5.2 app/models/spree/shipment_handler.rb
spree_core-4.5.1 app/models/spree/shipment_handler.rb