tece to shopify

Ruby code posted
created at 07 Aug 13:13

Edit | Back
1
2
3
4
5
6
7
8
9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
p_new = product
if r1.nil?
  puts "!! #{item_id} no product found in ERP"
  return ""
end

if r1["Vintage"].to_i > 1000
  p_new.title = r1["Vintage"] + " " + r1["LongTitle"]
else
  p_new.title = r1["LongTitle"]
end

vol = (r1["Volume"].to_f*1000).to_i
if (vol < 700 or vol > 799) and vol > 1
  p_new.title = p_new.title + " (" + vol.to_s + " ml)"
end

if p_new.handle?.nil? or "" == p_new.handle
  p_new.handle = (r1["LongTitle"]+"-"+r1["Vintage"]+"-"+r1["ItemID"]).downcase.gsub(/[^a-z0-9]/," ").strip.gsub(" ","-")
end

vendor_re = /.*\s?-\s(.*)|.*(Domaine.*)|(.*)/

p_new.vendor = r1["LongTitle"].match(vendor_re).to_a[1..3].join()
if r1["Reference3"] != ""
  p_new.vendor = r1["Reference3"]
  puts p_new.vendor
end
p_new.product_type = r1["ItemType"].gsub("00_","").capitalize

country_s = r1["Country"].capitalize
country_s = "USA" if country_s.downcase == "usa"
country_s = "United Kingdom" if country_s.downcase == "uk"

tags = []
tags.push "appellation-" + r1["Appellation"].gsub("_AOC","").gsub("_"," ").capitalize
tags.push "country-" + country_s
tags.push p_new.product_type
tags.push "region-" + r1["Region"].capitalize
tags.push "size-#{vol}ml" if vol > 10
tags.push "vintage-#{r1["Vintage"]}"
tags.push "wine"  # if not asso / cigar
if not r1["ZhName"].match(/\p{Han}/).nil?
  tags.push "zh-#{r1["ZhName"]}"
end

score_h = 0
r1["Score"].split(",").each do |s|
  tags.push "score-#{s}"
  score = s.match(/\d+/).to_s.to_i
  score_h = score if score > score_h
end

if score_h >= 99
  tags.push "score-A"
elsif score_h >= 95 and score_h <= 98
  tags.push "score-B"
elsif score_h >= 90 and score_h <= 94
  tags.push "score-C"
elsif score_h < 90 and score_h >= 60
  tags.push "score-D"
end

p_new.tags = tags.join(", ")

if p_new.new?
  v_new = Variant.new
else
  v_new = p_new.variants.first
end

v_new.price = r1["Price2"].to_i
v_new.compare_at_price =  r1["Price2"] == r1["Price3"]  ? 0 : r1["Price3"].to_i

v_new.sku = r1["ItemID"]
v_new.gram = vol * 2 if vol > 1
#v_new.inventory_quantity = r1["AvaQty"].to_i
#v_new.inventory_quantity = 0 if v_new.price == 0
v_new.inventory_management = "shopify"
v_new.taxable = false
v_new.weight = (vol * 2.0 / 1000) if vol > 1
2.19 KB in 13 ms with coderay