Class: Prestashop::Mapper::Product

Inherits:
Model
  • Object
show all
Defined in:
lib/prestashop/mapper/models/product.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#hash_lang, #meta_description, #meta_keywords, #meta_title, model, resource

Methods included from Extension

included

Constructor Details

#initialize(args = {}) ⇒ Product

Returns a new instance of Product.


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

def initialize args = {}
  @id                         = args[:id]
  @id_manufacturer            = args[:id_manufacturer]
  @id_supplier                = args.fetch(:id_supplier)
  @id_category_default        = args[:id_category_default]
  @new                        = args[:new]
  @cache_default_attribute    = args.fetch(:cache_default_attribute, 0)
  @id_tax_rules_group         = args[:id_tax_rules_group]
  @position_in_category       = args.fetch(:position_in_category, 0)
  @manufacturer_name          = args[:manufacturer_name]
  @quantity                   = args[:quantity]
  @type                       = args.fetch(:type, 'simple')
  @id_shop_default            = args.fetch(:id_shop_default, 1)
  @reference                  = args.fetch(:reference)
  @supplier_reference         = args[:supplier_reference]
  @location                   = args[:location]
  @width                      = args[:width]
  @height                     = args[:height]
  @depth                      = args[:depth]
  @weight                     = args[:weight]
  @quantity_discount          = args.fetch(:quantity_discount, 0)
  @ean13                      = args[:ean13]
  @upc                        = args[:upc]
  @cache_is_pack              = args.fetch(:cache_is_pack, 0)
  @cache_has_attachment       = args.fetch(:cache_has_attachment, 0)
  @is_virtual                 = args.fetch(:is_virtual, 0)
  @on_sale                    = args.fetch(:on_sale, 0)
  @online_only                = args.fetch(:online_only, 0)
  @ecotax                     = args.fetch(:ecotax, 0)
  @minimal_quantity           = args.fetch(:minimal_quantity, 1)
  @price                      = args[:price]
  @wholesale_price            = args[:wholesale_price]
  @unity                      = args[:unity]
  @unit_price_ratio           = args[:unit_price_ratio]
  @additional_shipping_cost   = args[:additional_shipping_cost]
  @customizable               = args[:customizable]
  @text_fields                = args[:text_fields]
  @uploadable_files           = args[:uploadable_files]
  @active                     = args[:active]
  @redirect_type              = args[:redirect_type]
  @id_product_redirect        = args[:id_product_redirect]
  @available_for_order        = args[:available_for_order]
  @available_date             = args.fetch(:available_date, Date.today.strftime("%F"))
  @condition                  = args.fetch(:condition, 'new')
  @show_price                 = args[:show_price]
  @indexed                    = 0
  @visibility                 = args.fetch(:visibility, 'both')
  @advanced_stock_management  = args[:advanced_stock_management]
  # date_add
  # date_upd
  @meta_description           = args[:meta_description]
  @meta_keywords              = args[:meta_keywords]
  @meta_title                 = args[:meta_title]
  @link_rewrite               = args[:link_rewrite]
  @name                       = args.fetch(:name)
  @description                = args[:description]
  @description_short          = args[:description_short]
  @available_now              = args[:available_now]
  @available_later            = args[:available_later]

  @id_lang                    = args.fetch(:id_lang)
  @id_categories              = args[:id_categories]
  @id_features                = args[:id_features]
end

Instance Attribute Details

#activeObject

Returns the value of attribute active


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def active
  @active
end

#additional_shipping_costObject

Returns the value of attribute additional_shipping_cost


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def additional_shipping_cost
  @additional_shipping_cost
end

#advanced_stock_managementObject

Returns the value of attribute advanced_stock_management


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def advanced_stock_management
  @advanced_stock_management
end

#available_dateObject

Returns the value of attribute available_date


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def available_date
  @available_date
end

#available_for_orderObject

Returns the value of attribute available_for_order


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def available_for_order
  @available_for_order
end

#available_laterObject


105
106
107
# File 'lib/prestashop/mapper/models/product.rb', line 105

def available_later
  @available_later.plain.truncate(125)
end

#available_nowObject


101
102
103
# File 'lib/prestashop/mapper/models/product.rb', line 101

def available_now
  @available_now.plain.truncate(125)
end

#cache_default_attributeObject

Returns the value of attribute cache_default_attribute


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def cache_default_attribute
  @cache_default_attribute
end

#cache_has_attachmentObject

Returns the value of attribute cache_has_attachment


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def cache_has_attachment
  @cache_has_attachment
end

#cache_is_packObject

Returns the value of attribute cache_is_pack


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def cache_is_pack
  @cache_is_pack
end

#conditionObject

Returns the value of attribute condition


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def condition
  @condition
end

#customizableObject

Returns the value of attribute customizable


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def customizable
  @customizable
end

