Class: Magento::Import::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/magento/import/product.rb

Instance Method Summary collapse

Constructor Details

#initialize(website_ids, images_folder = nil) ⇒ Product

Returns a new instance of Product.



6
7
8
9
# File 'lib/magento/import/product.rb', line 6

def initialize(website_ids, images_folder = nil)
  @website_ids = website_ids
  @image_finder = images_folder ? ImageFinder.new(images_folder) : nil
end

Instance Method Details

#import(products) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/magento/import/product.rb', line 11

def import(products)
  products.each do |product|
    params = Magento::Params::CreateProduct.new(
      sku: product.sku,
      name: product.name.gsub(/[ ]+/, ' '),
      description: product.description || product.name,
      brand: product.brand,
      price: product.price.to_f,
      special_price: product.special_price ? product.special_price.to_f : nil,
      quantity: numeric?(product.quantity) ? product.quantity.to_f : 0,
      weight: 0.3,
      manage_stock: numeric?(product.quantity),
      attribute_set_id: 4,
      category_ids: [product.cat1, product.cat2, product.cat3].compact,
      website_ids: @website_ids,
      images: images(product)
    ).to_h
  
    product = Magento::Product.create(params)
  
    puts "Created product: #{product.sku} => #{product.name}"
  rescue => e
    puts "Error on create: #{product.sku} => #{product.name}"
    puts " - Error details: #{e}"
  end
end