Class: Gemgento::Adapter::Sellect::Product

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gemgento/adapter/sellect/product.rb

Class Method Summary collapse

Class Method Details

.create_attribute_option(product_attribute, option_label) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/gemgento/adapter/sellect/product.rb', line 136

def self.create_attribute_option(product_attribute, option_label)
  attribute_option = Gemgento::ProductAttributeOption.new
  attribute_option.product_attribute = product_attribute
  attribute_option.label = option_label
  attribute_option.store = Gemgento::Store.current
  attribute_option.sync_needed = false
  attribute_option.save

  attribute_option.sync_needed = true
  attribute_option.sync_local_to_magento
  attribute_option.destroy

  return Gemgento::ProductAttributeOption.where(product_attribute: product_attribute, label: option_label).first
end

.create_configurable_images(configurable_product) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/models/gemgento/adapter/sellect/product.rb', line 220

def self.create_configurable_images(configurable_product)
  default_product = configurable_product.simple_products.first

  default_product.assets.each do |asset|

    asset_copy = Gemgento::Asset.new

    configurable_product.assets.where(store: Gemgento::Store.current).each do |existing_configurable_asset|
      if !existing_configurable_asset.asset_file.nil? && FileUtils.compare_file(existing_configurable_asset.asset_file.file.path(:original), asset.asset_file.file.path(:original))
        asset_copy = existing_configurable_asset
        break
      end
    end

    asset_copy.product = configurable_product
    asset_copy.store = asset.store
    asset_copy.set_file(File.open(asset.asset_file.file.path(:original)))
    asset_copy.label = asset.label
    asset_copy.position = asset.position
    asset_copy.asset_types = asset.asset_types

    asset_copy.sync_needed = false
    asset_copy.save

    asset_copy.sync_needed = true
    asset_copy.save
  end
end

.get_option_label(option_id, variant_id) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/gemgento/adapter/sellect/product.rb', line 124

def self.get_option_label(option_id, variant_id)
  self.table_name = 'sellect_option_values'
  option_value = self.joins(ActiveRecord::Base.escape_sql(
                'INNER JOIN sellect_option_values_variants ON sellect_option_values_variants.option_value_id = sellect_option_values.id ' +
                    'AND sellect_option_values.option_type_id = ? AND sellect_option_values_variants.variant_id = ?',
                option_id,
                variant_id
            )).first

  return option_value.name
end

.get_price(variant_id, currency) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/gemgento/adapter/sellect/product.rb', line 187

def self.get_price(variant_id, currency)
  self.table_name = 'sellect_pricings'

  pricing = self.where('variant_id = ? AND currency LIKE ?', variant_id, currency).first

  if pricing.nil?
    return nil
  else
    return pricing.price
  end
end

.import(currency = 'usd', app_root = '/Users/Kevin/Sites/victoria_beckham_sites/vb_old/') ⇒ Object



5
6
7
8
9
10
11
12
# File 'app/models/gemgento/adapter/sellect/product.rb', line 5

def self.import(currency = 'usd', app_root = '/Users/Kevin/Sites/victoria_beckham_sites/vb_old/')
  #TODO: Unmapped attributes - color, hex_color, available_on, tax_category_id, shipping_category_id, on_sale, sale_price, model_name, size_pictured, product_details, count_on_hand, style_color, is_representative_color, season_id
  self.table_name = 'sellect_products'

  self.where('deleted_at IS NULL').each do |sellect_product|
    import_configurable_product(currency, app_root, sellect_product)
  end
end

.import_configurable_product(sellect_product, currency, app_root) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/gemgento/adapter/sellect/product.rb', line 14

def self.import_configurable_product(sellect_product, currency, app_root)
  product = Gemgento::Product.not_deleted.configurable.find_or_initialize_by(sku: sellect_product.sku)

  product.magento_type = 'configurable'
  product.sku = sellect_product.sku
  product.status = 1
  product.product_attribute_set = Gemgento::ProductAttributeSet.first
  product.visibility = 4
  product.sync_needed = false
  product.save

  product.set_attribute_value('name', sellect_product.name)
  product.set_attribute_value('short_description', sellect_product.description)
  product.set_attribute_value('url_key', sellect_product.permalink)
  product.set_attribute_value('meta_description', sellect_product.meta_description)
  product.set_attribute_value('meta_keyword', sellect_product.meta_keywords)
  product.set_attribute_value('description', sellect_product.detail_description)
  product.set_attribute_value('style_code', sellect_product.style)

  product.stores << Gemgento::Store.current unless product.stores.include? Gemgento::Store.current

  set_categories(sellect_product.id, product)

  simple_products = import_simple_products(sellect_product.id, product, app_root, currency)

  if simple_products.size <= 1 # if one or less simple products, the configurable is not needed
    product.destroy
  else # configurable is needed, set configurable attrbitues and simple products
    set_configurable_attribute(product, 'size')
    set_configurable_attribute(product, 'color')

    simple_products.each do |simple_product|
      product.simple_products << simple_product unless product.simple_products.include?(simple_product)
    end

    product.sync_needed = true
    product.save

    create_configurable_images(product)
  end
