Sha256: 10316b889a41c60ea143cc8b23c71ca03c654ca1012409289fef47f487dc1f5e

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module Workarea
  decorate Storefront::CartViewModel, with: 'ShippingMessage' do
    decorated do
      include ActionView::Helpers::NumberHelper
    end

    def free_shipping_discount
      discount ||= Workarea::Pricing::Discount.where(
        order_total_operator: :greater_than,
        amount: 0.to_m,
        message_active: true
      ).first

      return nil unless discount.present? && discount.active? && discount.discount_message_valid?

      discount ||= if shipping.present? && shipping.shipping_service.present?
                  shipping.price_adjustments.detect do |discount|
                    discount.shipping_service.id == shipping_service.id
                  end
                else
                  discount
                end
    end

    def amount_away_from_free_shipping
      discount ||= free_shipping_discount

      return nil unless discount.present?

      discount.order_total - subtotal_price
    end

    def free_shipping_message
      amount_delta ||= amount_away_from_free_shipping
      return nil unless amount_delta.present?

      if amount_delta > 0
        free_shipping_discount.message.gsub(/{amount}/, number_to_currency(amount_delta))
      else
        free_shipping_discount.message_applied
      end
    end

    private
      def shipping
        Workarea::Shipping.find_by_order(model.id)
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-shipping_message-1.2.1 app/view_models/workarea/storefront/cart_view_model.decorator
workarea-shipping_message-1.2.0 app/view_models/workarea/storefront/cart_view_model.decorator