#depthObject

Returns the value of attribute depth


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def depth
  @depth
end

#descriptionObject

Returns the value of attribute description


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def description
  @description
end

#description_shortObject


85
86
87
# File 'lib/prestashop/mapper/models/product.rb', line 85

def description_short
  @description_short ? @description_short.restricted.truncate(252) : (description.truncate(252) if description)
end

#ean13Object

Returns the value of attribute ean13


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def ean13
  @ean13
end

#ecotaxObject

Returns the value of attribute ecotax


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def ecotax
  @ecotax
end

#heightObject

Returns the value of attribute height


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def height
  @height
end

#idObject Also known as: find?

Find product by reference and id_supplier, returns id


152
153
154
# File 'lib/prestashop/mapper/models/product.rb', line 152

def id
  @id ||= self.class.find_by 'filter[reference]' => reference, 'filter[id_supplier]' => id_supplier
end

#id_categoriesObject

Returns the value of attribute id_categories


13
14
15
# File 'lib/prestashop/mapper/models/product.rb', line 13

def id_categories
  @id_categories
end

#id_category_defaultObject

Returns the value of attribute id_category_default


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def id_category_default
  @id_category_default
end

#id_featuresObject

Returns the value of attribute id_features


13
14
15
# File 'lib/prestashop/mapper/models/product.rb', line 13

def id_features
  @id_features
end

#id_langObject

Returns the value of attribute id_lang


13
14
15
# File 'lib/prestashop/mapper/models/product.rb', line 13

def id_lang
  @id_lang
end

#id_manufacturerObject

Returns the value of attribute id_manufacturer


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def id_manufacturer
  @id_manufacturer
end

#id_product_redirectObject

Returns the value of attribute id_product_redirect


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def id_product_redirect
  @id_product_redirect
end

#id_shop_defaultObject

Returns the value of attribute id_shop_default


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def id_shop_default
  @id_shop_default
end

#id_supplierObject

Returns the value of attribute id_supplier


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def id_supplier
  @id_supplier
end

#id_tax_rules_groupObject

Returns the value of attribute id_tax_rules_group


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def id_tax_rules_group
  @id_tax_rules_group
end

#indexedObject

Returns the value of attribute indexed


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def indexed
  @indexed
end

#is_virtualObject

Returns the value of attribute is_virtual


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def is_virtual
  @is_virtual
end

89
90
91
# File 'lib/prestashop/mapper/models/product.rb', line 89

def link_rewrite
  @link_rewrite ? @link_rewrite.parameterize : name.parameterize
end

#locationObject

Returns the value of attribute location


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def location
  @location
end

#manufacturer_nameObject

Returns the value of attribute manufacturer_name


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def manufacturer_name
  @manufacturer_name
end

#meta_description=(value) ⇒ Object (writeonly)

Sets the attribute meta_description

Parameters:

  • value

    the value to set the attribute meta_description to.


14
15
16
# File 'lib/prestashop/mapper/models/product.rb', line 14

def meta_description=(value)
  @meta_description = value
end

#meta_keywords=(value) ⇒ Object (writeonly)

Sets the attribute meta_keywords

Parameters:

  • value

    the value to set the attribute meta_keywords to.


14
15
16
# File 'lib/prestashop/mapper/models/product.rb', line 14

def meta_keywords=(value)
  @meta_keywords = value
end

#meta_title=(value) ⇒ Object (writeonly)

Sets the attribute meta_title

Parameters:

  • value

    the value to set the attribute meta_title to.


14
15
16
# File 'lib/prestashop/mapper/models/product.rb', line 14

def meta_title=(value)
  @meta_title = value
end

#minimal_quantityObject

Returns the value of attribute minimal_quantity


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def minimal_quantity
  @minimal_quantity
end

#nameObject


81
82
83
# File 'lib/prestashop/mapper/models/product.rb', line 81

def name
  @name.plain.truncate(125)
end

#newObject

Returns the value of attribute new


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def new
  @new
end

#on_saleObject

Returns the value of attribute on_sale


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def on_sale
  @on_sale
end

#online_onlyObject

Returns the value of attribute online_only


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def online_only
  @online_only
end

#position_in_categoryObject

Returns the value of attribute position_in_category


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def position_in_category
  @position_in_category
end

#priceObject

Returns the value of attribute price


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def price
  @price
end

#quantityObject

Returns the value of attribute quantity


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def quantity
  @quantity
end

#quantity_discountObject

Returns the value of attribute quantity_discount


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def quantity_discount
  @quantity_discount
end

#redirect_typeObject

Returns the value of attribute redirect_type


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def redirect_type
  @redirect_type
end

#referenceObject


93
94
95
96
97
98
99
# File 'lib/prestashop/mapper/models/product.rb', line 93

