huginn_bigcommerce_product_agent 1.3.5 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6edc2db0f86f9193fa3463ca3a21db71bc52b06b007d1d00d81b934891eea499
4
- data.tar.gz: f2635fda8a26cf5c0e7706c2be7794897769d0a99f3da2c243e61f96da069242
3
+ metadata.gz: 728526f71b899d714ffeaf9cca25b150326ba889dc17c732ace2c41b7b92782e
4
+ data.tar.gz: d237a9f1a6eb179f76679a7a870b56a7844239ee45f4fd32f56ba8a296201ca9
5
5
  SHA512:
6
- metadata.gz: e730a73a5be204f43e0c54866f37e9e0e16542ad5357806d6dc1e69c41c0a3a9cd0ef9fb1c90b39982b3471dac0c61c23b0551ef60184ef9b66bfa878e0fa5f2
7
- data.tar.gz: 628d7bd1ddf7dcfa831a3f34d0522c155eefcb99a90b39528a736251ed03d17480c30f9fa2a93def20af9aed0e6a6d31cea82333733a9ab10c910465c3e3284d
6
+ metadata.gz: 54e4aa00c7216da7c8d66ba4ea45cbcd1ec51339982686719505f2806761a91e601d807165c194975777e9b7ae0ee8327cde0d9ee966ccee264659d70b083ee1
7
+ data.tar.gz: 9e9ff91a64dbab6ac8e0a1b599a5415ce2744d39cf00e3ca12d697fddde434dcf9aa174ecd89b0e67333838bd03cfe8585797c6d9e50c73cd57b936eb3ed8b9a
@@ -26,8 +26,8 @@ module BigcommerceProductAgent
26
26
 
27
27
  def upsert(payload, params={})
28
28
  begin
29
- if payload['id']
30
- return update(payload['id'], payload, params)
29
+ if payload[:id]
30
+ return update(payload[:id], payload, params)
31
31
  else
32
32
  return create(payload, params)
33
33
  end
@@ -28,7 +28,8 @@ module Agents
28
28
  'custom_fields_map' => {},
29
29
  'meta_fields_map' => {},
30
30
  'meta_fields_namespace' => '',
31
- 'mode' => modes[0]
31
+ 'mode' => modes[0],
32
+ 'not_purchasable_format_list' => []
32
33
  }
33
34
  end
34
35
 
@@ -62,6 +63,10 @@ module Agents
62
63
  unless options['mode'].present? && modes.include?(options['mode'])
63
64
  errors.add(:base, "mode is a required field and must be one of: #{modes.join(', ')}")
64
65
  end
66
+
67
+ if options['not_purchasable_format_list'].present? && !options['not_purchasable_format_list'].is_a?(Array)
68
+ errors.add(:base, 'not_purchasable_format_list must be an Array')
69
+ end
65
70
  end
66
71
 
67
72
  def working?
@@ -103,15 +108,6 @@ module Agents
103
108
  def handle_variants(event)
104
109
  product = event.payload
105
110
 
