Class: Magenthor::Product

Inherits:
Base
  • Object
show all
Defined in:
lib/magenthor/product.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

setup

Constructor Details

#initialize(set_id) ⇒ Magenthor::Product

Initialize a new product entity based on an attribute set id

Parameters:

  • set_id (Integer, String)

    the attribute set of the new product



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/magenthor/product.rb', line 15

def initialize set_id
    attributes = Magenthor::Catalog.attribute_list set_id
    attributes.each do |attr|
        if attr["code"] != 'product_id' and attr["code"] != 'created_at'
            singleton_class.class_eval do; attr_accessor attr["code"]; end
            send(attr["code"]+"=", nil)
        end
    end
    singleton_class.class_eval do; attr_accessor "type"; end
    singleton_class.class_eval do; attr_accessor "set"; end
    self.set = set_id
    self.type = nil
    self.product_id = nil
end

Instance Attribute Details

#product_idObject

Returns the value of attribute product_id.



5
6
7
# File 'lib/magenthor/product.rb', line 5

def product_id
  @product_id
end

Class Method Details

.info(product_id, store_view = '') ⇒ Magenthor::Product

Retrieve the information about a specific product

Parameters:

  • product_id (String, Integer)

    the id of the product to get

  • store_view (String, Integer) (defaults to: '')

    Magento store view ID or code

Returns:



101
102
103
104
105
106
107
108
109
110
# File 'lib/magenthor/product.rb', line 101

def info product_id, store_view = ''
    response = commit('catalog_product.info', [product_id, store_view])
    return false if response == false
    obj = new response["set"]
    response.each do |key, value|
        obj.class.class_eval do; attr_accessor key; end unless obj.respond_to?(key)
        obj.send(key+"=", value)
    end
    return obj
end

.list(filters = [], store_view = '') ⇒ Array<Magenthor::Product>

Retrieve the list of Magento product based on filters and store view

Parameters:

  • filters (Array) (defaults to: [])

    array of filters by attributes

  • store_view (String, Integer) (defaults to: '')

    Magento store view ID or code

Returns:



86
87
88
89
90
91
92
93
94
# File 'lib/magenthor/product.rb', line 86

def list filters = [], store_view = ''
    response = commit('catalog_product.list', [filters, store_view])
    return false if response == false
    products = []
    response.each do |p|
        products << Magenthor::Product.info(p["product_id"], store_view)
    end
    return products
end

Instance Method Details

#createTrueClass, FalseClass

Create on Magento a new product base on the local one

Returns:

  • (TrueClass, FalseClass)

    true if successful or false



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/magenthor/product.rb', line 44

def create
    if self.type.nil? or self.set.nil? or self.sku.nil?
        puts "Type, set and sku are mandatory"
        return false
    end
    attributes = {}
    methods.grep(/\w=$/).each do |m|
        value =  send(m.to_s.gsub('=',''))
        attributes[m.to_s.gsub('=','')] = value unless value.nil?
    end
    response = self.class.commit('catalog_product.create', [self.type, self.set, self.sku, attributes])
    return false if response == false
    obj = self.class.info response
    methods.grep(/\w=$/).each do |m|
        send(m, obj.send(m.to_s.gsub('=','')))
    end
    self.product_id = obj.product_id
    return true
end

#deleteTrueClass, FalseClass

Delete from Magento the local product

Returns:

  • (TrueClass, FalseClass)

    true if successful or false



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/magenthor/product.rb', line 67

def delete
    response = self.class.commit('catalog_product.delete', [self.product_id])
    return false if response == false

    methods.grep(/\w=$/).each do |m|
        send(m, nil)
    end
    self.product_id = nil

    return true
end

#updateTrueClass, FalseClass

Save on Magento the updates made on the local product

Returns:

  • (TrueClass, FalseClass)

    true if successful or false



33
34
35
36
37
38
39
# File 'lib/magenthor/product.rb', line 33

def update
    attributes = {}
    methods.grep(/\w=$/).each do |m|
        attributes[m.to_s.gsub('=','')] = send(m.to_s.gsub('=',''))
    end
    self.class.commit('catalog_product.update', [self.product_id, attributes])
end