Sha256: 4cfed597694b85e2f4e456fe675eed2c24e2bfdf13e4904c652020c315592c2e

Contents?: true

Size: 1.41 KB

Versions: 62

Compression:

Stored size: 1.41 KB

Contents

module Workarea
  class CartCleaner
    attr_reader :cart

    def initialize(cart)
      @cart = cart
    end

    def messages
      @messages ||= []
    end

    def message?
      messages.present?
    end

    def clean
      items_to_remove = []

      cart.items.each do |item|
        product = products.detect { |p| p.id == item.product_id }
        variant = product && product.variants.detect { |v| v.sku == item.sku }
        price = pricing.for_sku(item.sku, quantity: item.quantity)

        if product.blank?
          items_to_remove << item.id
          self.messages << I18n.t('workarea.carts.product_unavailable', product_id: item.product_id)
          next
        end

        unless product.purchasable? && variant.try(:active?)
          items_to_remove << item.id
          self.messages << I18n.t('workarea.carts.product_unavailable', product_id: item.product_id)
          next
        end

        unless price.persisted?
          items_to_remove << item.id
          self.messages << I18n.t('workarea.carts.product_unavailable', product_id: item.product_id)
          next
        end
      end

      items_to_remove.each do |item_id|
        cart.remove_item(item_id)
      end
    end

    private

    def products
      @products ||= Catalog::Product.any_in(id: cart.items.map(&:product_id)).to_a
    end

    def pricing
      @pricing ||= Pricing::Collection.new(cart.items.map(&:sku))
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.26 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.45 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.25 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.23 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.44 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.22 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.43 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.21 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.42 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.20 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.41 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.19 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.40 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.18 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.39 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.17 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.38 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.16 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.37 app/services/workarea/cart_cleaner.rb