106
- wrapper_skus = {
107
- physical: get_mapper(:ProductMapper).get_wrapper_sku_physical(product),
108
- digital: get_mapper(:ProductMapper).get_wrapper_sku_digital(product),
109
- }
110
- bc_products = @product.get_by_skus(
111
- wrapper_skus.map {|k,v| v},
112
- %w[custom_fields options]
113
- )
114
-
115
111
  split = get_mapper(:ProductMapper).split_digital_and_physical(
116
112
  product,
117
113
  interpolated['custom_fields_map']
@@ -119,6 +115,25 @@ module Agents
119
115
  physical = split[:physical]
120
116
  digital = split[:digital]
121
117
 
118
+ wrapper_skus = {
119
+ physical: get_mapper(:ProductMapper).get_wrapper_sku(physical),
120
+ digital: get_mapper(:ProductMapper).get_wrapper_sku(digital),
121
+ }
122
+
123
+ bc_products = @product.get_by_skus(
124
+ wrapper_skus.map {|k,v| v},
125
+ %w[custom_fields options]
126
+ )
127
+ #save skus
128
+ digital_skus = []
129
+ physical_skus = []
130
+ if split[:digital] and split[:physical]
131
+ digital_skus.concat([wrapper_skus[:digital]])
132
+ digital_skus.concat(get_mapper(:ProductMapper).get_product_skus(split[:digital])).join(",")
133
+ physical_skus.concat([wrapper_skus[:physical]])
134
+ physical_skus.concat(get_mapper(:ProductMapper).get_product_skus(split[:physical])).join(",")
135
+ end
136
+
122
137
  # upsert wrapper products
123
138
  split.each do |type, product|
124
139
  is_digital = type == :digital ? true : false
@@ -133,22 +148,26 @@ module Agents
133
148
  variant_option_name = get_mapper(:OptionMapper).variant_option_name
134
149
  bc_option = !bc_product.nil? ? bc_product['options'].select {|opt| opt['display_name'] === variant_option_name}.first : nil
135
150
 
151
+ search_skus = is_digital ? physical_skus : digital_skus
136
152
  # ##############################
137
153
  # 1. update wrapper product
138
154
  # ##############################
139
- upsert_result = upsert_product(wrapper_sku, product, bc_product, is_digital)
155
+ upsert_result = upsert_product(wrapper_sku, product, bc_product, is_digital, search_skus)
140
156
  bc_product = upsert_result[:product]
141
- bc_products[wrapper_sku] = bc_product
142
- product_id = bc_products[wrapper_sku]['id']
143
157
 
144
158
  # clean up custom/meta fields. there are not batch operations so we might as well do them here.
145
159
  custom_fields_delete = upsert_result[:custom_fields_delete].select {|field| field['name'] != 'related_product_id'}
146
160
  clean_up_custom_fields(custom_fields_delete)
147
- update_meta_fields(
161
+ meta_fields = update_meta_fields(
148
162
  upsert_result[:meta_fields_upsert],
149
163
  upsert_result[:meta_fields_delete],
150
164
  )
151
165
 
166
+ bc_product['meta_fields'] = meta_fields
167
+
168
+ bc_products[wrapper_sku] = bc_product
169
+ product_id = bc_products[wrapper_sku]['id']
170
+
152
171
  # ##############################
153
172
  # 2. upsert option & option_values
154
173
  # ##############################
@@ -180,6 +199,8 @@ module Agents
180
199
  [option_value],
181
200
  product_id,
182
201
  bc_variant.nil? ? nil : bc_variant['id'],
202
+ interpolated['not_purchasable_format_list'],
203
+ bc_option_value
183
204
  )
184
205
  end
185
206
 
@@ -327,7 +348,7 @@ module Agents
327
348
  return ::BigcommerceProductAgent::Mapper.const_get(class_name.to_sym)
328
349
  end
329
350
 
330
- def upsert_product(sku, product, bc_product = nil, is_digital=false)
351
+ def upsert_product(sku, product, bc_product = nil, is_digital=false, search_skus=[])
331
352
  custom_fields_updates = get_mapper(:CustomFieldMapper).map(
332
353
  interpolated['custom_fields_map'],
333
354
  product,
@@ -340,7 +361,10 @@ module Agents
340
361
  sku,
341
362
  product,
342
363
  product_id,
343
- { custom_fields: custom_fields_updates[:upsert] },
364
+ {
365
+ additional_search_terms: search_skus,
366
+ custom_fields: custom_fields_updates[:upsert]
367
+ },
344
368
  is_digital,
345
369
  )
346
370
 
@@ -25,10 +25,10 @@ module BigcommerceProductAgent
25
25
 
26
26
  result = {
27
27
  name: name,
28
- sku: variant ? variant['sku'] : default_sku,
28
+ sku: product ? product['sku'] : default_sku,
29
29
  is_default: product['isDefault'],
30
30
  type: product['isDigital'] == true || is_digital ? 'digital' : 'physical',
31
- description: product['description'],
31
+ description: product['description'] || '',
32
32
  price: product['offers'] && product['offers'][0] ? product['offers'][0]['price'] : '0',
33
33
  categories: self.get_categories(product),
34
34
  available: 'available',
@@ -37,8 +37,8 @@ module BigcommerceProductAgent
37
37
  depth: product['depth'] ? product['depth']['value'] : '0',
38
38
  height: product['height'] ? product['height']['value'] : '0',
39
39
  meta_keywords: self.meta_keywords(product),
40
- meta_description: self.meta_description(product),
41
- search_keywords: self.meta_keywords(product).join(','),
40
+ meta_description: self.meta_description(product) || '',
41
+ search_keywords: self.get_search_keywords(additional_data.delete(:additional_search_terms), product),
42
42
  is_visible: variant ? false : true,
43
43
  }
44
44
  result[:upc] = product['gtin12'] if product['gtin12']
@@ -47,21 +47,16 @@ module BigcommerceProductAgent
47
47
  end
48
48
 
49
49
  def self.get_wrapper_sku(product)
50
- "#{product['sku']}-W"
51
- end
52
-
53
- def self.get_wrapper_sku_physical(product)
54
- self.get_wrapper_sku(product)
55
- end
56
-
57
- def self.get_wrapper_sku_digital(product)
58
- "#{self.get_wrapper_sku_physical(product)}-DIGITAL"
50
+ if product
51
+ "#{product['sku']}-W"
52
+ end
59
53
  end
60
54
 
61
55
  def self.payload(sku, product, product_id = nil, additional_data = {}, is_digital = false)
62
56
  variant = self.get_variant_by_sku(sku, product)
63
57
  payload = self.map(product, variant, additional_data, is_digital, sku)
64
- payload['id'] = product_id unless product_id.nil?
58
+ payload[:id] = product_id unless product_id.nil?
59
+ payload[:sku] = self.get_wrapper_sku(product)
65
60
 
66
61
  return payload
67
62
  end
@@ -106,6 +101,7 @@ module BigcommerceProductAgent
106
101
  if digitals.length > 0
107
102
  clone = Marshal.load(Marshal.dump(product))
108
103
  clone['model'] = digitals
104
+ clone['sku'] = clone['model'].select {|m| m['isDefault'] = true}.first['sku']
109
105
  self.merge_additional_properties(clone, field_map)
110
106
  result[:digital] = clone
111
107
  end
@@ -115,6 +111,7 @@ module BigcommerceProductAgent
115
111
  if physicals.length > 0
116
112
  clone = Marshal.load(Marshal.dump(product))
117
113
  clone['model'] = physicals
114
+ clone['sku'] = clone['model'].select {|m| m['isDefault'] = true}.first['sku']
118
115
  self.merge_additional_properties(clone, field_map)
119
116
  result[:physical] = clone
120
117
  end
@@ -185,6 +182,11 @@ module BigcommerceProductAgent
185
182
  end
186
183
  end
187
184
  end
185
+
186
+ # get a list of search keywords for the products
187
+ def self.get_search_keywords(additional_search_terms, product)
188
+ return (self.meta_keywords(product) + additional_search_terms.split(",")).join(",")
189
+ end
188
190
  end
189
191
  end
190
192
  end
@@ -2,7 +2,7 @@ module BigcommerceProductAgent
2
2
  module Mapper
3
3
  class VariantMapper
4
4
 
5
- def self.map(variant, option_values, product_id, variant_id=nil)
5
+ def self.map(variant, option_values, product_id, variant_id=nil, not_purchasable_format_list, bc_option_value)
6
6
  mapped = {
7
7
  product_id: product_id,
8
8
  sku: variant['sku'],
@@ -29,6 +29,14 @@ module BigcommerceProductAgent
29
29
  mapped[:id] = variant_id
30
30
  end
31
31
 
32
+ unless not_purchasable_format_list.nil?
33
+ not_purchasable_format_list.each do |format|
34
+ if format == bc_option_value['label']
35
+ mapped[:purchasing_disabled] = true
36
+ end
37
+ end
38
+ end
39
+
32
40
  return mapped
33
41
  end
34
42
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huginn_bigcommerce_product_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.5
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Spizziri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-30 00:00:00.000000000 Z
11
+ date: 2020-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler