Class: Gemgento::ProductImport
- Defined in:
- app/models/gemgento/product_import.rb
Overview
Constant Summary collapse
- NON_ATTRIBUTE_HEADERS =
%w(sku status image visibility magento_type category)
Instance Attribute Summary collapse
-
#simple_products ⇒ Object
Returns the value of attribute simple_products.
Attributes inherited from Import
#header_row, #row, #spreadsheet
Instance Method Summary collapse
- #configurable_attributes ⇒ Object
-
#create_attribute_option(product_attribute, option_label) ⇒ Gemgento::ProductAttributeOption
Create a new ProductAttributeOption.
-
#create_configurable_product ⇒ Gemgento::Product
Create/Update a configurable product.
-
#create_images(product) ⇒ void
Find and create all images for a Product.
-
#create_simple_product ⇒ Gemgento::Product
Create/Update a simple product.
- #default_options ⇒ Object
-
#existing_magento_id(sku) ⇒ Integer?
Look for existing magento_id based on sku.
- #image_file_extensions_raw ⇒ Object
- #image_file_extensions_raw=(values) ⇒ Object
- #image_labels_raw ⇒ Object
- #image_labels_raw=(values) ⇒ Object
- #image_types_raw ⇒ Object
- #image_types_raw=(values) ⇒ Object
- #include_images? ⇒ Boolean
- #process_configurable_product ⇒ Object
-
#process_row ⇒ Object
IMPORT PROCESSING ###.
- #process_simple_product ⇒ Object
- #product_attribute_set ⇒ Gemgento::ProductAttributeSet
- #root_category ⇒ Object
-
#set_attribute_values(product) ⇒ Gemgento::Product
Set product attribute values supplied by spreadsheet.
-
#set_categories(product) ⇒ void
Associate Product with Categories.
-
#set_default_attribute_values(product) ⇒ Gemgento::Product
Set default attribute values based on options and missing values.
- #set_default_config_inventories(product) ⇒ void
- #set_default_inventory_values? ⇒ Boolean
- #store ⇒ Object
Methods inherited from Import
#content_index_range, #percentage_complete, #process, #process_later, #set_default_options, #set_default_process_errors, #set_total_rows, #value
Instance Attribute Details
#simple_products ⇒ Object
Returns the value of attribute simple_products.
8 9 10 |
# File 'app/models/gemgento/product_import.rb', line 8 def simple_products @simple_products end |
Instance Method Details
#configurable_attributes ⇒ Object
87 88 89 |
# File 'app/models/gemgento/product_import.rb', line 87 def configurable_attributes @configurable_attributes ||= Gemgento::ProductAttribute.where(is_configurable: true, id: [:configurable_attribute_ids]) end |
#create_attribute_option(product_attribute, option_label) ⇒ Gemgento::ProductAttributeOption
Create a new ProductAttributeOption.
195 196 197 198 199 200 201 202 203 |
# File 'app/models/gemgento/product_import.rb', line 195 def 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 = self.store attribute_option.sync_local_to_magento return Gemgento::ProductAttributeOption.find_by(product_attribute: product_attribute, label: option_label, store: self.store) end |
#create_configurable_product ⇒ Gemgento::Product
Create/Update a configurable product.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'app/models/gemgento/product_import.rb', line 289 def create_configurable_product sku = value('sku') # set the default configurable product attributes configurable_product = Gemgento::Product.not_deleted.find_or_initialize_by(sku: sku) configurable_product.magento_id = existing_magento_id(sku) configurable_product.magento_type = 'configurable' configurable_product.product_attribute_set = product_attribute_set configurable_product.status = value('status', :boolean) configurable_product.stores << store unless configurable_product.stores.include?(store) configurable_product.sync_needed = false configurable_product.save # add the configurable attributes configurable_attributes.each do |configurable_attribute| configurable_product.configurable_attributes << configurable_attribute unless configurable_product.configurable_attributes.include? configurable_attribute end # associate all simple products with the new configurable product self.simple_products.each do |simple_product| configurable_product.simple_products << simple_product unless configurable_product.simple_products.include?(simple_product) end # set the additional configurable product details set_attribute_values(configurable_product) set_categories(configurable_product) configurable_product.visibility = self.configurable_product_visibility.to_i configurable_product.sync_needed = true if configurable_product.save # add the images create_images(configurable_product) if include_images? set_default_config_inventories(configurable_product) if self.set_default_inventory_values? end return configurable_product end |
#create_images(product) ⇒ void
This method returns an undefined value.
Find and create all images for a Product.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'app/models/gemgento/product_import.rb', line 251 def create_images(product) self.image_labels.each_with_index do |label, position| self.image_file_extensions.each do |extension| file_name = self.image_path + value('image') + '_' + label + extension next unless File.exist?(file_name) types = [] unless self.image_types[position].nil? types = product_attribute_set.asset_types.where(code: self.image_types[position].split(',').map(&:strip)) end image = Asset.new image.product = product image.store = self.store image.position = position image.label = label image.set_file(File.open(file_name)) types.each do |type| image.asset_types << type end # save without sync to format with paperclip image.sync_needed = false image.save # save with sync to push to magento image.sync_needed = true image.save end end end |
#create_simple_product ⇒ Gemgento::Product
Create/Update a simple product.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/models/gemgento/product_import.rb', line 126 def create_simple_product sku = value('sku') product = Gemgento::Product.not_deleted.find_or_initialize_by(sku: sku) product.magento_id = existing_magento_id(sku) product.magento_type = 'simple' product.product_attribute_set = self.product_attribute_set product.stores << self.store unless product.stores.include?(self.store) product.status = value('status', :boolean) unless product.magento_id product.sync_needed = false product.save end product = set_attribute_values(product) set_categories(product) product.sync_needed = true if product.save create_images(product) if self.include_images? set_default_config_inventories(product) if self.set_default_inventory_values? end return product end |
#default_options ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/gemgento/product_import.rb', line 12 def { image_labels: [], image_file_extensions: [], image_types: [], image_path: nil, product_attribute_set_id: nil, store_id: nil, root_category_id: nil, simple_product_visibility: 1, configurable_product_visibility: 1, set_default_inventory_values: false, include_images: false, configurable_attribute_ids: [] } end |
#existing_magento_id(sku) ⇒ Integer?
Look for existing magento_id based on sku
347 348 349 350 351 352 353 354 355 356 |
# File 'app/models/gemgento/product_import.rb', line 347 def existing_magento_id(sku) magento_id = nil if magento_product = Magento::ProductAdapter.find_by(sku: sku) magento_id = magento_product[:product_id].to_i magento_id = nil unless magento_id > 0 end return magento_id end |
#image_file_extensions_raw ⇒ Object
37 38 39 |
# File 'app/models/gemgento/product_import.rb', line 37 def image_file_extensions_raw image_file_extensions.join(', ') end |
#image_file_extensions_raw=(values) ⇒ Object
41 42 43 |
# File 'app/models/gemgento/product_import.rb', line 41 def image_file_extensions_raw=(values) [:image_file_extensions] = values.gsub(' ', '').split(',') end |
#image_labels_raw ⇒ Object
29 30 31 |
# File 'app/models/gemgento/product_import.rb', line 29 def image_labels_raw image_labels.join("\n") end |
#image_labels_raw=(values) ⇒ Object
33 34 35 |
# File 'app/models/gemgento/product_import.rb', line 33 def image_labels_raw=(values) [:image_labels] = values.gsub("\r", '').split("\n") end |
#image_types_raw ⇒ Object
45 46 47 |
# File 'app/models/gemgento/product_import.rb', line 45 def image_types_raw image_types.join("\n") end |
#image_types_raw=(values) ⇒ Object
49 50 51 52 |
# File 'app/models/gemgento/product_import.rb', line 49 def image_types_raw=(values) [:image_types] = [] [:image_types] = values.gsub("\r", '').split("\n").map { |t| t.split(',').collect(&:strip) } end |
#include_images? ⇒ Boolean
54 55 56 |
# File 'app/models/gemgento/product_import.rb', line 54 def include_images? [:include_images].to_bool end |
#process_configurable_product ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'app/models/gemgento/product_import.rb', line 113 def process_configurable_product configurable_product = create_configurable_product if configurable_product.errors.any? self.process_errors << "Row ##{current_row}: #{configurable_product.errors..join(', ')}" end self.simple_products = [] end |
#process_row ⇒ Object
IMPORT PROCESSING ###
93 94 95 96 97 98 99 100 101 |
# File 'app/models/gemgento/product_import.rb', line 93 def process_row self.simple_products ||= [] if value('magento_type').casecmp('simple') == 0 process_simple_product else process_configurable_product end end |
#process_simple_product ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'app/models/gemgento/product_import.rb', line 103 def process_simple_product simple_product = create_simple_product if simple_product.errors.any? self.process_errors << "Row ##{current_row}: #{simple_product.errors..join(', ')}" else self.simple_products << simple_product end end |
#product_attribute_set ⇒ Gemgento::ProductAttributeSet
63 64 65 66 67 68 69 |
# File 'app/models/gemgento/product_import.rb', line 63 def product_attribute_set if [:product_attribute_set_id].nil? nil else @product_attribute_set ||= Gemgento::ProductAttributeSet.find([:product_attribute_set_id]) end end |
#root_category ⇒ Object
79 80 81 82 83 84 85 |
# File 'app/models/gemgento/product_import.rb', line 79 def root_category if [:root_category_id].nil? nil else @root_category ||= Gemgento::Category.find([:root_category_id]) end end |
#set_attribute_values(product) ⇒ Gemgento::Product
Set product attribute values supplied by spreadsheet.
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 186 187 188 |
# File 'app/models/gemgento/product_import.rb', line 159 def set_attribute_values(product) self.header_row.each do |attribute_code| next if NON_ATTRIBUTE_HEADERS.include?(attribute_code) product_attribute = product_attribute_set.product_attributes.find_by!(code: attribute_code) value = value(attribute_code) value = value.gsub('.0', '') if value.end_with? '.0' if product_attribute.frontend_input == 'select' label = value attribute_option = product_attribute..find_by(label: label, store: self.store) if attribute_option.nil? attribute_option = create_attribute_option(product_attribute, label) end value = attribute_option.nil? ? nil : attribute_option.value end if value.nil? self.process_errors << "Row #{current_row}: Unknown attribute value '#{value(attribute_code)}' for code '#{attribute_code}'" else product.set_attribute_value(product_attribute.code, value, self.store) end end product = set_default_attribute_values(product) return product end |
#set_categories(product) ⇒ void
This method returns an undefined value.
Associate Product with Categories.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'app/models/gemgento/product_import.rb', line 225 def set_categories(product) categories = value('category').split('&') categories.each do |category_string| category_string.strip! subcategories = category_string.split('>') parent_id = self.root_category.id subcategories.each do |category_url_key| category_url_key.strip! category = Gemgento::Category.find_by(url_key: category_url_key, parent_id: parent_id) unless category.nil? Gemgento::ProductCategory.find_or_create_by!(category: category, product: product, store: self.store) parent_id = category.id else self.process_errors << "Row ##{@index}: Unknown category url key '#{category_url_key}' - skipped" end end end end |
#set_default_attribute_values(product) ⇒ Gemgento::Product
Set default attribute values based on options and missing values.
209 210 211 212 213 214 215 216 217 218 219 |
# File 'app/models/gemgento/product_import.rb', line 209 def set_default_attribute_values(product) product.status = 1 if product.status.nil? product.visibility = self.simple_product_visibility.to_i if product.url_key.nil? url_key = product.name.to_s.strip.gsub(' ', '-').gsub(/[^\w\s]/, '').downcase product.set_attribute_value('url_key', url_key) end return product end |
#set_default_config_inventories(product) ⇒ void
This method returns an undefined value.
331 332 333 334 335 336 337 338 339 340 341 |
# File 'app/models/gemgento/product_import.rb', line 331 def set_default_config_inventories(product) inventory = product.inventories.find_or_initialize_by(store: self.store) inventory.use_config_manage_stock = true inventory.use_config_backorders = true inventory.use_config_min_qty = true inventory.sync_needed = true inventory.save rescue ActiveRecord::RecordNotUnique # when Magento pushes inventory data back, it will create missing inventory rows set_default_config_inventories(product) end |
#set_default_inventory_values? ⇒ Boolean
58 59 60 |
# File 'app/models/gemgento/product_import.rb', line 58 def set_default_inventory_values? [:set_default_inventory_values].to_bool end |