Class: Gemgento::API::SOAP::Catalog::ProductTag

Inherits:
Object
  • Object
show all
Defined in:
lib/gemgento/api/soap/catalog/product_tag.rb

Class Method Summary collapse

Class Method Details

.add(tags, product, store, user = nil) ⇒ Gemgento::MagentoResponse

Add tags to a product.

Parameters:

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Void

Associate a tag using a set of magento product ids.

Parameters:

  • tag (Tag)
  • source_product_ids (Hash, Array(Hash))

Returns:

  • (Void)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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_allVoid

Fetch all product tags from Magento.

Returns:

  • (Void)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Gemgento::MagentoResponse

Retrieve info on a specific tag.

Parameters:

Returns:



53
54
55
56
57
58
59
# 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) ⇒ Gemgento::MagentoResponse

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

Parameters:

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Gemgento::MagentoResponse

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

Parameters:

Returns:



129
130
131
132
133
134
135
136
137
138
139
140
# 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) ⇒ Object

Sync a Magento tag to Gemgento.

Parameters:

  • magento_tag_id (Integer)
  • source (Hash)
  • store (Store)


66
67
68
69
70
71
72
73
74
75
76
77
# 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