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.4.27 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.4 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.26 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.3 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.25 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.2 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.24 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.1 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.23 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.22 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.0 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.21 app/services/workarea/cart_cleaner.rb
workarea-core-3.5.0.beta.1 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.20 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.19 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.18 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.17 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.16 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.15 app/services/workarea/cart_cleaner.rb
workarea-core-3.4.14 app/services/workarea/cart_cleaner.rb