Class: Magento::Product

Inherits:
Model
  • Object
show all
Defined in:
lib/magento/product.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

api_resource, create, #delete, delete, entity_name, find, #id, #save, update, #update

Methods included from ModelParser

included, #to_h

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *params, &block) ⇒ Object



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

def method_missing(m, *params, &block)
  attr(m) || super(m, *params, &block)
end

Class Method Details

.add_media(sku, attributes) ⇒ Object

Create new gallery entry

Example:

Magento::Product.add_media('sku', {
  media_type: 'image',
  label: 'Image title',
  position: 1,
  content: {
    base64_encoded_data: 'image-string-base64',
    type: 'image/jpg',
    name: 'filename.jpg'
  },
  types: ['image']
})

Or you can use the Magento::Params::CreateImage helper class

params = Magento::Params::CreateImage.new(
  title: 'Image title',
  path: '/path/to/image.jpg', # or url
  position: 1,
).to_h

Magento::Product.add_media('sku', params)


170
171
172
# File 'lib/magento/product.rb', line 170

def add_media(sku, attributes)
  request.post("products/#{sku}/media", { entry: attributes }).parse
end

.add_tier_price(sku, price, quantity:, customer_group_id: :all) ⇒ Boolean

Add price on product sku for specified customer_group_id

Param quantity is the minimun amount to apply the price

Returns:

  • (Boolean)


184
185
186
187
188
# File 'lib/magento/product.rb', line 184

def add_tier_price(sku, price, quantity:, customer_group_id: :all)
  request.post(
    "products/#{sku}/group-prices/#{customer_group_id}/tiers/#{quantity}/price/#{price}"
  ).parse
end

Assign a product link to another product

Product.create_links('product-sku', [
  {
    link_type: 'upsell',
    linked_product_sku: 'linked_product_sku',
    linked_product_type: 'simple',
    position: position,
    sku: 'product-sku'
  }
])


225
226
227
# File 'lib/magento/product.rb', line 225

def create_links(sku, product_links)
  request.post("products/#{sku}/links", { items: product_links })
end


229
230
231
# File 'lib/magento/product.rb', line 229

def remove_link(sku, link_type:, linked_product_sku:)
  request.delete("products/#{sku}/links/#{link_type}/#{linked_product_sku}")
end

.remove_media(sku, media_id) ⇒ Object

returns true if the media was deleted



175
176
177
# File 'lib/magento/product.rb', line 175

def remove_media(sku, media_id)
  request.delete("products/#{sku}/media/#{media_id}").parse
end

.remove_tier_price(sku, quantity:, customer_group_id: :all) ⇒ Boolean

Remove tier price

Product.remove_tier_price('sku', quantity: 1, customer_group_id: :all)

Returns:

  • (Boolean)


195
196
197
198
199
# File 'lib/magento/product.rb', line 195

def remove_tier_price(sku, quantity:, customer_group_id: :all)
  request.delete(
    "products/#{sku}/group-prices/#{customer_group_id}/tiers/#{quantity}"
  ).parse
end

.update_stock(sku, id, attributes) ⇒ Object

Update product stock

Magento::Product.update_stock(sku, id, {
  qty: 12,
  is_in_stock: true 
})

see all available attributes in: magento.redoc.ly/2.4.1-admin/tag/productsproductSkustockItemsitemId



209
210
211
# File 'lib/magento/product.rb', line 209

def update_stock(sku, id, attributes)
  request.put("products/#{sku}/stockItems/#{id}", stockItem: attributes).parse
end

Instance Method Details

#add_media(attributes) ⇒ Object

Create new gallery entry

Example:

product = Magento::Product.find('sku')

product.add_media(
  media_type: 'image',
  label: 'Image label',
  position: 1,
  content: {
    base64_encoded_data: 'image-string-base64',
    type: 'image/jpg',
    name: 'filename.jpg'
  },
  types: ['image']
)

Or you can use the Magento::Params::CreateImage helper class

params = Magento::Params::CreateImage.new(
  title: 'Image title',
  path: '/path/to/image.jpg', # or url
  position: 1,
).to_h

product.add_media(params)


69
70
71
# File 'lib/magento/product.rb', line 69

def add_media(attributes)
  self.class.add_media(sku, attributes)
end

#add_tier_price(price, quantity:, customer_group_id: :all) ⇒ Boolean

Add price on product sku for specified customer_group_id

Param quantity is the minimun amount to apply the price

product = Magento::Product.find(1) product.add_tier_price(3.99, quantity: 1, customer_group_id: :all)

OR

Magento::Product.add_tier_price(1, 3.99, quantity: 1, customer_group_id: :all)

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/magento/product.rb', line 90

def add_tier_price(price, quantity:, customer_group_id: :all)
  self.class.add_tier_price(
    sku, price, quantity: quantity, customer_group_id: customer_group_id
  )
end

#attr(attribute_code) ⇒ Object

returns custom_attribute value by custom_attribute code return nil if custom_attribute is not present



19
20
21
# File 'lib/magento/product.rb', line 19

def attr(attribute_code)
  @custom_attributes&.find { |a| a.attribute_code == attribute_code.to_s }&.value
end

Assign a product link to another product

product = Magento::Product.find('sku')

product.create_links([
  {
    link_type: 'upsell',
    linked_product_sku: 'linked_product_sku',
    linked_product_type: 'simple',
    position: position,
    sku: 'product-sku'
  }
])


133
134
135
# File 'lib/magento/product.rb', line 133

def create_links(product_links)
  self.class.create_links(sku, product_links)
end


137
138
139
# File 'lib/magento/product.rb', line 137

def remove_link(link_type:, linked_product_sku:)
  self.class.remove_link(sku, link_type: link_type, linked_product_sku: linked_product_sku)
end

#remove_media(media_id) ⇒ Object

returns true if the media was deleted



74
75
76
# File 'lib/magento/product.rb', line 74

def remove_media(media_id)
  self.class.remove_media(sku, media_id)
end

#remove_tier_price(quantity:, customer_group_id: :all) ⇒ Boolean

Remove tier price

product = Magento::Product.find(1)
product.remove_tier_price(quantity: 1, customer_group_id: :all)

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/magento/product.rb', line 103

def remove_tier_price(quantity:, customer_group_id: :all)
  self.class.remove_tier_price(
    sku, quantity: quantity, customer_group_id: customer_group_id
  )
end

#respond_to?(attribute_code) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/magento/product.rb', line 37

def respond_to?(attribute_code)
  super || @custom_attributes&.any? { |a| a.attribute_code == attribute_code.to_s }
end

#set_custom_attribute(code, value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/magento/product.rb', line 23

def set_custom_attribute(code, value)
  @custom_attributes ||= []
  attribute = @custom_attributes.find { |a| a.attribute_code == code.to_s }

  if attribute
    attribute.value = value
  else
    @custom_attributes << Magento::CustomAttribute.build(
      attribute_code: code.to_s,
      value: value
    )
  end
end

#stockObject



9
10
11
# File 'lib/magento/product.rb', line 9

def stock
  extension_attributes&.stock_item
end

#stock_quantityObject



13
14
15
# File 'lib/magento/product.rb', line 13

def stock_quantity
  stock&.qty
end

#update_stock(attributes) ⇒ Object

Update product stock

product = Magento::Product.find('sku')
product.update_stock(qty: 12, is_in_stock: true)

see all available attributes in: magento.redoc.ly/2.4.1-admin/tag/productsproductSkustockItemsitemId



115
116
117
# File 'lib/magento/product.rb', line 115

def update_stock(attributes)
  self.class.update_stock(sku, id, attributes)
end