magento 0.24.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +4 -2
- data/Gemfile +5 -3
- data/README.md +636 -226
- data/lib/magento.rb +7 -1
- data/lib/magento/cart.rb +65 -0
- data/lib/magento/order.rb +47 -2
- data/lib/magento/params/create_image.rb +43 -0
- data/lib/magento/params/create_product_link.rb +30 -0
- data/lib/magento/product.rb +96 -1
- data/lib/magento/query.rb +13 -10
- data/lib/magento/sales_rule.rb +4 -0
- data/lib/magento/version.rb +1 -1
- data/spec/magento/core/model_mapper_spec.rb +74 -0
- data/spec/magento/core/model_spec.rb +71 -0
- data/spec/magento/core/query_spec.rb +207 -0
- data/spec/magento/core/record_collection_spec.rb +116 -0
- data/spec/magento/core/request_spec.rb +141 -0
- data/spec/magento/order_spec.rb +37 -0
- data/spec/{product_spec.rb → magento/product_spec.rb} +21 -0
- data/spec/spec_helper.rb +17 -3
- data/spec/vcr_cassettes/order/send_email.yml +65 -0
- data/spec/vcr_cassettes/product/find.yml +72 -0
- metadata +13 -4
- data/.rspec_status +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6aec96b1878cf98094666a1c656840c7cc1a8f7f08fea94ec597333b5ea32b9
|
4
|
+
data.tar.gz: 39959ab9be096fb11a8d0a13a48a957f5fc7c8ab78cd82014c586d3170c6e98a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4387d0771f06cc23a980a730e0c3c94f7a67fd9fb4f370e89f7ab175bb21ff1ba9047eab9f56e79466b808bb2a07cda84454839d6783a28b3ef97646f47f273
|
7
|
+
data.tar.gz: 2ba0b2f46693f6584b5406e70442f5d3dd2235fb4ed2000fdd0a43533655f4d060989cb3493c58004a8dcc3714686b3246ff9a4f5e617e6d60e56e3de71506e8
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
source
|
3
|
+
source 'https://rubygems.org'
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in magento.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
-
gem "rake", "~> 12.0"
|
9
|
-
gem "rspec", "~> 3.0"
|
10
8
|
gem 'byebug', '~> 11.1', '>= 11.1.3'
|
9
|
+
gem 'rake', '~> 12.0'
|
10
|
+
gem 'rspec', '~> 3.0'
|
11
|
+
gem 'vcr'
|
12
|
+
gem 'webmock'
|
data/README.md
CHANGED
@@ -1,11 +1,83 @@
|
|
1
|
-
# Magento Ruby library
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# Magento 2 Ruby library
|
2
|
+
|
3
|
+
Ruby library to consume the magento 2 api
|
4
|
+
|
5
|
+
> Tested in version 2.3 of magento
|
6
|
+
|
7
|
+
[Getting started](#getting-started)
|
8
|
+
- [Install](#install)
|
9
|
+
- [Setup](#setup)
|
10
|
+
|
11
|
+
[Model common methods](#model-common-methods)
|
12
|
+
- [find](#find)
|
13
|
+
- [find_by](#find_by)
|
14
|
+
- [first](#first)
|
15
|
+
- [count](#count)
|
16
|
+
- [all](#count)
|
17
|
+
- [create](#create)
|
18
|
+
- [update](#update)
|
19
|
+
- [delete](#delete)
|
20
|
+
|
21
|
+
[Search criteria](#search-criteria)
|
22
|
+
- [Select fields](#select-fields)
|
23
|
+
- [Filters](#filters)
|
24
|
+
- [Sort order](#sort-order)
|
25
|
+
- [Pagination](#pagination)
|
26
|
+
- [Record Collection](#record-collection)
|
27
|
+
|
28
|
+
**Additional methods**
|
29
|
+
|
30
|
+
[Product](#product)
|
31
|
+
- [Shurtcuts](#shurtcuts)
|
32
|
+
- [Update stock](#update-stock)
|
33
|
+
- [Add media](#add-media-to-product)
|
34
|
+
- [Remove media](#remove-media-from-product)
|
35
|
+
- [Add tier price](#add-tier-price-to-product)
|
36
|
+
- [Remove tier price](#remove-tier-price-from-product)
|
37
|
+
- [Create links](#create-links-to-product)
|
38
|
+
- [Remove link](#remove-link-from-product)
|
39
|
+
|
40
|
+
[Order](#order)
|
41
|
+
- [Invoice](#invoice-an-order)
|
42
|
+
- [Offline Refund](#create-offline-refund-for-order)
|
43
|
+
- [Creates new Shipment](#creates-new-shipment-for-given-order)
|
44
|
+
- [Cancel](#cancel-an-order)
|
45
|
+
|
46
|
+
[Invoice](#invoice)
|
47
|
+
- [Refund](#create-refund-for-invoice)
|
48
|
+
- [Capture](#capture-an-invoice)
|
49
|
+
- [Void](#void-an-invoice)
|
50
|
+
- [Send email](#send-invoice-email)
|
51
|
+
- [Get comments](#get-invoice-comments)
|
52
|
+
|
53
|
+
[Sales Rule](#sales-rule)
|
54
|
+
- [Generate Sales Rules and Coupons](#generate-sales-rules-and-coupons)
|
55
|
+
|
56
|
+
[Customer](#customer)
|
57
|
+
- [Find by token](#get-customer-by-token)
|
58
|
+
|
59
|
+
[Guest cart](#guest-cart)
|
60
|
+
- [Payment information](#payment-information)
|
61
|
+
- [Add Coupon](#add-coupon-to-guest-cart)
|
62
|
+
- [Remove Coupon](#remove-coupon-from-guest-cart)
|
63
|
+
|
64
|
+
[Inventory](#invoice)
|
65
|
+
- [Check whether a product is salable](#check-whether-a-product-is-salable)
|
66
|
+
- [Check whether a product is salable for a specified quantity](#check-whether-a-product-is-salable-for-a-specified-quantity)
|
67
|
+
|
68
|
+
**Helper classes**
|
69
|
+
|
70
|
+
- [Create product params](#create-product-params)
|
71
|
+
- [Create product image params](#create-product-image-params)
|
72
|
+
- [Import products from csv file](#import-products-from-csv-file)
|
73
|
+
|
74
|
+
## Getting started
|
75
|
+
|
76
|
+
### Install
|
5
77
|
Add in your Gemfile
|
6
78
|
|
7
79
|
```rb
|
8
|
-
gem 'magento', '~> 0.
|
80
|
+
gem 'magento', '~> 0.28.0'
|
9
81
|
```
|
10
82
|
|
11
83
|
or run
|
@@ -28,96 +100,211 @@ Magento.with_config(store: :other_store) do # accepts store, url and token param
|
|
28
100
|
end
|
29
101
|
```
|
30
102
|
|
31
|
-
##
|
32
|
-
|
33
|
-
Magento::
|
34
|
-
|
35
|
-
|
36
|
-
Magento::Category
|
37
|
-
Magento::Customer
|
38
|
-
```
|
103
|
+
## Model common methods
|
104
|
+
|
105
|
+
All classes that inherit from `Magento::Model` have the methods described below
|
106
|
+
|
107
|
+
### `find`
|
39
108
|
|
40
|
-
|
109
|
+
Get resource details with the `find` method
|
41
110
|
|
111
|
+
Example:
|
42
112
|
```rb
|
43
113
|
Magento::Product.find('sku-test')
|
44
114
|
Magento::Order.find(25)
|
45
115
|
Magento::Country.find('BR')
|
46
116
|
```
|
47
|
-
\* _same pattern to all models_
|
48
117
|
|
49
|
-
|
118
|
+
### `find_by`
|
50
119
|
|
51
|
-
|
120
|
+
Returns the first resource found based on the argument passed
|
52
121
|
|
122
|
+
Example:
|
53
123
|
```rb
|
54
|
-
Magento::
|
124
|
+
Magento::Product.find_by(name: 'Some product name')
|
125
|
+
Magento::Customer.find_by(email: 'customer@email.com')
|
55
126
|
```
|
56
127
|
|
57
|
-
|
128
|
+
### `first`
|
58
129
|
|
59
|
-
|
130
|
+
Returns the first resource found for the [search criteria](#search-criteria)
|
60
131
|
|
132
|
+
Example:
|
61
133
|
```rb
|
62
|
-
|
63
|
-
# it is the same as
|
64
|
-
product.custom_attributes.find { |a| a.attribute_code == 'description' }&.value
|
65
|
-
|
66
|
-
# or
|
67
|
-
product.description
|
134
|
+
Magento::Order.where(grand_total_gt: 100).first
|
68
135
|
```
|
69
136
|
|
70
|
-
|
137
|
+
### `count`
|
138
|
+
|
139
|
+
Returns the total amount of the resource, also being able to use it based on the [search criteria](#search-criteria)
|
71
140
|
|
141
|
+
Example:
|
72
142
|
```rb
|
73
|
-
product.attr :special_price
|
74
|
-
> nil
|
75
143
|
|
76
|
-
|
77
|
-
|
144
|
+
Magento::Order.count
|
145
|
+
>> 1500
|
146
|
+
|
147
|
+
Magento::Order.where(status: :pending).count
|
148
|
+
>> 48
|
78
149
|
```
|
79
150
|
|
151
|
+
### `all`
|
152
|
+
|
153
|
+
Used to get a list of a specific resource based on the [search criteria](#search-criteria).
|
154
|
+
|
155
|
+
Returns a [Record Collection](#record-collection)
|
156
|
+
|
157
|
+
Example:
|
80
158
|
```rb
|
81
|
-
|
82
|
-
|
159
|
+
# Default search criteria:
|
160
|
+
# page: 1
|
161
|
+
# page_size: 50
|
162
|
+
Magento::Product.all
|
83
163
|
|
84
|
-
|
85
|
-
|
164
|
+
Magento::Product
|
165
|
+
.order(created_at: :desc)
|
166
|
+
.page_size(10)
|
167
|
+
.all
|
86
168
|
```
|
87
169
|
|
88
|
-
|
170
|
+
### `create`
|
89
171
|
|
90
|
-
|
172
|
+
Creates a new resource based on reported attributes.
|
173
|
+
|
174
|
+
Consult the magento documentation for available attributes for each resource:
|
91
175
|
|
92
|
-
|
176
|
+
Documentation links:
|
177
|
+
- [Product](https://magento.redoc.ly/2.3.6-admin/tag/products#operation/catalogProductRepositoryV1SavePost)
|
178
|
+
- [Category](https://magento.redoc.ly/2.3.6-admin/tag/categories#operation/catalogCategoryRepositoryV1SavePost)
|
179
|
+
- [Order](https://magento.redoc.ly/2.3.6-admin/tag/orders#operation/salesOrderRepositoryV1SavePost)
|
180
|
+
- [Customer](https://magento.redoc.ly/2.3.6-admin/tag/customers#operation/customerAccountManagementV1CreateAccountPost)
|
93
181
|
|
182
|
+
Example:
|
94
183
|
```rb
|
95
|
-
|
96
|
-
|
97
|
-
|
184
|
+
Magento::Order.create(
|
185
|
+
customer_firstname: '',
|
186
|
+
customer_lastname: '',
|
187
|
+
customer_email: '',
|
188
|
+
# others attrbutes ...,
|
189
|
+
items: [
|
190
|
+
{
|
191
|
+
sku: '',
|
192
|
+
price: '',
|
193
|
+
qty_ordered: 1,
|
194
|
+
# others attrbutes ...,
|
195
|
+
}
|
196
|
+
],
|
197
|
+
billing_address: {
|
198
|
+
# attrbutes...
|
199
|
+
},
|
200
|
+
payment: {
|
201
|
+
# attrbutes...
|
202
|
+
},
|
203
|
+
extension_attributes: {
|
204
|
+
# attrbutes...
|
205
|
+
}
|
206
|
+
)
|
207
|
+
```
|
98
208
|
|
99
|
-
|
209
|
+
#### `update`
|
100
210
|
|
101
|
-
|
102
|
-
|
211
|
+
Update a resource attributes.
|
212
|
+
|
213
|
+
Example:
|
214
|
+
|
215
|
+
```rb
|
216
|
+
Magento::Product.update('sku-teste', name: 'Updated name')
|
217
|
+
|
218
|
+
# or by instance method
|
219
|
+
|
220
|
+
product = Magento::Product.find('sku-teste')
|
221
|
+
|
222
|
+
product.update(name: 'Updated name', status: '2')
|
223
|
+
|
224
|
+
# or save after changing the object
|
225
|
+
|
226
|
+
product.name = 'Updated name'
|
227
|
+
product.save
|
103
228
|
```
|
104
229
|
|
105
|
-
|
230
|
+
### `delete`
|
231
|
+
|
232
|
+
Delete a especific resource.
|
106
233
|
|
107
234
|
```rb
|
108
|
-
Magento::Product.
|
235
|
+
Magento::Product.delete('sku-teste')
|
236
|
+
|
237
|
+
# or
|
238
|
+
product = Magento::Product.find('sku-teste')
|
239
|
+
product.delete
|
109
240
|
```
|
110
241
|
|
111
|
-
|
242
|
+
## Search Criteria
|
243
|
+
|
244
|
+
They are methods used to assemble the search parameters
|
245
|
+
|
246
|
+
All methods return an instance of the `Magento::Query` class. The request is only executed after calling method `all`.
|
247
|
+
|
248
|
+
Example:
|
249
|
+
|
250
|
+
```rb
|
251
|
+
customers = Magento::Customer
|
252
|
+
.where(dob_gt: '1995-01-01')
|
253
|
+
.order(:dob)
|
254
|
+
.all
|
255
|
+
|
256
|
+
# or
|
257
|
+
|
258
|
+
query = Magento::Customer.where(dob_gt: '1995-01-01')
|
259
|
+
|
260
|
+
query = query.order(:dob) if ordered_by_date_of_birth
|
261
|
+
|
262
|
+
customers = query.all
|
263
|
+
```
|
264
|
+
|
265
|
+
### Select fields:
|
266
|
+
|
267
|
+
Example:
|
112
268
|
```rb
|
113
269
|
Magento::Product.select(:id, :sku, :name).all
|
114
|
-
|
115
|
-
Magento::Product
|
116
|
-
|
270
|
+
|
271
|
+
Magento::Product
|
272
|
+
.select(
|
273
|
+
:id,
|
274
|
+
:sku,
|
275
|
+
:name,
|
276
|
+
extension_attributes: :category_links
|
277
|
+
)
|
278
|
+
.all
|
279
|
+
|
280
|
+
Magento::Product
|
281
|
+
.select(
|
282
|
+
:id,
|
283
|
+
:sku,
|
284
|
+
:name,
|
285
|
+
extension_attributes: [
|
286
|
+
:category_links,
|
287
|
+
:website_ids
|
288
|
+
]
|
289
|
+
)
|
290
|
+
.all
|
291
|
+
|
292
|
+
Magento::Product
|
293
|
+
.select(
|
294
|
+
:id,
|
295
|
+
:sku,
|
296
|
+
:name,
|
297
|
+
extension_attributes: [
|
298
|
+
{ category_links: :category_id },
|
299
|
+
:website_ids
|
300
|
+
]
|
301
|
+
)
|
302
|
+
.all
|
117
303
|
```
|
118
304
|
|
119
|
-
|
305
|
+
### Filters
|
120
306
|
|
307
|
+
Example:
|
121
308
|
```rb
|
122
309
|
Magento::Product.where(visibility: 4).all
|
123
310
|
Magento::Product.where(name_like: 'IPhone%').all
|
@@ -131,6 +318,7 @@ Magento::Product.where(price_gt: 10)
|
|
131
318
|
Magento::Product.where(price_lt: 1, price_gt: 100).all
|
132
319
|
|
133
320
|
Magento::Order.where(status_in: [:canceled, :complete]).all
|
321
|
+
|
134
322
|
```
|
135
323
|
|
136
324
|
| Condition | Notes |
|
@@ -153,56 +341,37 @@ Magento::Order.where(status_in: [:canceled, :complete]).all
|
|
153
341
|
|to | The end of a range. Must be used with from |
|
154
342
|
|
155
343
|
|
156
|
-
|
344
|
+
### Sort Order
|
157
345
|
|
346
|
+
Example:
|
158
347
|
```rb
|
159
348
|
Magento::Product.order(:sku).all
|
160
349
|
Magento::Product.order(sku: :desc).all
|
161
350
|
Magento::Product.order(status: :desc, name: :asc).all
|
162
351
|
```
|
163
352
|
|
164
|
-
|
353
|
+
### Pagination:
|
165
354
|
|
355
|
+
Example:
|
166
356
|
```rb
|
167
357
|
# Set page and quantity per page
|
168
|
-
Magento::Product
|
169
|
-
|
170
|
-
|
358
|
+
Magento::Product
|
359
|
+
.page(1) # Current page, Default is 1
|
360
|
+
.page_size(25) # Default is 50
|
361
|
+
.all
|
171
362
|
|
172
363
|
# per is an alias to page_size
|
173
364
|
Magento::Product.per(25).all
|
174
365
|
```
|
175
366
|
|
176
|
-
|
177
|
-
```rb
|
178
|
-
products = Magento::Product.select(:sku, :name)
|
179
|
-
.where(name_like: 'biscoito%')
|
180
|
-
.page(1)
|
181
|
-
.page_size(5)
|
182
|
-
.all
|
183
|
-
```
|
184
|
-
|
185
|
-
## Get one
|
186
|
-
|
187
|
-
```rb
|
188
|
-
Magento::Order.where(increment_id: '000013457').first
|
189
|
-
# or
|
190
|
-
Magento::Order.find_by(increment_id: '000013457')
|
191
|
-
```
|
192
|
-
|
193
|
-
## Count
|
194
|
-
```rb
|
195
|
-
Magento::Order.count
|
196
|
-
Magento::Order.where(status: :pending).count
|
197
|
-
```
|
198
|
-
|
199
|
-
\* _same pattern to all models_
|
200
|
-
|
201
|
-
### Response
|
367
|
+
## Record Collection
|
202
368
|
|
203
369
|
The `all` method retorns a `Magento::RecordCollection` instance
|
204
370
|
|
371
|
+
Example:
|
205
372
|
```rb
|
373
|
+
products = Magento::Product.all
|
374
|
+
|
206
375
|
products.first
|
207
376
|
>> <Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
|
208
377
|
|
@@ -282,106 +451,198 @@ All Methods:
|
|
282
451
|
:empty?
|
283
452
|
```
|
284
453
|
|
285
|
-
##
|
454
|
+
## Product
|
455
|
+
|
456
|
+
### Shurtcuts
|
457
|
+
|
458
|
+
Shurtcut to get custom attribute value by custom attribute code in product
|
459
|
+
|
460
|
+
Exemple:
|
286
461
|
|
287
462
|
```rb
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
{
|
295
|
-
sku: '',
|
296
|
-
price: '',
|
297
|
-
qty_ordered: 1,
|
298
|
-
# others attrbutes ...,
|
299
|
-
}
|
300
|
-
],
|
301
|
-
billing_address: {
|
302
|
-
# attrbutes...
|
303
|
-
},
|
304
|
-
payment: {
|
305
|
-
# attrbutes...
|
306
|
-
},
|
307
|
-
extension_attributes: {
|
308
|
-
# attrbutes...
|
309
|
-
}
|
310
|
-
)
|
463
|
+
product.attr :description
|
464
|
+
# it is the same as
|
465
|
+
product.custom_attributes.find { |a| a.attribute_code == 'description' }&.value
|
466
|
+
|
467
|
+
# or
|
468
|
+
product.description
|
311
469
|
```
|
312
470
|
|
313
|
-
|
471
|
+
when the custom attribute does not exists:
|
314
472
|
|
315
473
|
```rb
|
316
|
-
product
|
474
|
+
product.attr :special_price
|
475
|
+
> nil
|
317
476
|
|
318
|
-
product.
|
319
|
-
|
477
|
+
product.special_price
|
478
|
+
> NoMethodError: undefined method `special_price' for #<Magento::Product:...>
|
479
|
+
```
|
320
480
|
|
321
|
-
|
481
|
+
```rb
|
482
|
+
product.respond_to? :special_price
|
483
|
+
> false
|
322
484
|
|
323
|
-
product.
|
485
|
+
product.respond_to? :description
|
486
|
+
> true
|
487
|
+
```
|
324
488
|
|
325
|
-
|
489
|
+
Shurtcut to get product stock and stock quantity
|
326
490
|
|
327
|
-
|
491
|
+
```rb
|
492
|
+
product = Magento::Product.find('sku')
|
493
|
+
|
494
|
+
product.stock
|
495
|
+
> <Magento::StockItem @item_id=7243, @product_id=1221, ...>
|
496
|
+
|
497
|
+
product.stock_quantity
|
498
|
+
> 22
|
328
499
|
```
|
329
500
|
|
330
|
-
###
|
501
|
+
### Update stock
|
502
|
+
|
503
|
+
Update product stock
|
331
504
|
|
332
505
|
```rb
|
333
|
-
product = Magento::Product.find('sku
|
506
|
+
product = Magento::Product.find('sku')
|
507
|
+
product.update_stock(qty: 12, is_in_stock: true)
|
334
508
|
|
335
|
-
|
509
|
+
# or through the class method
|
336
510
|
|
337
|
-
|
511
|
+
Magento::Product.update_stock('sku', id, {
|
512
|
+
qty: 12,
|
513
|
+
is_in_stock: true
|
514
|
+
})
|
515
|
+
```
|
338
516
|
|
339
|
-
Magento
|
517
|
+
> see all available attributes in: [Magento Rest Api Documentation](https://magento.redoc.ly/2.4.1-admin/tag/productsproductSkustockItemsitemId)
|
518
|
+
|
519
|
+
|
520
|
+
### Add media to product
|
521
|
+
|
522
|
+
Create new gallery entry
|
523
|
+
|
524
|
+
Example:
|
525
|
+
```rb
|
526
|
+
product = Magento::Product.find('sku')
|
527
|
+
|
528
|
+
image_params = {
|
529
|
+
media_type: 'image',
|
530
|
+
label: 'Image label',
|
531
|
+
position: 1,
|
532
|
+
content: {
|
533
|
+
base64_encoded_data: '/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAA...',
|
534
|
+
type: 'image/jpg',
|
535
|
+
name: 'filename.jpg'
|
536
|
+
},
|
537
|
+
types: ['image']
|
538
|
+
}
|
539
|
+
|
540
|
+
product.add_media(image_params)
|
541
|
+
|
542
|
+
# or through the class method
|
543
|
+
|
544
|
+
Magento::Product.add_media('sku', image_params)
|
340
545
|
```
|
546
|
+
> see all available attributes in: [Magento Rest Api Documentation](https://magento.redoc.ly/2.3.6-admin/#operation/catalogProductAttributeMediaGalleryManagementV1CreatePost)
|
341
547
|
|
342
|
-
## GuestCart
|
343
548
|
|
344
|
-
|
549
|
+
you can also use the `Magento::Params::CreateImage` helper class
|
550
|
+
|
345
551
|
```rb
|
346
|
-
|
552
|
+
params = Magento::Params::CreateImage.new(
|
553
|
+
title: 'Image title',
|
554
|
+
path: '/path/to/image.jpg', # or url
|
555
|
+
position: 1,
|
556
|
+
).to_h
|
347
557
|
|
348
|
-
|
349
|
-
|
350
|
-
cart_id: 'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN'
|
351
|
-
)
|
558
|
+
product.add_media(params)
|
559
|
+
```
|
352
560
|
|
353
|
-
|
354
|
-
email: 'customer@gmail.com',
|
355
|
-
payment: { method: 'cashondelivery' }
|
356
|
-
)
|
561
|
+
> see more about [Magento::Params::CreateImage](lib/magento/params/create_image.rb#L9)
|
357
562
|
|
358
|
-
|
563
|
+
### Remove media from product
|
564
|
+
|
565
|
+
Example:
|
566
|
+
```rb
|
567
|
+
product = Magento::Product.find('sku')
|
568
|
+
|
569
|
+
product.add_media(madia_id)
|
570
|
+
|
571
|
+
# or through the class method
|
572
|
+
|
573
|
+
Magento::Product.add_media('sku', madia_id)
|
359
574
|
```
|
360
575
|
|
361
|
-
Add
|
576
|
+
### Add tier price to product
|
577
|
+
|
578
|
+
Add `price` on product `sku` for specified `customer_group_id`
|
579
|
+
|
580
|
+
The `quantity` params is the minimun amount to apply the price
|
362
581
|
```rb
|
363
|
-
|
582
|
+
product = Magento::Product.find('sku')
|
583
|
+
product.add_tier_price(3.99, quantity: 1, customer_group_id: :all)
|
364
584
|
|
365
|
-
|
366
|
-
# You can also use the class method
|
367
|
-
Magento::GuestCart.add_coupon('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7', 'COAU4HXE0I')
|
585
|
+
# or through the class method
|
368
586
|
|
369
|
-
|
587
|
+
Magento::Product.add_tier_price('sku', 3.99, quantity: 1, customer_group_id: :all)
|
370
588
|
```
|
371
589
|
|
372
|
-
|
590
|
+
### Remove tier price from product
|
591
|
+
|
373
592
|
```rb
|
374
|
-
|
593
|
+
product = Magento::Product.find(1)
|
594
|
+
product.remove_tier_price(quantity: 1, customer_group_id: :all)
|
375
595
|
|
376
|
-
|
377
|
-
# You can also use the class method
|
378
|
-
Magento::GuestCart.delete_coupon('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
|
596
|
+
# or through the class method
|
379
597
|
|
380
|
-
|
598
|
+
Magento::Product.remove_tier_price('sku', quantity: 1, customer_group_id: :all)
|
599
|
+
```
|
600
|
+
|
601
|
+
### Create links to product
|
602
|
+
|
603
|
+
Assign a product link to another product
|
604
|
+
|
605
|
+
Example:
|
606
|
+
```rb
|
607
|
+
product = Magento::Product.find('sku')
|
608
|
+
|
609
|
+
link_param = {
|
610
|
+
link_type: 'upsell',
|
611
|
+
linked_product_sku: 'linked_product_sku',
|
612
|
+
linked_product_type: 'simple',
|
613
|
+
position: 1,
|
614
|
+
sku: 'sku'
|
615
|
+
}
|
616
|
+
|
617
|
+
product.create_links([link_param])
|
618
|
+
|
619
|
+
# or through the class method
|
620
|
+
|
621
|
+
Product.create_links('sku', [link_param])
|
622
|
+
```
|
623
|
+
|
624
|
+
### Remove link from product
|
625
|
+
|
626
|
+
Example:
|
627
|
+
```rb
|
628
|
+
product = Magento::Product.find('sku')
|
629
|
+
|
630
|
+
product.remove_link(link_type: 'simple', linked_product_sku: 'linked_product_sku')
|
631
|
+
|
632
|
+
# or through the class method
|
633
|
+
|
634
|
+
Product.remove_link(
|
635
|
+
'sku',
|
636
|
+
link_type: 'simple',
|
637
|
+
linked_product_sku: 'linked_product_sku'
|
638
|
+
)
|
381
639
|
```
|
382
640
|
|
383
|
-
##
|
641
|
+
## Order
|
642
|
+
|
643
|
+
### Invoice an Order
|
384
644
|
|
645
|
+
Example:
|
385
646
|
```rb
|
386
647
|
Magento::Order.invoice(order_id)
|
387
648
|
>> 25 # return incoice id
|
@@ -409,21 +670,22 @@ invoice_id = order.invoice(
|
|
409
670
|
|
410
671
|
[Complete Invoice Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdinvoice#operation/salesInvoiceOrderV1ExecutePost)
|
411
672
|
|
412
|
-
|
673
|
+
### Create offline refund for order
|
413
674
|
|
675
|
+
Example:
|
414
676
|
```rb
|
415
|
-
Magento::
|
677
|
+
Magento::Order.refund(order_id)
|
416
678
|
>> 12 # return refund id
|
417
679
|
|
418
680
|
# or from instance
|
419
681
|
|
420
|
-
|
682
|
+
order = Magento::Order.find(order_id)
|
421
683
|
|
422
|
-
|
684
|
+
order.refund
|
423
685
|
|
424
686
|
# you can pass parameters too
|
425
687
|
|
426
|
-
|
688
|
+
order.refund(
|
427
689
|
items: [
|
428
690
|
{
|
429
691
|
extension_attributes: {},
|
@@ -431,7 +693,6 @@ invoice.refund(
|
|
431
693
|
qty: 0
|
432
694
|
}
|
433
695
|
],
|
434
|
-
isOnline: true,
|
435
696
|
notify: true,
|
436
697
|
appendComment: true,
|
437
698
|
comment: {
|
@@ -450,24 +711,93 @@ invoice.refund(
|
|
450
711
|
)
|
451
712
|
```
|
452
713
|
|
453
|
-
[Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/
|
714
|
+
[Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundOrderV1ExecutePost)
|
715
|
+
|
716
|
+
### Creates new Shipment for given Order.
|
717
|
+
|
718
|
+
Example:
|
719
|
+
```rb
|
720
|
+
Magento::Order.ship(order_id)
|
721
|
+
>> 25 # return shipment id
|
722
|
+
|
723
|
+
# or from instance
|
724
|
+
|
725
|
+
order = Magento::Order.find(order_id)
|
454
726
|
|
727
|
+
order.ship
|
455
728
|
|
456
|
-
|
729
|
+
# you can pass parameters too
|
457
730
|
|
731
|
+
order.ship(
|
732
|
+
capture: false,
|
733
|
+
appendComment: true,
|
734
|
+
items: [{ order_item_id: 123, qty: 1 }], # pass items to partial shipment
|
735
|
+
tracks: [
|
736
|
+
{
|
737
|
+
extension_attributes: { },
|
738
|
+
track_number: "string",
|
739
|
+
title: "string",
|
740
|
+
carrier_code: "string"
|
741
|
+
}
|
742
|
+
]
|
743
|
+
notify: true
|
744
|
+
)
|
745
|
+
```
|
746
|
+
|
747
|
+
[Complete Shipment Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdship#operation/salesShipOrderV1ExecutePost)
|
748
|
+
|
749
|
+
|
750
|
+
### Cancel an Order
|
751
|
+
|
752
|
+
Example:
|
458
753
|
```rb
|
459
|
-
Magento::Order.
|
754
|
+
order = Magento::Order.find(order_id)
|
755
|
+
|
756
|
+
order.cancel # or
|
757
|
+
|
758
|
+
Magento::Order.cancel(order_id)
|
759
|
+
```
|
760
|
+
|
761
|
+
### Add a comment on given Order
|
762
|
+
|
763
|
+
Example:
|
764
|
+
```rb
|
765
|
+
order = Magento::Order.find(order_id)
|
766
|
+
|
767
|
+
order.add_comment(
|
768
|
+
'comment',
|
769
|
+
is_customer_notified: 0,
|
770
|
+
is_visible_on_front: 1
|
771
|
+
)
|
772
|
+
|
773
|
+
# or
|
774
|
+
|
775
|
+
Magento::Order.add_comment(
|
776
|
+
order_id,
|
777
|
+
'comment',
|
778
|
+
is_customer_notified: 0,
|
779
|
+
is_visible_on_front: 1
|
780
|
+
)
|
781
|
+
```
|
782
|
+
|
783
|
+
## Invoice
|
784
|
+
|
785
|
+
### Create refund for invoice
|
786
|
+
|
787
|
+
Example:
|
788
|
+
```rb
|
789
|
+
Magento::Invoice.invoice(invoice_id)
|
460
790
|
>> 12 # return refund id
|
461
791
|
|
462
792
|
# or from instance
|
463
793
|
|
464
|
-
|
794
|
+
invoice = Magento::Invoice.find(invoice_id)
|
465
795
|
|
466
|
-
|
796
|
+
refund_id = invoice.refund
|
467
797
|
|
468
798
|
# you can pass parameters too
|
469
799
|
|
470
|
-
|
800
|
+
invoice.refund(
|
471
801
|
items: [
|
472
802
|
{
|
473
803
|
extension_attributes: {},
|
@@ -475,6 +805,7 @@ order.refund(
|
|
475
805
|
qty: 0
|
476
806
|
}
|
477
807
|
],
|
808
|
+
isOnline: true,
|
478
809
|
notify: true,
|
479
810
|
appendComment: true,
|
480
811
|
comment: {
|
@@ -493,70 +824,52 @@ order.refund(
|
|
493
824
|
)
|
494
825
|
```
|
495
826
|
|
496
|
-
[Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/
|
827
|
+
[Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundInvoiceV1ExecutePost)
|
497
828
|
|
498
|
-
|
829
|
+
### Capture an invoice
|
499
830
|
|
831
|
+
Example:
|
500
832
|
```rb
|
501
833
|
invoice = Magento::Invoice.find(invoice_id)
|
834
|
+
invoice.capture
|
502
835
|
|
503
|
-
|
836
|
+
# or through the class method
|
504
837
|
Magento::Invoice.capture(invoice_id)
|
505
|
-
|
506
|
-
invoice.void # or
|
507
|
-
Magento::Invoice.void(invoice_id)
|
508
|
-
|
509
|
-
invoice.send_email # or
|
510
|
-
Magento::Invoice.send_email(invoice_id)
|
511
|
-
|
512
|
-
Magento::Invoice.comments(invoice_id).all
|
513
|
-
Magento::Invoice.comments(invoice_id).where(created_at_gt: Date.today.prev_day).all
|
514
838
|
```
|
515
839
|
|
516
|
-
|
840
|
+
### Void an invoice
|
517
841
|
|
842
|
+
Example:
|
518
843
|
```rb
|
519
|
-
Magento::
|
520
|
-
|
521
|
-
|
522
|
-
# or from instance
|
844
|
+
invoice = Magento::Invoice.find(invoice_id)
|
845
|
+
invoice.void
|
523
846
|
|
524
|
-
|
847
|
+
# or through the class method
|
848
|
+
Magento::Invoice.void(invoice_id)
|
849
|
+
```
|
525
850
|
|
526
|
-
|
851
|
+
### Send invoice email
|
527
852
|
|
528
|
-
|
853
|
+
Example:
|
854
|
+
```rb
|
855
|
+
invoice = Magento::Invoice.find(invoice_id)
|
856
|
+
invoice.send_email
|
529
857
|
|
530
|
-
|
531
|
-
|
532
|
-
appendComment: true,
|
533
|
-
items: [{ order_item_id: 123, qty: 1 }], # pass items to partial shipment
|
534
|
-
tracks: [
|
535
|
-
{
|
536
|
-
extension_attributes: { },
|
537
|
-
track_number: "string",
|
538
|
-
title: "string",
|
539
|
-
carrier_code: "string"
|
540
|
-
}
|
541
|
-
]
|
542
|
-
notify: true
|
543
|
-
)
|
858
|
+
# or through the class method
|
859
|
+
Magento::Invoice.send_email(invoice_id)
|
544
860
|
```
|
545
861
|
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
## Cancel an Order
|
862
|
+
### Get invoice comments
|
550
863
|
|
864
|
+
Example:
|
551
865
|
```rb
|
552
|
-
|
553
|
-
|
554
|
-
order.cancel # or
|
555
|
-
|
556
|
-
Magento::Order.cancel(order_id)
|
866
|
+
Magento::Invoice.comments(invoice_id).all
|
867
|
+
Magento::Invoice.comments(invoice_id).where(created_at_gt: Date.today.prev_day).all
|
557
868
|
```
|
558
869
|
|
559
|
-
##
|
870
|
+
## Sales Rules
|
871
|
+
|
872
|
+
### Generate Sales Rules and Coupons
|
560
873
|
|
561
874
|
```rb
|
562
875
|
rule = Magento::SalesRule.create(
|
@@ -591,33 +904,71 @@ Magento::SalesRule.generate_coupon(
|
|
591
904
|
}
|
592
905
|
)
|
593
906
|
```
|
594
|
-
see all params in:
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
See [how to add coupons to cart](#guestcart)
|
907
|
+
> see all params in:
|
908
|
+
[Magento docs Coupon](https://magento.redoc.ly/2.3.5-admin/tag/couponsgenerate#operation/salesRuleCouponManagementV1GeneratePost) and
|
909
|
+
[Magento docs SalesRules](https://magento.redoc.ly/2.3.5-admin/tag/salesRules#operation/salesRuleRuleRepositoryV1SavePost)
|
599
910
|
|
600
|
-
|
911
|
+
|
912
|
+
## Customer
|
913
|
+
|
914
|
+
### Get customer by token
|
601
915
|
```rb
|
602
|
-
Magento::
|
603
|
-
|
916
|
+
Magento::Customer.find_by_token('user_token')
|
917
|
+
```
|
604
918
|
|
605
|
-
|
606
|
-
|
919
|
+
## Guest Cart
|
920
|
+
|
921
|
+
### Payment information
|
922
|
+
Set payment information to finish the order
|
923
|
+
|
924
|
+
Example:
|
925
|
+
```rb
|
926
|
+
cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
|
927
|
+
|
928
|
+
# or use "build" to not request information from the magento API
|
929
|
+
cart = Magento::GuestCart.build(
|
930
|
+
cart_id: 'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN'
|
931
|
+
)
|
932
|
+
|
933
|
+
cart.payment_information(
|
934
|
+
email: 'customer@gmail.com',
|
935
|
+
payment: { method: 'cashondelivery' }
|
936
|
+
)
|
937
|
+
|
938
|
+
>> "234575" # return the order id
|
607
939
|
```
|
608
940
|
|
609
|
-
###
|
941
|
+
### Add coupon to guest cart
|
942
|
+
|
943
|
+
Example:
|
610
944
|
```rb
|
611
|
-
Magento::
|
612
|
-
|
613
|
-
|
614
|
-
|
945
|
+
cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
|
946
|
+
|
947
|
+
cart.add_coupon('COAU4HXE0I')
|
948
|
+
# You can also use the class method
|
949
|
+
Magento::GuestCart.add_coupon('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7', 'COAU4HXE0I')
|
950
|
+
|
951
|
+
>> true # return true on success
|
952
|
+
```
|
953
|
+
|
954
|
+
### Remove coupon from guest cart
|
955
|
+
|
956
|
+
Example:
|
957
|
+
```rb
|
958
|
+
cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
|
959
|
+
|
960
|
+
cart.delete_coupon()
|
961
|
+
# You can also use the class method
|
962
|
+
Magento::GuestCart.delete_coupon('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
|
963
|
+
|
964
|
+
>> true # return true on success
|
615
965
|
```
|
616
966
|
|
617
967
|
## Inventory
|
618
968
|
|
619
969
|
### Check whether a product is salable
|
620
970
|
|
971
|
+
Example:
|
621
972
|
```rb
|
622
973
|
Inventory.get_product_salable_quantity(sku: '4321', stock_id: 1)
|
623
974
|
>> 1
|
@@ -625,6 +976,7 @@ Inventory.get_product_salable_quantity(sku: '4321', stock_id: 1)
|
|
625
976
|
|
626
977
|
### Check whether a product is salable for a specified quantity
|
627
978
|
|
979
|
+
Example:
|
628
980
|
```rb
|
629
981
|
Inventory.is_product_salable_for_requested_qty(
|
630
982
|
sku: '4321',
|
@@ -643,27 +995,84 @@ Inventory.is_product_salable_for_requested_qty(
|
|
643
995
|
}
|
644
996
|
```
|
645
997
|
|
646
|
-
##
|
998
|
+
## **Helper classes**
|
999
|
+
|
1000
|
+
## Create product params
|
647
1001
|
|
648
1002
|
```rb
|
649
|
-
|
650
|
-
|
1003
|
+
params = Magento::Params::CreateProduct.new(
|
1004
|
+
sku: '556-teste-builder',
|
1005
|
+
name: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
1006
|
+
description: 'Descrição do produto',
|
1007
|
+
brand: 'Coca-Cola',
|
1008
|
+
price: 4.99,
|
1009
|
+
special_price: 3.49,
|
1010
|
+
quantity: 2,
|
1011
|
+
weight: 0.3,
|
1012
|
+
attribute_set_id: 4,
|
1013
|
+
images: [
|
1014
|
+
*Magento::Params::CreateImage.new(
|
1015
|
+
path: 'https://urltoimage.com/image.jpg',
|
1016
|
+
title: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
1017
|
+
position: 1,
|
1018
|
+
main: true
|
1019
|
+
).variants,
|
1020
|
+
Magento::Params::CreateImage.new(
|
1021
|
+
path: '/path/to/image.jpg',
|
1022
|
+
title: 'REFRIGERANTE PET COCA-COLA 1,5L ORIGINAL',
|
1023
|
+
position: 2
|
1024
|
+
)
|
1025
|
+
]
|
1026
|
+
)
|
1027
|
+
|
1028
|
+
Magento::Product.create(params.to_h)
|
651
1029
|
```
|
652
1030
|
|
653
|
-
|
1031
|
+
## Create product image params
|
1032
|
+
|
1033
|
+
Helper class to create product image params.
|
1034
|
+
|
1035
|
+
before generating the hash, the following image treatments are performed:
|
1036
|
+
- resize image
|
1037
|
+
- remove alpha
|
1038
|
+
- leaves square
|
1039
|
+
- convert image to jpg
|
654
1040
|
|
1041
|
+
Example:
|
655
1042
|
```rb
|
656
|
-
Magento::
|
657
|
-
|
658
|
-
|
659
|
-
|
1043
|
+
params = Magento::Params::CreateImage.new(
|
1044
|
+
title: 'Image title',
|
1045
|
+
path: '/path/to/image.jpg', # or url
|
1046
|
+
position: 1,
|
1047
|
+
size: 'small', # options: 'large'(defaut), 'medium' and 'small',
|
1048
|
+
disabled: true, # default is false,
|
1049
|
+
main: true, # default is false,
|
1050
|
+
).to_h
|
1051
|
+
|
1052
|
+
Magento::Product.add_media('sku', params)
|
660
1053
|
```
|
661
1054
|
|
662
|
-
|
1055
|
+
The resize defaut confiruration is:
|
1056
|
+
|
1057
|
+
```rb
|
1058
|
+
Magento.configure do |config|
|
1059
|
+
config.product_image.small_size = '200x200>'
|
1060
|
+
config.product_image.medium_size = '400x400>'
|
1061
|
+
config.product_image.large_size = '800x800>'
|
1062
|
+
end
|
1063
|
+
```
|
1064
|
+
|
1065
|
+
## Import products from csv file
|
1066
|
+
|
1067
|
+
_TODO: exemple to [Magento::Import.from_csv](lib/magento/import.rb#L8)_
|
1068
|
+
|
1069
|
+
_TODO: exemple to [Magento::Import.get_csv_template](lib/magento/import.rb#L14)_
|
1070
|
+
|
663
1071
|
|
664
1072
|
___
|
665
1073
|
|
666
|
-
|
1074
|
+
|
1075
|
+
## **TODO:**
|
667
1076
|
|
668
1077
|
### Search products
|
669
1078
|
```rb
|
@@ -681,6 +1090,7 @@ Magento::Product.where(name_like: 'some name%').last
|
|
681
1090
|
|
682
1091
|
### Tests
|
683
1092
|
|
1093
|
+
___
|
684
1094
|
|
685
1095
|
## Development
|
686
1096
|
|
@@ -690,4 +1100,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
690
1100
|
|
691
1101
|
## Contributing
|
692
1102
|
|
693
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/WallasFaria/
|
1103
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/WallasFaria/magento_ruby.
|