Magento Ruby library
Install
Add in your Gemfile
gem 'magento', '~> 0.4.3'
or run
gem install magento
Setup
Magento.url = 'https://yourstore.com'
Magento.token = 'MAGENTO_API_KEY'
Magento.store = :default # optional, Default is :all
Models
Magento::Product
Magento::Order
Magento::Country
Magento::Category
Get details
Magento::Product.find('sku-test')
Magento::Order.find(25)
Magento::Country.find('BR')
* same pattern to all models
Outside pattern
Get customer by token
Magento::Customer.find_by_token('user_token')
Get List
Magento::Product.all
Select fields:
Magento::Product.select(:id, :sku, :name).all
Magento::Product.select(:id, :sku, :name, extension_attributes: :category_links).all
Magento::Product.select(:id, :sku, :name, extension_attributes: [:category_links, :website_ids]).all
Magento::Product.select(:id, :sku, :name, extension_attributes: [:website_ids, { category_links: :category_id }]).all
Filters:
Magento::Product.where(name_like: 'IPhone%').all
Magento::Product.where(price_gt: 100).all
# price > 10 AND price < 20
Magento::Product.where(price_gt: 10)
.where(price_lt: 20).all
# price < 1 OR price > 100
Magento::Product.where(price_lt: 1, price_gt: 100).all
Magento::Order.where(status_in: [:canceled, :complete]).all
Condition | Notes |
---|---|
eq | Equals. |
finset | A value within a set of values |
from | The beginning of a range. Must be used with to |
gt | Greater than |
gteq | Greater than or equal |
in | In. The value is an array |
like | Like. The value can contain the SQL wildcard characters when like is specified. |
lt | Less than |
lteq | Less than or equal |
moreq | More or equal |
neq | Not equal |
nfinset | A value that is not within a set of values |
nin | Not in. The value is an array |
notnull | Not null |
null | Null |
to | The end of a range. Must be used with from |
SortOrder:
Magento::Product.order(:sku).all
Magento::Product.order(sku: :desc).all
Magento::Product.order(status: :desc, name: :asc).all
Pagination:
# Set page and quantity per page
Magento::Product.page(1) # Current page, Default is 1
.page_size(25) # Default is 50
.all
# per is an alias to page_size
Magento::Product.per(25).all
Example of several options together:
products = Magento::Product.select(:sku, :name)
.where(name_like: 'biscoito%')
.page(1)
.page_size(5)
.all
* same pattern to all models
Response
The all
method retorns a Magento::RecordCollection
instance
products.first
>> <Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
products[0]
>> <Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
products.last
>> <Magento::Product @sku="964", @name="Biscoito Negresco 140 G Original">
products.map(&:sku)
>> ["2100", "792", "836", "913", "964"]
products.size
>> 5
products.current_page
>> 1
products.page_size
>> 5
products.total_count
>> 307
products.filter_groups
>> [<Magento::FilterGroup @filters=[<Magento::Filter @field="name", @value="biscoito%", @condition_type="like">]>]
All Methods:
# Information about search criteria
:current_page
:page_size
:total_count
:filter_groups
# Iterating with the list of items
:count
:length
:size
:first
:last
:[]
:find
:each
:each_with_index
:sample
:map
:select
:filter
:reject
:collect
:take
:take_while
:sort
:sort_by
:reverse_each
:reverse
:all?
:any?
:none?
:one?
:empty?
Create
Magento::Order.create(
customer_firstname: '',
customer_lastname: '',
customer_email: '',
# others attrbutes ...,
items: [
{
sku: '',
price: '',
qty_ordered: 1,
# others attrbutes ...,
}
],
billing_address: {
# attrbutes...
},
payment: {
# attrbutes...
},
extension_attributes: {
# attrbutes...
}
)
Update
product = Magento::Product.find('sku-teste')
product.name = 'Updated name'
product.save
# or
product.update(name: 'Updated name')
Delete
product = Magento::Product.find('sku-teste')
product.delete
# or
Magento::Product.delete('sku-teste')
TODO:
Search products
Magento::Product.search('tshort')