class Gemgento::API::SOAP::Catalog::ProductTag

Public Class Methods

add(tags, product, store, user = nil) click to toggle source

Add tags to a product.

@param tags [Array(Tag)] @param products [Product] @param store [Store] @param user [User] @return [Gemgento::MagentoResponse]

# File lib/gemgento/api/soap/catalog/product_tag.rb, line 107
def self.add(tags, product, store, user = nil)
  message = {
      tag: "'#{tags.map(&:name).join("', '")}'",
      product_id: product.magento_id,
      store: store.magento_id
  }
  message[:customer_id] = user.magento_id unless user.nil?

  response = MagentoApi.create_call(:catalog_product_tag_info, message)

  if response.success?
    response.body[:result][:item] = [response.body[:result][:item]] unless response.body[:result][:item].is_a?(Array)
  end

  return response
end
associate_products(tag, source_product_ids) click to toggle source

Associate a tag using a set of magento product ids.

@param tag [Tag] @param source_product_ids [Hash, Array(Hash)] @return [Void]

# File lib/gemgento/api/soap/catalog/product_tag.rb, line 84
def self.associate_products(tag, source_product_ids)
  product_ids = []
  source_product_ids = [source_product_ids] unless source_product_ids.is_a? Array

  source_product_ids.each do |product_key|
    Product.unscoped do
      if product = Product.not_deleted.find_by(magento_id: product_key[:key])
        tag.products << product unless tag.products.include? product
        product_ids << product.id
      end
    end
  end

  tag.products.where('gemgento_products.id NOT IN (?)', product_ids).destroy_all
end
fetch_all() click to toggle source

Fetch all product tags from Magento.

@return [Void]

# File lib/gemgento/api/soap/catalog/product_tag.rb, line 10
def self.fetch_all
  Product.not_deleted.where(magento_id: [391]).each do |product|
    product.stores.each do |store|
      list_response = list product, store

      if list_response.success?

        list_response.body[:result][:item].each do |tag|
          info_response = info tag[:tag_id], store

          if info_response.success?
            sync_magento_to_local tag[:tag_id], info_response.body[:result], store
          end
        end
      end
    end
  end
end
info(magento_tag_id, store) click to toggle source

Retrieve info on a specific tag.

@param magento_tag_id [Gemgento::Integer] @param store [Gemgento::Store] @return [Gemgento::MagentoResponse]

# File lib/gemgento/api/soap/catalog/product_tag.rb, line 53
def self.info(magento_tag_id, store)
  message = {
      tag_id: magento_tag_id,
      store: store.magento_id
  }
  MagentoApi.create_call(:catalog_product_tag_info, message)
end
list(product, store) click to toggle source

Retrieve a list of tags related to a product and store.

@param product [Product] @param store [Store] @return [Gemgento::MagentoResponse]

# File lib/gemgento/api/soap/catalog/product_tag.rb, line 34
def self.list(product, store)
  message = {
      product_id: product.magento_id,
      store: store.magento_id
  }
  response = MagentoApi.create_call(:catalog_product_tag_list, message)

  if response.success?
    response.body[:result][:item] = [response.body[:result][:item]] unless response.body[:result][:item].is_a? Array
  end

  return response
end
manage(tag, store) click to toggle source

Manage a tag, this will create/update a tag with absolute values.

@param tag [Tag] @param store [Store] @return [Gemgento::MagentoResponse]

# File lib/gemgento/api/soap/catalog/product_tag.rb, line 129
def self.manage(tag, store)
  message = {
      name: tag.name,
      status: tag.status,
      base_popularity: tag.base_popularity(store),
      product_ids: {item: tag.products.map(&:magento_id)},
      store: store.magento_id
  }
  message[:tag_id] = tag.magento_id unless tag.magento_id.nil?

  MagentoApi.create_call(:catalog_product_tag_manage, message)
end
sync_magento_to_local(magento_tag_id, source, store) click to toggle source

Sync a Magento tag to Gemgento.

@param magento_tag_id [Integer] @param source [Hash] @param store [Store]

# File lib/gemgento/api/soap/catalog/product_tag.rb, line 66
def self.sync_magento_to_local(magento_tag_id, source, store)
  tag = Tag.find_or_initialize_by(magento_id: magento_tag_id)
  tag.name = source[:name]
  tag.sync_needed = false
  tag.save

  store_tag = StoreTag.find_or_initialize_by(store: store, tag: tag)
  store_tag.base_popularity = source[:base_popularity]
  store_tag.save

  associate_products(tag, source[:products][:item]) if source[:products][:item]
end