class FlexCommerce::PaypalExpress::ShippingMethodsForCart

@class ShippingMethodsForCart

This deals with free shipping promotions and updates the shipping methods accordingly

Attributes

cart[RW]
shipping_methods[RW]
shipping_promotions[RW]

Public Class Methods

new(cart:, shipping_methods:) click to toggle source
# File lib/paypal_express/shipping_methods_for_cart.rb, line 11
def initialize(cart:, shipping_methods:)
  self.cart = cart
  self.shipping_methods = shipping_methods
  self.shipping_promotions = cart.available_shipping_promotions
end

Public Instance Methods

call() click to toggle source
# File lib/paypal_express/shipping_methods_for_cart.rb, line 17
def call
  free_shipping_method_ids = [ ]
  shipping_promotions.reverse.each do |promotion|
    # See if promotion is having a shipping method,
    # and also see if that cart total is eligible for promotion
    if promotion.shipping_methods && can_apply_promotion_to_cart?(promotion: promotion)
      free_shipping_method_ids << promotion.shipping_methods.map(&:id)
    end
  end

  free_shipping_method_ids.flatten!
  updated_shipping_methods = []
  shipping_methods.each do |shipping_method|
    shipping_method_free = free_shipping_method_ids.include?(shipping_method.id)
    updated_shipping_methods << CartShippingMethod.new(shipping_method, shipping_method_free)
  end
  updated_shipping_methods
end

Private Instance Methods

can_apply_promotion_to_cart?(promotion:) click to toggle source
# File lib/paypal_express/shipping_methods_for_cart.rb, line 40
def can_apply_promotion_to_cart?(promotion:)
  (cart.sub_total - cart.total_discount) >= promotion.minimum_cart_total&.to_i
end