end

.import_simple_products(sellect_id, configurable_product, app_root, currency = 'usd') ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/gemgento/adapter/sellect/product.rb', line 56

def self.import_simple_products(sellect_id, configurable_product, app_root, currency = 'usd')
  self.table_name = 'sellect_variants'

  simple_products = []
  upc = Gemgento::ProductAttribute.find_by(code: 'upc')
  raise "Missing required product attribute 'UPC'" if upc.nil?

  sellect_variants = self.where('product_id = ?', sellect_id)

  sellect_variants.each do |sellect_variant|
    product = Gemgento::Product.not_deleted.simple.filter({ attribute: upc, value: sellect_variant.upc }).first_or_initialize
    product.magento_type = 'simple'
    product.status = 1
    product.visibility = sellect_variants.size > 1 ? 1 : 4
    product.product_attribute_set = configurable_product.product_attribute_set
    product.sync_needed = false
    product.save

    product.set_attribute_value('name', configurable_product.name)
    product.set_attribute_value('short_description', configurable_product.description)
    product.set_attribute_value('url_key', configurable_product.url_key)
    product.set_attribute_value('meta_description', configurable_product.meta_description)
    product.set_attribute_value('meta_keyword', configurable_product.meta_keyword)
    product.set_attribute_value('description', configurable_product.description)
    product.set_attribute_value('style_code', configurable_product.style_code)
    product.set_attribute_value('upc', sellect_variant.upc)
    product.set_attribute_value('weight', sellect_variant.weight.nil? ? 0 : sellect_variant.weight)
    product.set_attribute_value('price', get_price(sellect_variant.id, currency))
    product.stores << Gemgento::Store.current unless product.stores.include? Gemgento::Store.current
    product.categories = configurable_product.categories
    product.sync_needed = false
    product.save

    set_option_values(sellect_variant.id, product)

    product.sku = "#{sellect_variant.sku}_#{product.size}"

    product.sync_needed = true
    product.save

    set_assets(sellect_variant, product, app_root)

    simple_products << product
  end

  return simple_products
end

.set_assets(sellect_product, gemgento_product, app_root) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/models/gemgento/adapter/sellect/product.rb', line 151

def self.set_assets(sellect_product, gemgento_product, app_root)
  self.inheritance_column = :_type_disabled
  self.table_name = 'sellect_assets'

  self.where(viewable_id: sellect_product.id, viewable_type: 'Sellect::Variant').each do |sellect_asset|
    file = "#{app_root}/#{sellect_asset.id}/original/#{sellect_asset.attachment_file_name}"

    if File.exist?(file)
      image = Gemgento::Asset.new

      Gemgento::Asset.unscoped do
        gemgento_product.assets.reload.each do |asset|
          if !asset.asset_file.nil? && FileUtils.compare_file(asset.asset_file.file.path(:original), file)
            image = asset
            break
          end
        end
      end

      image.product = gemgento_product
      image.store = Gemgento::Store.current
      image.position = sellect_asset.position
      image.label = sellect_asset.alt
      image.set_file(File.open(file))
      image.asset_types << Gemgento::AssetType.all
      image.sync_needed = false
      image.save

      image.sync_needed = true
      image.save

      image
    end
  end
end

.set_categories(product_id, product) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/models/gemgento/adapter/sellect/product.rb', line 199

def self.set_categories(product_id, product)
  self.table_name = 'sellect_product_categories'

  self.joins(ActiveRecord::Base.escape_sql(
            'INNER JOIN sellect_product_positions ON sellect_product_positions.product_category_id = sellect_product_categories.id ' +
                'AND sellect_product_positions.product_id = ?',
            product_id
        )).each do |sellect_category|
    categories = Gemgento::Category.where(url_key: sellect_category.permalink)

    categories.each do |category|
      product.categories << category unless product.categories.include? category
    end
  end
end

.set_configurable_attribute(product, attribute_code) ⇒ Object



215
216
217
218
# File 'app/models/gemgento/adapter/sellect/product.rb', line 215

def self.set_configurable_attribute(product, attribute_code)
  attribute = Gemgento::ProductAttribute.find_by(code: attribute_code)
  product.configurable_attributes << attribute unless product.configurable_attributes.include?(attribute)
end

.set_option_values(sellect_id, product) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/gemgento/adapter/sellect/product.rb', line 104

def self.set_option_values(sellect_id, product)
  self.table_name = 'sellect_option_types'

  self.all.each do |option|
    attribute = Gemgento::ProductAttribute.find_by(code: option.name.downcase)
    label = get_option_label(option.id, sellect_id)
    label = '' if label.nil?

    attribute_option = Gemgento::ProductAttributeOption.find_by(product_attribute_id: attribute.id, label: label)

    if attribute_option.nil?
      attribute_option = create_attribute_option(attribute, label)
    end

    product.set_attribute_value(attribute.code, attribute_option.value)
    product.sync_needed = false
    product.save
  end
end