Class: Magento::Params::CreateProduct

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/magento/params/create_product.rb

Overview

Example

params = Magento::Params::CreateProduct.new(
  sku: '556-teste-builder',
  name: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
  description: 'Descrição do produto',
  brand: 'Coca-Cola',
  price: 4.99,
  special_price: 3.49,
  quantity: 2,
  weight: 0.3,
  attribute_set_id: 4,
  images: [
    *Magento::Params::CreateImage.new(
      path: 'https://urltoimage.com/image.jpg',
      title: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
      position: 0,
      main: true
    ).variants, # it's generate all variants thumbnail => '100x100', small_image => '300x300' and image => '800x800'
    Magento::Params::CreateImage.new(
      path: '/path/to/image.jpg',
      title: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
      position: 1
    )
  ]
)

Magento::Product.create(params.to_h)

Constant Summary collapse

ProductTypes =
Type::String.default('simple'.freeze).enum(
  'simple',
  'bundle',
  'configurable',
  'downloadable',
  'grouped',
  'Virtual'
)
Visibilities =
Type::String.default('catalog_and_search'.freeze).enum(
  'not_visible_individually' => 1,
  'catalog' => 1,
  'search' => 3,
  'catalog_and_search' => 4
)
Statuses =
Type::String.default('enabled'.freeze).enum('enabled' => 1, 'disabled' => 2)

Instance Method Summary collapse

Instance Method Details

#categoriesObject



143
144
145
# File 'lib/magento/params/create_product.rb', line 143

def categories
  category_ids.map { |c| { "category_id": c, "position": 0 } }
end

#custom_attributesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/magento/params/create_product.rb', line 127

def custom_attributes
  default_attributes = [
    CustomAttribute.new(attribute_code: 'description', value: description),
    CustomAttribute.new(attribute_code: 'url_key', value: name.parameterize ),
    CustomAttribute.new(attribute_code: 'featured', value: featured)
  ]

  default_attributes.push(CustomAttribute.new(attribute_code: 'product_brand', value: brand)) if brand

  if special_price.to_f > 0
    default_attributes << CustomAttribute.new(attribute_code: 'special_price', value: special_price.to_s)
  end

  default_attributes + orig_custom_attributes
end

#orig_custom_attributesObject



77
# File 'lib/magento/params/create_product.rb', line 77

alias orig_custom_attributes custom_attributes

#stockObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/magento/params/create_product.rb', line 99

def stock
  {
    qty: quantity,
    is_in_stock: quantity.to_i > 0,
    is_qty_decimal: is_qty_decimal,
    show_default_notification_message: false,
    use_config_min_qty: true,
    min_qty: 1,
    use_config_min_sale_qty: 0,
    min_sale_qty: 0,
    use_config_max_sale_qty: true,
    max_sale_qty: 0,
    use_config_backorders: true,
    backorders: 0,
    use_config_notify_stock_qty: true,
    notify_stock_qty: 0,
    use_config_qty_increments: true,
    qty_increments: 0,
    use_config_enable_qty_inc: true,
    enable_qty_increments: true,
    use_config_manage_stock: manage_stock,
    manage_stock: manage_stock,
    low_stock_date: 'string',
    is_decimal_divided: is_qty_decimal,
    stock_status_changed_auto: 0
  }
end

#to_hObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/magento/params/create_product.rb', line 79

def to_h
  {
    sku: sku,
    name: name.titlecase,
    price: price,
    status: Statuses.mapping[status],
    visibility: Visibilities.mapping[visibility],
    type_id: type_id,
    weight: weight,
    attribute_set_id: attribute_set_id,
    extension_attributes: {
      website_ids: website_ids,
      category_links: categories,
      stock_item: stock
    },
    media_gallery_entries: images.map(&:to_h),
    custom_attributes: custom_attributes.map(&:to_h)
  }
end