magento 0.15.1 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/magento.rb +1 -0
- data/lib/magento/import.rb +13 -0
- data/lib/magento/import/category.rb +51 -0
- data/lib/magento/import/csv_reader.rb +30 -0
- data/lib/magento/import/product.rb +63 -0
- data/lib/magento/params/create_category.rb +7 -7
- data/lib/magento/params/create_image.rb +1 -0
- data/lib/magento/params/create_product.rb +3 -2
- data/lib/magento/request.rb +1 -1
- data/lib/magento/version.rb +1 -1
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54c756e43ba125d24cfd2695f5b3b362acf6a489cb0ccc7b7abd853b8488164c
|
4
|
+
data.tar.gz: 103375f7ba57dae9d78e93d19e0cb95d6192365390b4d05005c465eea6737908
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d839bf8cc47b2a7e639de881d2e8505cd8b3bb2d52428a1e8c58438e96e3bbf84392ccf22498b393a5ec60b812afca078e9f2cd33568f2475081501601f0c919
|
7
|
+
data.tar.gz: 3192fd700e6356b0a58039b26fe8c5b25ab577328d67cce6a4495465f84c4dc9f6aa48e37cacbaafc7068b61ac91a983fe7b41287ff4316920b09951079a0dcf
|
data/lib/magento.rb
CHANGED
@@ -21,6 +21,7 @@ require_relative 'magento/order'
|
|
21
21
|
require_relative 'magento/invoice'
|
22
22
|
require_relative 'magento/guest_cart'
|
23
23
|
require_relative 'magento/sales_rule'
|
24
|
+
require_relative 'magento/import'
|
24
25
|
|
25
26
|
Dir[File.expand_path('magento/shared/*.rb', __dir__)].map { |f| require f }
|
26
27
|
Dir[File.expand_path('magento/params/*.rb', __dir__)].map { |f| require f }
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative 'import/csv_reader'
|
2
|
+
require_relative 'import/category'
|
3
|
+
require_relative 'import/product'
|
4
|
+
|
5
|
+
module Magento
|
6
|
+
module Import
|
7
|
+
def self.from_csv(file, image_path:, website_ids: [0])
|
8
|
+
products = CSVReader.new(file).get_products
|
9
|
+
products = Category.new(products).associate
|
10
|
+
Product.new(image_path, website_ids).import(products)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Magento
|
2
|
+
module Import
|
3
|
+
class Category
|
4
|
+
def initialize(products)
|
5
|
+
@products = products
|
6
|
+
@category_root = Magento::Category.all
|
7
|
+
@cats = @category_root.children_data
|
8
|
+
end
|
9
|
+
|
10
|
+
def associate
|
11
|
+
@products.each do |prod|
|
12
|
+
cat1 = find_or_create(name: prod.cat1, parent: @category_root) if prod.cat1
|
13
|
+
cat2 = find_or_create(name: prod.cat2, parent: cat1) if prod.cat2
|
14
|
+
cat3 = find_or_create(name: prod.cat3, parent: cat2) if prod.cat3
|
15
|
+
|
16
|
+
prod.cat1, prod.cat2, prod.cat3 = cat1&.id, cat2&.id, cat3&.id
|
17
|
+
|
18
|
+
@cats.push(*[cat1, cat2, cat3].compact)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def find_or_create(name:, parent:)
|
25
|
+
find(name, cats: @cats, parent_id: parent.id) || create(name, parent: parent)
|
26
|
+
end
|
27
|
+
|
28
|
+
def find(name, cats:, parent_id:)
|
29
|
+
cats.each do |cat|
|
30
|
+
return cat if cat.name == name && cat.parent_id == parent_id
|
31
|
+
|
32
|
+
if cat.respond_to?(:children_data) && cat.children_data&.size.to_i > 0
|
33
|
+
result = find(name, cats: cat.children_data, parent_id: parent_id)
|
34
|
+
return result if result
|
35
|
+
end
|
36
|
+
end
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def create(name, parent:)
|
41
|
+
params = Magento::Params::CreateCategoria.new(
|
42
|
+
name: name,
|
43
|
+
parent_id: parent.id,
|
44
|
+
url: "#{Magento.store}-#{parent.id}-#{name.parameterize}"
|
45
|
+
)
|
46
|
+
|
47
|
+
Magento::Category.create(params.to_h).tap { |c| puts "Create: #{c.id} => #{c.name}" }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module Magento
|
5
|
+
module Import
|
6
|
+
class CSVReader
|
7
|
+
def initialize(csv_file)
|
8
|
+
@csv = CSV.read(csv_file, col_sep: ';')
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_products
|
12
|
+
@csv[1..].map do |row|
|
13
|
+
name, sku, ean, description, price, special_price, quantity, cat1, cat2, cat3 = row
|
14
|
+
OpenStruct.new({
|
15
|
+
name: name,
|
16
|
+
sku: sku,
|
17
|
+
ean: ean,
|
18
|
+
description: description,
|
19
|
+
price: price,
|
20
|
+
special_price: special_price,
|
21
|
+
quantity: quantity,
|
22
|
+
cat1: cat1,
|
23
|
+
cat2: cat2,
|
24
|
+
cat3: cat3
|
25
|
+
})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Magento
|
2
|
+
module Import
|
3
|
+
class Product
|
4
|
+
def initialize(images_path, website_ids)
|
5
|
+
@images_path = images_path
|
6
|
+
@website_ids = website_ids
|
7
|
+
end
|
8
|
+
|
9
|
+
def import(products)
|
10
|
+
products.each do |product|
|
11
|
+
params = Magento::Params::CreateProduct.new(
|
12
|
+
sku: product.sku,
|
13
|
+
name: product.name.gsub(/[ ]+/, ' '),
|
14
|
+
description: product.description || product.name,
|
15
|
+
brand: product.brand,
|
16
|
+
price: product.price.to_f,
|
17
|
+
special_price: product.special_price ? product.special_price.to_f : nil,
|
18
|
+
quantity: numeric?(product.quantity) ? product.quantity.to_f : 0,
|
19
|
+
weight: 0.3,
|
20
|
+
manage_stock: numeric?(product.quantity),
|
21
|
+
attribute_set_id: 4,
|
22
|
+
category_ids: [product.cat1, product.cat2, product.cat3].compact,
|
23
|
+
website_ids: @website_ids,
|
24
|
+
images: images(product)
|
25
|
+
).to_h
|
26
|
+
|
27
|
+
product = Magento::Product.create(params)
|
28
|
+
|
29
|
+
puts "Produto criado: #{product.sku} => #{product.name}"
|
30
|
+
rescue => e
|
31
|
+
puts "Erro ao criado: #{product.sku} => #{product.name}"
|
32
|
+
puts " - Detalhes do erro: #{e}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def images(product)
|
39
|
+
image = find_image(product)
|
40
|
+
return [] unless image
|
41
|
+
|
42
|
+
Magento::Params::CreateImage.new(
|
43
|
+
path: image,
|
44
|
+
title: product.name,
|
45
|
+
position: 0,
|
46
|
+
main: true
|
47
|
+
).variants
|
48
|
+
end
|
49
|
+
|
50
|
+
def find_image(product)
|
51
|
+
prefix = "#{@images_path}/#{product.sku}"
|
52
|
+
|
53
|
+
extensions = %w[jpg jpeg png webp]
|
54
|
+
extensions.map { |e| ["#{prefix}.#{e}", "#{prefix}.#{e.upcase}"] }.flatten
|
55
|
+
.find { |file| File.exist?(file) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def numeric?(value)
|
59
|
+
!!(value.to_s =~ /^[\d]+$/)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -4,17 +4,17 @@ module Magento
|
|
4
4
|
module Params
|
5
5
|
class CreateCategoria < Dry::Struct
|
6
6
|
attribute :name, Type::String
|
7
|
-
attribute :parent_id, Type::
|
8
|
-
attribute :
|
7
|
+
attribute :parent_id, Type::Integer.optional
|
8
|
+
attribute :url, Type::String.optional.default(nil)
|
9
9
|
attribute :is_active, Type::Bool.default(true)
|
10
10
|
|
11
11
|
def to_h
|
12
12
|
{
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
}
|
13
|
+
name: name,
|
14
|
+
parent_id: parent_id,
|
15
|
+
is_active: is_active,
|
16
|
+
custom_attributes: url ? [{attribute_code: 'url_key', value: url }] : nil
|
17
|
+
}.compact
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -54,7 +54,7 @@ module Magento
|
|
54
54
|
attribute :sku, Type::String
|
55
55
|
attribute :name, Type::String
|
56
56
|
attribute :description, Type::String
|
57
|
-
attribute :brand, Type::String
|
57
|
+
attribute :brand, Type::String.optional.default(nil)
|
58
58
|
attribute :price, Type::Coercible::Float
|
59
59
|
attribute :special_price, Type::Float.optional.default(nil)
|
60
60
|
attribute :attribute_set_id, Type::Integer
|
@@ -128,10 +128,11 @@ module Magento
|
|
128
128
|
default_attributes = [
|
129
129
|
CustomAttribute.new(attribute_code: 'description', value: description),
|
130
130
|
CustomAttribute.new(attribute_code: 'url_key', value: name.parameterize ),
|
131
|
-
CustomAttribute.new(attribute_code: 'product_brand', value: brand ),
|
132
131
|
CustomAttribute.new(attribute_code: 'featured', value: featured)
|
133
132
|
]
|
134
133
|
|
134
|
+
default_attributes.push(CustomAttribute.new(attribute_code: 'product_brand', value: brand)) if brand
|
135
|
+
|
135
136
|
if special_price.to_f > 0
|
136
137
|
default_attributes << CustomAttribute.new(attribute_code: 'special_price', value: special_price.to_s)
|
137
138
|
end
|
data/lib/magento/request.rb
CHANGED
@@ -53,7 +53,7 @@ module Magento
|
|
53
53
|
|
54
54
|
begin
|
55
55
|
msg = resp.parse['message']
|
56
|
-
errors = resp.parse['errors']
|
56
|
+
errors = resp.parse['errors'] || resp.parse['parameters']
|
57
57
|
if resp.parse['parameters'].is_a? Hash
|
58
58
|
resp.parse['parameters'].each { |k, v| msg.sub! "%#{k}", v }
|
59
59
|
end
|
data/lib/magento/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magento
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wallas Faria
|
@@ -96,6 +96,10 @@ files:
|
|
96
96
|
- lib/magento/customer.rb
|
97
97
|
- lib/magento/errors.rb
|
98
98
|
- lib/magento/guest_cart.rb
|
99
|
+
- lib/magento/import.rb
|
100
|
+
- lib/magento/import/category.rb
|
101
|
+
- lib/magento/import/csv_reader.rb
|
102
|
+
- lib/magento/import/product.rb
|
99
103
|
- lib/magento/invoice.rb
|
100
104
|
- lib/magento/model.rb
|
101
105
|
- lib/magento/model_mapper.rb
|