Sha256: fad3985a17df59bc67c0c5ba803c34620803566b1cac16de79e82541a4adc4f9

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require 'shopify_api'

module Gemgento::Adapter::Shopify
  class Category

    # Import all Shopify collections.
    #
    # @return [void]
    def self.import
      page = 1
      ShopifyAPI::Base.site = Gemgento::Adapter::ShopifyAdapter.api_url
      shopify_collections = ShopifyAPI::CustomCollection.where(limit: 250, page: page)

      while shopify_collections.any?
        shopify_collections.each do |collection|
          sync_shopify_category(collection)
        end

        page = page + 1
        shopify_collections = ShopifyAPI::CustomCollection.where(limit: 250, page: page)
      end
    end

    # Create Gemgento::Category from Shopify collection.
    #
    # @param collection [ShopifyAPI::CustomCollection]
    # @return [Gemgento::Category]
    def self.sync_shopify_category(collection)
      if shopify_adapter = Gemgento::Adapter::ShopifyAdapter.find_by_shopify_model(collection)
        category = shopify_adapter.gemgento_model
      else
        category = Gemgento::Category.new
      end

      category.url_key = collection.handle
      category.name = collection.title
      category.parent = Gemgento::Category.root
      category.image = URI.parse(collection.image) if collection.has_attribute? :image
      category.is_active = collection.published_at ? true : false
      category.include_in_menu = false
      category.stores = Gemgento::Store.where.not(code: 'admin')
      category.sync_needed = true
      category.save

      Gemgento::Adapter::ShopifyAdapter.create_association(category, collection)

      return category
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gemgento-2.8.0 app/models/gemgento/adapter/shopify/category.rb