Sha256: e228e035f4dc557abab6907fb70da2e7440e7abf4076ee1e2f402db5ecbc8156

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true
module ShopifyAPI
  class Product < Base
    include Events
    include Metafields

    early_july_pagination_release!

    def initialize(*)
      super
      self.attributes.except!('total_inventory') unless allow_inventory_params?
    end

    # compute the price range
    def price_range
      prices = variants.collect(&:price).collect(&:to_f)
      format = "%0.2f"
      if prices.min != prices.max
        "#{format % prices.min} - #{format % prices.max}"
      else
        format % prices.min
      end
    end

    def total_inventory=(new_value)
      raise_deprecated_inventory_call('total_inventory') unless allow_inventory_params?
      super
    end

    def collections
      CustomCollection.find(:all, params: { product_id: self.id })
    end

    def smart_collections
      SmartCollection.find(:all, params: { product_id: self.id })
    end

    def add_to_collection(collection)
      collection.add_product(self)
    end

    def remove_from_collection(collection)
      collection.remove_product(self)
    end

    private

    def raise_deprecated_inventory_call(parameter)
      raise(
        ShopifyAPI::ValidationException,
        "'#{parameter}' is deprecated - see https://help.shopify.com/en/api/guides/inventory-migration-guide",
      )
    end

    def allow_inventory_params?
      Base.api_version < ApiVersion.find_version('2019-10')
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
shopify_api-9.5.2 lib/shopify_api/resources/product.rb
ruby_shopify_api-1.2.0 lib/ruby_shopify_api/resources/product.rb
ruby_shopify_api-1.1.0 lib/shopify_api/resources/product.rb
ruby_shopify_api-1.0.0 lib/shopify_api/resources/product.rb
shopify_api-9.5.1 lib/shopify_api/resources/product.rb
shopify_api-9.5 lib/shopify_api/resources/product.rb
shopify_api-9.4.1 lib/shopify_api/resources/product.rb
shopify_api-9.4.0 lib/shopify_api/resources/product.rb
shopify_api-9.3.0 lib/shopify_api/resources/product.rb