Class: Gemgento::API::SOAP::Catalog::Category

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

Class Method Summary collapse

Class Method Details

.assigned_products(category, store) ⇒ Gemgento::MagentoResponse

Get Products assigned to a Category in Magento.

Parameters:

Returns:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gemgento/api/soap/catalog/category.rb', line 114

def self.assigned_products(category, store)
  message = {
      category_id: category.magento_id,
      store_id: store.magento_id
  }
  response = MagentoApi.create_call(:catalog_category_assigned_products, message)

  if response.success?
    if response.body[:result][:item].nil?
      response.body[:result][:item] = []
    elsif !response.body[:result][:item].is_a? Array
      response.body[:result][:item] = [response.body[:result][:item]]
    end
  end

  return response
end

.create(category, store) ⇒ Gemgento::MagentoResponse

Create Category in Magento.

Parameters:

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gemgento/api/soap/catalog/category.rb', line 48

def self.create(category, store)
  data = {
      name: category.name,
      'is_active' => category.is_active ? 1 : 0,
      'include_in_menu' => category.include_in_menu ? 1 : 0,
      'available_sort_by' => {'arr:string' => %w[name]},
      'default_sort_by' => 'name',
      'url_key' => category.url_key,
      'position' => category.position,
      'is_anchor' => 1
  }
  message = {
      parentId: category.parent.magento_id,
      category_data: data,
      store_view: store.magento_id
  }
  MagentoApi.create_call(:catalog_category_create, message)
end

.fetch_allVoid

Pull all Magento Category into Gemgento.

Returns:

  • (Void)


10
11
12
13
14
15
16
17
# File 'lib/gemgento/api/soap/catalog/category.rb', line 10

def self.fetch_all
  Store.all.each do |store|
    response = tree(store)
    if response.success?
      sync_magento_tree_to_local(response.body[:tree], store) unless response.body[:tree].nil?
    end
  end
end

.info(magento_id, store) ⇒ Gemgento::MagentoResponse

Get Category info from Magento.

Parameters:

Returns:



35
36
37
38
39
40
41
# File 'lib/gemgento/api/soap/catalog/category.rb', line 35

def self.info(magento_id, store)
  message = {
      category_id: magento_id,
      store_view: store.magento_id
  }
  MagentoApi.create_call(:catalog_category_info, message)
end

.set_product_categoriesVoid

Update all Category Products based on Magento data.

Returns:

  • (Void)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gemgento/api/soap/catalog/category.rb', line 135

def self.set_product_categories
  ::Gemgento::Category.all.each do |category|

    category.stores.each do |store|
      response = assigned_products(category, store)
      next unless response.success?

      items = response.body[:result][:item]

      if items.nil? || items == false || items.empty?
        ProductCategory.unscoped.where(category: category, store: store).destroy_all
        next
      end

      product_category_ids = []
      items.each do |item|
        product = ::Gemgento::Product.find_by(magento_id: item[:product_id])
        next if product.nil?

        pairing = ProductCategory.unscoped.find_or_initialize_by(category: category, product: product, store: store)
        pairing.position = item[:position].nil? ? 1 : item[:position][0]
        pairing.store = store
        pairing.save

        product_category_ids << pairing.id
      end

      ProductCategory.unscoped.
          where('store_id = ? AND category_id = ? AND id NOT IN (?)', store.id, category.id, product_category_ids).
          destroy_all
    end
  end
end

.tree(store) ⇒ Gemgento::MagentoResponse

Get the Category tree from Magento.

Parameters:

Returns:



23
24
25
26
27
28
# File 'lib/gemgento/api/soap/catalog/category.rb', line 23

def self.tree(store)
  message = {
      store_view: store.magento_id
  }
  MagentoApi.create_call(:catalog_category_tree, message)
end

.update(category, store) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gemgento/api/soap/catalog/category.rb', line 67

def self.update(category, store)
  data = {
      name: category.name,
      'is_active' => category.is_active ? 1 : 0,
      'include_in_menu' => category.include_in_menu ? 1 : 0,
      'url_key' => category.url_key,
      'position' => category.position
  }
  message = {
      category_id: category.magento_id,
      category_data: data,
      store_view: store.magento_id
  }
  MagentoApi.create_call(:catalog_category_update, message)
end

.update_product(product_category) ⇒ Gemgento::MagentoResponse

Update ProductCategory info in Magento.

Parameters:

Returns:



173
174
175
176
177
178
179
180
181
# File 'lib/gemgento/api/soap/catalog/category.rb', line 173

def self.update_product(product_category)
  message = {
      category_id: product_category.category.magento_id,
      product: product_category.product.magento_id,
      position: product_category.position,
      product_identifier_type: 'id'
  }
  MagentoApi.create_call(:catalog_category_update_product, message)
end

.update_product_positions(category, store) ⇒ Gemgento::MagentoResponse

Update Category Product positions in Magento.

Parameters:

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gemgento/api/soap/catalog/category.rb', line 88

def self.update_product_positions(category, store)
  # create an array of product positions
  product_positions = []
    category.product_categories.where(store: store).each do |product_category|
    next if product_category.category.nil? or product_category.product.nil? or !product_category.product.deleted_at.nil?

    product_positions << {
        product_id: product_category.product.magento_id,
        position: product_category.position
    }
  end

  message = {
      category_id: category.magento_id,
      product_positions: {item: product_positions},
      store_id: store.magento_id
  }

  MagentoApi.create_call(:catalog_category_update_product_positions, message)
end