Module: WeChat::Product

Extended by:
ActiveSupport::Concern
Includes:
Entity::Base
Defined in:
lib/we_chat/product.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

CHILDREN =
{
sku: :sku_list,
category: :category_id,
category_property_item: :property,
product_attribute: :attrext}.freeze

Instance Method Summary collapse

Methods included from Entity::Base

#delete_from_we_chat, #reset_we_chat_id, #set_we_chat_id, #submit_to_we_chat, #submitted_to_we_chat?, #update_to_we_chat

Instance Method Details

#get_we_chat_id_from_data(data) ⇒ Object



54
55
56
# File 'lib/we_chat/product.rb', line 54

def get_we_chat_id_from_data(data)
  data[:product_id]
end

#to_we_chat_dataObject



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
# File 'lib/we_chat/product.rb', line 16

def to_we_chat_data
  we_chat_data = {}
  we_chat_data[:product_id] = self.we_chat_id
  we_chat_data[:product_base] = {}
  we_chat_data[:product_base][:name] = self.name
  we_chat_data[:product_base][:buy_limit] = self.buy_limit
  we_chat_data[:product_base][:main_img] = self.main_image_url
  we_chat_data[:product_base][:detail_html] = self.description

  we_chat_data[:product_base][:category_id] = []
  self.categories.each { |category| we_chat_data[:product_base][:category_id] << category.we_chat_id }

  we_chat_data[:product_base][:property] = []
  self.category_property_items.each { |property_item| we_chat_data[:product_base][:property] << { id: property_item.category_property.we_chat_id, vid: property_item.we_chat_id } }

  we_chat_data[:product_base][:sku_list] = []
  we_chat_data[:product_base][:sku_info] = []
  self.skus.each do |sku|
    we_chat_data[:product_base][:sku_list] << sku.to_we_chat_data

    if sku.sku_definition_items.length > 0
      sku.sku_definition_items.each do |sku_definition_item|
        index = we_chat_data[:product_base][:sku_info].index { |item| item[:id] == sku_definition_item.sku_definition.we_chat_id }
        sku_info_data = {id: sku_definition_item.sku_definition.we_chat_id, vid: []}
        if index && index >= 0
          sku_info_data = we_chat_data[:product_base][:sku_info][index]
        else
          we_chat_data[:product_base][:sku_info] << sku_info_data
        end

        sku_info_data[:vid] << sku_definition_item.we_chat_id unless sku_info_data[:vid].include?(sku_definition_item.we_chat_id)
      end
    end
  end

  we_chat_data
end