def reference
  if @reference and not @reference.to_s.empty?
    @reference.to_s.length > 32 ? Digest::MD5.hexdigest(@reference) : @reference
  else
    Digest::MD5.hexdigest(name)
  end
end

#show_priceObject

Returns the value of attribute show_price


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def show_price
  @show_price
end

#supplier_referenceObject

Returns the value of attribute supplier_reference


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def supplier_reference
  @supplier_reference
end

#text_fieldsObject

Returns the value of attribute text_fields


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def text_fields
  @text_fields
end

#typeObject

Returns the value of attribute type


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def type
  @type
end

#unit_price_ratioObject

Returns the value of attribute unit_price_ratio


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def unit_price_ratio
  @unit_price_ratio
end

#unityObject

Returns the value of attribute unity


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def unity
  @unity
end

#upcObject

Returns the value of attribute upc


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def upc
  @upc
end

#uploadable_filesObject

Returns the value of attribute uploadable_files


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def uploadable_files
  @uploadable_files
end

#visibilityObject

Returns the value of attribute visibility


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def visibility
  @visibility
end

#weightObject

Returns the value of attribute weight


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def weight
  @weight
end

#wholesale_priceObject

Returns the value of attribute wholesale_price


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def wholesale_price
  @wholesale_price
end

#widthObject

Returns the value of attribute width


8
9
10
# File 'lib/prestashop/mapper/models/product.rb', line 8

def width
  @width
end

Class Method Details

.deactivate(id_supplier) ⇒ Object


187
188
189
190
191
192
193
194
# File 'lib/prestashop/mapper/models/product.rb', line 187

def deactivate id_supplier
  first = (Date.today-365).strftime("%F")
  last = (Date.today-1).strftime("%F")
  products = where 'filter[date_upd]' => "[#{first},#{last}]", date: 1, 'filter[id_supplier]' => id_supplier, 'filter[active]' => 1, limit: 1000
  if products and !products.empty?
    products.map{|p| update(p, active: 0)}
  end
end

.fixed_hash(id) ⇒ Object


177
178
179
180
181
182
183
184
185
# File 'lib/prestashop/mapper/models/product.rb', line 177

def fixed_hash id
  product = find id
  product.delete(:position_in_category)
  product.delete(:manufacturer_name)
  product.delete(:quantity)
  product.delete(:type)
  product.delete(:associations)
  product
end

Instance Method Details

#feature_hash(id_feature, id_feature_value) ⇒ Object

Generate hash of single feature


163
164
165
# File 'lib/prestashop/mapper/models/product.rb', line 163

def feature_hash id_feature, id_feature_value
  { id: id_feature, id_feature_value: id_feature_value } if id_feature and id_feature_value
end

#features_hashObject

Generate hash of features


168
169
170
# File 'lib/prestashop/mapper/models/product.rb', line 168

def features_hash
  id_features.map{|f| feature_hash(f[:id_feature], f[:id_feature_value])} if id_features
end

#hashObject


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/prestashop/mapper/models/product.rb', line 109

def hash
  product = { 
    id_supplier:          id_supplier,
    id_manufacturer:      id_manufacturer,
    id_category_default:  id_category_default,
    id_shop_default:      id_shop_default,
    id_tax_rules_group:   id_tax_rules_group,
    on_sale:              on_sale,
    online_only:          online_only,
    ean13:                ean13,
    upc:                  upc,
    ecotax:               ecotax,
    minimal_quantity:     minimal_quantity,
    price:                price,
    wholesale_price:      wholesale_price,
    reference:            reference,
    active:               active,
    redirect_type:        '404',
    available_for_order:  available_for_order,
    condition:            condition,
    show_price:           show_price,
    name:                 hash_lang(name, id_lang),
    description:          hash_lang(description, id_lang),
    description_short:    hash_lang(description_short, id_lang),
    link_rewrite:         hash_lang(link_rewrite, id_lang),
    meta_title:           hash_lang(meta_title, id_lang),
    meta_description:     hash_lang(meta_description, id_lang),
    meta_keywords:        hash_lang(meta_keywords, id_lang),
    available_now:        hash_lang(available_now, id_lang),
    available_later:      hash_lang(available_later, id_lang),
    associations: {} }
  unless id_categories_all.empty?
    product[:associations][:categories] = {}
    product[:associations][:categories][:category] = hash_ids(id_categories_all)
  end
  if features_hash
    product[:associations][:product_features] = {}
    product[:associations][:product_features][:product_feature] = features_hash
  end
  product
end

#id_categories_allObject


172
173
174
# File 'lib/prestashop/mapper/models/product.rb', line 172

def id_categories_all
  [id_category_default, id_categories].flatten.uniq
end

#update(options = {}) ⇒ Object

Update product with given options


158
159
160
# File 'lib/prestashop/mapper/models/product.rb', line 158

def update options = {}
  self.class.update(id, options)
end