magento 0.9.2 → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2db72674e6f177281d192ec853591b747c91e3ce7f3bd61ccb5beb1a01ddf9d
4
- data.tar.gz: 50afe2399868005885926446ac9cb6563625e99f4752ca6c339728f53adb0b0f
3
+ metadata.gz: bb31fed59d9d16923e61b3f77d218087e2c2dc0e40912f59c0f0f447886ec6a4
4
+ data.tar.gz: 7d93a4d2dcef4ad968a7dacd9eb0445f7f6885791ef202873df555efad9fc4dd
5
5
  SHA512:
6
- metadata.gz: d7734ec0ecf1fcfc46d3675f3cfcee3dd47fbab3606bb8839079b9c5b1f487ba125a6529ee6f7addef0f3e5f81bdb8bd6c97d5c9d73e45a4554ec0e5ab162ec9
7
- data.tar.gz: 1ece8d5edc5efc8d0e336b77c0a0259caf930c51dc7a4fb40ad05954e17a4d960226356d8610c1004e91dc155d6afbeaf094b677130b8a1995ee02e702fb4abb
6
+ metadata.gz: 0b15c59d8eb5f07ebc3df8e875128463790c3a26f6e96f0b076404a3aa18c7760f0d66636f3292b596f0d527da180f1125a8789dc59ed38b769b215bf02be580
7
+ data.tar.gz: 193242af27b216087e158b95bd136ee9fc70b66ca0e68711e5eda257b854beba663949a70a385b495191b12273ded59fb7dec44595a28d52704a4fe5ea1f38d5
data/.gitignore CHANGED
@@ -25,4 +25,4 @@ test/vcr_cassettes
25
25
 
26
26
  .vscode
27
27
  .byebug_history
28
- /*test.rb
28
+ /manual_test
data/Gemfile CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- # Specify your gem's dependencies in pagarme.gemspec
5
+ # Specify your gem's dependencies in magento.gemspec
6
6
  gemspec
data/README.md CHANGED
@@ -1,506 +1,592 @@
1
- # Magento Ruby library
2
-
3
- ## Install
4
-
5
- Add in your Gemfile
6
-
7
- ```rb
8
- gem 'magento', '~> 0.9.2'
9
- ```
10
-
11
- or run
12
-
13
- ```sh
14
- gem install magento
15
- ```
16
-
17
- ### Setup
18
-
19
- ```rb
20
- Magento.url = 'https://yourstore.com'
21
- Magento.token = 'MAGENTO_API_KEY'
22
- Magento.store = :default # optional, Default is :all
23
-
24
- Magento.with_config(store: :other_store) do # accepts store, url and token parameters
25
- Magento::Product.find('sku')
26
- end
27
- ```
28
-
29
- ## Models
30
- ```rb
31
- Magento::Product
32
- Magento::Order
33
- Magento::Country
34
- Magento::Category
35
- ```
36
-
37
- ## Get details
38
-
39
- ```rb
40
- Magento::Product.find('sku-test')
41
- Magento::Order.find(25)
42
- Magento::Country.find('BR')
43
- ```
44
- \* _same pattern to all models_
45
-
46
- **Outside pattern**
47
-
48
- Get customer by token
49
-
50
- ```rb
51
- Magento::Customer.find_by_token('user_token')
52
- ```
53
-
54
- ## Get List
55
-
56
- ```rb
57
- Magento::Product.all
58
- ```
59
-
60
- #### Select fields:
61
- ```rb
62
- Magento::Product.select(:id, :sku, :name).all
63
- Magento::Product.select(:id, :sku, :name, extension_attributes: :category_links).all
64
- Magento::Product.select(:id, :sku, :name, extension_attributes: [:category_links, :website_ids]).all
65
- Magento::Product.select(:id, :sku, :name, extension_attributes: [:website_ids, { category_links: :category_id }]).all
66
- ```
67
-
68
- #### Filters:
69
-
70
- ```rb
71
- Magento::Product.where(name_like: 'IPhone%').all
72
- Magento::Product.where(price_gt: 100).all
73
-
74
- # price > 10 AND price < 20
75
- Magento::Product.where(price_gt: 10)
76
- .where(price_lt: 20).all
77
-
78
- # price < 1 OR price > 100
79
- Magento::Product.where(price_lt: 1, price_gt: 100).all
80
-
81
- Magento::Order.where(status_in: [:canceled, :complete]).all
82
- ```
83
-
84
- | Condition | Notes |
85
- | --------- | ----- |
86
- |eq | Equals. |
87
- |finset | A value within a set of values |
88
- |from | The beginning of a range. Must be used with to |
89
- |gt | Greater than |
90
- |gteq | Greater than or equal |
91
- |in | In. The value is an array |
92
- |like | Like. The value can contain the SQL wildcard characters when like is specified. |
93
- |lt | Less than |
94
- |lteq | Less than or equal |
95
- |moreq | More or equal |
96
- |neq | Not equal |
97
- |nfinset | A value that is not within a set of values |
98
- |nin | Not in. The value is an array |
99
- |notnull | Not null |
100
- |null | Null |
101
- |to | The end of a range. Must be used with from |
102
-
103
-
104
- #### SortOrder:
105
-
106
- ```rb
107
- Magento::Product.order(:sku).all
108
- Magento::Product.order(sku: :desc).all
109
- Magento::Product.order(status: :desc, name: :asc).all
110
- ```
111
-
112
- #### Pagination:
113
-
114
- ```rb
115
- # Set page and quantity per page
116
- Magento::Product.page(1) # Current page, Default is 1
117
- .page_size(25) # Default is 50
118
- .all
119
-
120
- # per is an alias to page_size
121
- Magento::Product.per(25).all
122
- ```
123
-
124
- #### Example of several options together:
125
- ```rb
126
- products = Magento::Product.select(:sku, :name)
127
- .where(name_like: 'biscoito%')
128
- .page(1)
129
- .page_size(5)
130
- .all
131
- ```
132
-
133
- \* _same pattern to all models_
134
-
135
- ### Response
136
-
137
- The `all` method retorns a `Magento::RecordCollection` instance
138
-
139
- ```rb
140
- products.first
141
- >> <Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
142
-
143
- products[0]
144
- >> <Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
145
-
146
- products.last
147
- >> <Magento::Product @sku="964", @name="Biscoito Negresco 140 G Original">
148
-
149
- products.map(&:sku)
150
- >> ["2100", "792", "836", "913", "964"]
151
-
152
- products.size
153
- >> 5
154
-
155
- products.current_page
156
- >> 1
157
-
158
- products.next_page
159
- >> 2
160
-
161
- products.last_page?
162
- >> false
163
-
164
- products.page_size
165
- >> 5
166
-
167
- products.total_count
168
- >> 307
169
-
170
- products.filter_groups
171
- >> [<Magento::FilterGroup @filters=[<Magento::Filter @field="name", @value="biscoito%", @condition_type="like">]>]
172
- ```
173
-
174
- All Methods:
175
-
176
- ```rb
177
- # Information about search criteria
178
- :current_page
179
- :next_page
180
- :last_page?
181
- :page_size
182
- :total_count
183
- :filter_groups
184
-
185
- # Iterating with the list of items
186
- :count
187
- :length
188
- :size
189
-
190
- :first
191
- :last
192
- :[]
193
- :find
194
-
195
- :each
196
- :each_with_index
197
- :sample
198
-
199
- :map
200
- :select
201
- :filter
202
- :reject
203
- :collect
204
- :take
205
- :take_while
206
-
207
- :sort
208
- :sort_by
209
- :reverse_each
210
- :reverse
211
-
212
- :all?
213
- :any?
214
- :none?
215
- :one?
216
- :empty?
217
- ```
218
-
219
- ## Create
220
-
221
- ```rb
222
- Magento::Order.create(
223
- customer_firstname: '',
224
- customer_lastname: '',
225
- customer_email: '',
226
- # others attrbutes ...,
227
- items: [
228
- {
229
- sku: '',
230
- price: '',
231
- qty_ordered: 1,
232
- # others attrbutes ...,
233
- }
234
- ],
235
- billing_address: {
236
- # attrbutes...
237
- },
238
- payment: {
239
- # attrbutes...
240
- },
241
- extension_attributes: {
242
- # attrbutes...
243
- }
244
- )
245
- ```
246
-
247
- ### Update
248
-
249
- ```rb
250
- product = Magento::Product.find('sku-teste')
251
-
252
- product.name = 'Updated name'
253
- product.save
254
-
255
- # or
256
-
257
- product.update(name: 'Updated name')
258
-
259
- # or
260
-
261
- Magento::Product.update('sku-teste', name: 'Updated name')
262
- ```
263
-
264
- ### Delete
265
-
266
- ```rb
267
- product = Magento::Product.find('sku-teste')
268
-
269
- product.delete
270
-
271
- # or
272
-
273
- Magento::Product.delete('sku-teste')
274
- ```
275
-
276
- ## GuestCart
277
-
278
- Set payment information to finish the order
279
- ```rb
280
- cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
281
-
282
- # or use "build" to not request information from the magento API
283
- cart = Magento::GuestCart.build(
284
- cart_id: 'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN'
285
- )
286
-
287
- cart.payment_information(
288
- email: 'customer@gmail.com',
289
- payment: { method: 'cashondelivery' }
290
- )
291
-
292
- >> "234575" # return the order id
293
- ```
294
-
295
- ## Invoice an Order
296
-
297
- ```rb
298
- Magento::Order.invoice(order_id)
299
- >> 25 # return incoice id
300
-
301
- # or from instance
302
-
303
- order = Magento::Order.find(order_id)
304
-
305
- invoice_id = order.invoice
306
-
307
- # you can pass parameters too
308
-
309
- invoice_id = order.invoice(
310
- capture: false,
311
- appendComment: true,
312
- items: [{ order_item_id: 123, qty: 1 }], # pass items to partial invoice
313
- comment: {
314
- extension_attributes: { },
315
- comment: "string",
316
- is_visible_on_front: 0
317
- },
318
- notify: true
319
- )
320
- ```
321
-
322
- [Complete Invoice Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdinvoice#operation/salesInvoiceOrderV1ExecutePost)
323
-
324
- ## Create refund for invoice
325
-
326
- ```rb
327
- Magento::Invoice.invoice(invoice_id)
328
- >> 12 # return refund id
329
-
330
- # or from instance
331
-
332
- invoice = Magento::Invoice.find(invoice_id)
333
-
334
- refund_id = invoice.refund
335
-
336
- # you can pass parameters too
337
-
338
- invoice.refund(
339
- items: [
340
- {
341
- extension_attributes: {},
342
- order_item_id: 0,
343
- qty: 0
344
- }
345
- ],
346
- isOnline: true,
347
- notify: true,
348
- appendComment: true,
349
- comment: {
350
- extension_attributes: {},
351
- comment: string,
352
- is_visible_on_front: 0
353
- },
354
- arguments: {
355
- shipping_amount: 0,
356
- adjustment_positive: 0,
357
- adjustment_negative: 0,
358
- extension_attributes: {
359
- return_to_stock_items: [0]
360
- }
361
- }
362
- )
363
- ```
364
-
365
- [Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundInvoiceV1ExecutePost)
366
-
367
-
368
- ## Create offline refund for order
369
-
370
- ```rb
371
- Magento::Order.refund(order_id)
372
- >> 12 # return refund id
373
-
374
- # or from instance
375
-
376
- order = Magento::Order.find(order_id)
377
-
378
- order.refund
379
-
380
- # you can pass parameters too
381
-
382
- order.refund(
383
- items: [
384
- {
385
- extension_attributes: {},
386
- order_item_id: 0,
387
- qty: 0
388
- }
389
- ],
390
- notify: true,
391
- appendComment: true,
392
- comment: {
393
- extension_attributes: {},
394
- comment: string,
395
- is_visible_on_front: 0
396
- },
397
- arguments: {
398
- shipping_amount: 0,
399
- adjustment_positive: 0,
400
- adjustment_negative: 0,
401
- extension_attributes: {
402
- return_to_stock_items: [0]
403
- }
404
- }
405
- )
406
- ```
407
-
408
- [Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundOrderV1ExecutePost)
409
-
410
- ## Other Invoice methods
411
-
412
- ```rb
413
- invoice = Magento::Invoice.find(invoice_id)
414
-
415
- invoice.capture # or
416
- Magento::Invoice.capture(invoice_id)
417
-
418
- invoice.void # or
419
- Magento::Invoice.void(invoice_id)
420
-
421
- invoice.send_email # or
422
- Magento::Invoice.send_email(invoice_id)
423
-
424
- Magento::Invoice.comments(invoice_id).all
425
- Magento::Invoice.comments(invoice_id).where(created_at_gt: Date.today.prev_day).all
426
- ```
427
-
428
- ## Creates new Shipment for given Order.
429
-
430
- ```rb
431
- Magento::Order.ship(order_id)
432
- >> 25 # return shipment id
433
-
434
- # or from instance
435
-
436
- order = Magento::Order.find(order_id)
437
-
438
- order.ship
439
-
440
- # you can pass parameters too
441
-
442
- order.ship(
443
- capture: false,
444
- appendComment: true,
445
- items: [{ order_item_id: 123, qty: 1 }], # pass items to partial shipment
446
- tracks: [
447
- {
448
- extension_attributes: { },
449
- track_number: "string",
450
- title: "string",
451
- carrier_code: "string"
452
- }
453
- ]
454
- notify: true
455
- )
456
- ```
457
-
458
- [Complete Shipment Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdship#operation/salesShipOrderV1ExecutePost)
459
-
460
-
461
- ## Cancel an Order
462
-
463
- ```rb
464
- order = Magento::Order.find(order_id)
465
-
466
- order.cancel # or
467
-
468
- Magento::Order.cancel(order_id)
469
- ```
470
-
471
- ___
472
-
473
- ##TODO:
474
-
475
- ### Search products
476
- ```rb
477
- Magento::Product.search('tshort')
478
- ```
479
-
480
- ### First result
481
- ```rb
482
- Magento::Product.first
483
- >> <Magento::Product @sku="some-sku" ...>
484
-
485
- Magento::Product.where(name_like: 'some name%').first
486
- >> <Magento::Product @sku="some-sku" ...>
487
- ```
488
-
489
- ### Last result
490
- ```rb
491
- Magento::Product.last
492
- >> <Magento::Product @sku="some-sku" ...>
493
-
494
- Magento::Product.where(name_like: 'some name%').last
495
- >> <Magento::Product @sku="some-sku" ...>
496
- ```
497
-
498
- ### Count result
499
- ```rb
500
- Magento::Product.count
501
- >> 7855
502
- Magento::Product.where(name_like: 'some name%').count
503
- >> 15
504
- ```
505
-
506
- ### Tests
1
+ # Magento Ruby library
2
+
3
+ ## Install
4
+
5
+ Add in your Gemfile
6
+
7
+ ```rb
8
+ gem 'magento', '~> 0.13.1'
9
+ ```
10
+
11
+ or run
12
+
13
+ ```sh
14
+ gem install magento
15
+ ```
16
+
17
+ ### Setup
18
+
19
+ ```rb
20
+ Magento.url = 'https://yourstore.com'
21
+ Magento.token = 'MAGENTO_API_KEY'
22
+ Magento.store = :default # optional, Default is :all
23
+
24
+ Magento.with_config(store: :other_store) do # accepts store, url and token parameters
25
+ Magento::Product.find('sku')
26
+ end
27
+ ```
28
+
29
+ ## Models
30
+ ```rb
31
+ Magento::Product
32
+ Magento::Order
33
+ Magento::Country
34
+ Magento::Category
35
+ Magento::Customer
36
+ ```
37
+
38
+ ## Get details
39
+
40
+ ```rb
41
+ Magento::Product.find('sku-test')
42
+ Magento::Order.find(25)
43
+ Magento::Country.find('BR')
44
+ ```
45
+ \* _same pattern to all models_
46
+
47
+ **Outside pattern**
48
+
49
+ Get customer by token
50
+
51
+ ```rb
52
+ Magento::Customer.find_by_token('user_token')
53
+ ```
54
+
55
+ ## Shurtcut to get custom attribute value by custom attribute code in product
56
+
57
+ Exemple:
58
+
59
+ ```rb
60
+ product.attr :description
61
+ # it is the same as
62
+ product.custom_attributes.find { |a| a.attribute_code == 'description' }&.value
63
+
64
+ # or
65
+ product.description
66
+ ```
67
+
68
+ when the custom attribute does not exists:
69
+
70
+ ```rb
71
+ product.attr :special_price
72
+ > nil
73
+
74
+ product.special_price
75
+ > NoMethodError: undefined method `special_price' for #<Magento::Product:...>
76
+ ```
77
+
78
+ ```rb
79
+ product.respond_to? :special_price
80
+ > false
81
+
82
+ product.respond_to? :description
83
+ > true
84
+ ```
85
+
86
+ ## Get List
87
+
88
+ ```rb
89
+ Magento::Product.all
90
+ ```
91
+
92
+ #### Select fields:
93
+ ```rb
94
+ Magento::Product.select(:id, :sku, :name).all
95
+ Magento::Product.select(:id, :sku, :name, extension_attributes: :category_links).all
96
+ Magento::Product.select(:id, :sku, :name, extension_attributes: [:category_links, :website_ids]).all
97
+ Magento::Product.select(:id, :sku, :name, extension_attributes: [:website_ids, { category_links: :category_id }]).all
98
+ ```
99
+
100
+ #### Filters:
101
+
102
+ ```rb
103
+ Magento::Product.where(visibility: 4).all
104
+ Magento::Product.where(name_like: 'IPhone%').all
105
+ Magento::Product.where(price_gt: 100).all
106
+
107
+ # price > 10 AND price < 20
108
+ Magento::Product.where(price_gt: 10)
109
+ .where(price_lt: 20).all
110
+
111
+ # price < 1 OR price > 100
112
+ Magento::Product.where(price_lt: 1, price_gt: 100).all
113
+
114
+ Magento::Order.where(status_in: [:canceled, :complete]).all
115
+ ```
116
+
117
+ | Condition | Notes |
118
+ | --------- | ----- |
119
+ |eq | Equals. |
120
+ |finset | A value within a set of values |
121
+ |from | The beginning of a range. Must be used with to |
122
+ |gt | Greater than |
123
+ |gteq | Greater than or equal |
124
+ |in | In. The value is an array |
125
+ |like | Like. The value can contain the SQL wildcard characters when like is specified. |
126
+ |lt | Less than |
127
+ |lteq | Less than or equal |
128
+ |moreq | More or equal |
129
+ |neq | Not equal |
130
+ |nfinset | A value that is not within a set of values |
131
+ |nin | Not in. The value is an array |
132
+ |notnull | Not null |
133
+ |null | Null |
134
+ |to | The end of a range. Must be used with from |
135
+
136
+
137
+ #### SortOrder:
138
+
139
+ ```rb
140
+ Magento::Product.order(:sku).all
141
+ Magento::Product.order(sku: :desc).all
142
+ Magento::Product.order(status: :desc, name: :asc).all
143
+ ```
144
+
145
+ #### Pagination:
146
+
147
+ ```rb
148
+ # Set page and quantity per page
149
+ Magento::Product.page(1) # Current page, Default is 1
150
+ .page_size(25) # Default is 50
151
+ .all
152
+
153
+ # per is an alias to page_size
154
+ Magento::Product.per(25).all
155
+ ```
156
+
157
+ #### Example of several options together:
158
+ ```rb
159
+ products = Magento::Product.select(:sku, :name)
160
+ .where(name_like: 'biscoito%')
161
+ .page(1)
162
+ .page_size(5)
163
+ .all
164
+ ```
165
+
166
+ ## Get one
167
+
168
+ ```rb
169
+ Magento::Order.where(increment_id: '000013457').first
170
+ # or
171
+ Magento::Order.find_by(increment_id: '000013457')
172
+ ```
173
+
174
+ ## Count
175
+ ```rb
176
+ Magento::Order.count
177
+ Magento::Order.where(status: :pending).count
178
+ ```
179
+
180
+ \* _same pattern to all models_
181
+
182
+ ### Response
183
+
184
+ The `all` method retorns a `Magento::RecordCollection` instance
185
+
186
+ ```rb
187
+ products.first
188
+ >> <Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
189
+
190
+ products[0]
191
+ >> <Magento::Product @sku="2100", @name="Biscoito Piraque Salgadinho 100G">
192
+
193
+ products.last
194
+ >> <Magento::Product @sku="964", @name="Biscoito Negresco 140 G Original">
195
+
196
+ products.map(&:sku)
197
+ >> ["2100", "792", "836", "913", "964"]
198
+
199
+ products.size
200
+ >> 5
201
+
202
+ products.current_page
203
+ >> 1
204
+
205
+ products.next_page
206
+ >> 2
207
+
208
+ products.last_page?
209
+ >> false
210
+
211
+ products.page_size
212
+ >> 5
213
+
214
+ products.total_count
215
+ >> 307
216
+
217
+ products.filter_groups
218
+ >> [<Magento::FilterGroup @filters=[<Magento::Filter @field="name", @value="biscoito%", @condition_type="like">]>]
219
+ ```
220
+
221
+ All Methods:
222
+
223
+ ```rb
224
+ # Information about search criteria
225
+ :current_page
226
+ :next_page
227
+ :last_page?
228
+ :page_size
229
+ :total_count
230
+ :filter_groups
231
+
232
+ # Iterating with the list of items
233
+ :count
234
+ :length
235
+ :size
236
+
237
+ :first
238
+ :last
239
+ :[]
240
+ :find
241
+
242
+ :each
243
+ :each_with_index
244
+ :sample
245
+
246
+ :map
247
+ :select
248
+ :filter
249
+ :reject
250
+ :collect
251
+ :take
252
+ :take_while
253
+
254
+ :sort
255
+ :sort_by
256
+ :reverse_each
257
+ :reverse
258
+
259
+ :all?
260
+ :any?
261
+ :none?
262
+ :one?
263
+ :empty?
264
+ ```
265
+
266
+ ## Create
267
+
268
+ ```rb
269
+ Magento::Order.create(
270
+ customer_firstname: '',
271
+ customer_lastname: '',
272
+ customer_email: '',
273
+ # others attrbutes ...,
274
+ items: [
275
+ {
276
+ sku: '',
277
+ price: '',
278
+ qty_ordered: 1,
279
+ # others attrbutes ...,
280
+ }
281
+ ],
282
+ billing_address: {
283
+ # attrbutes...
284
+ },
285
+ payment: {
286
+ # attrbutes...
287
+ },
288
+ extension_attributes: {
289
+ # attrbutes...
290
+ }
291
+ )
292
+ ```
293
+
294
+ ### Update
295
+
296
+ ```rb
297
+ product = Magento::Product.find('sku-teste')
298
+
299
+ product.name = 'Updated name'
300
+ product.save
301
+
302
+ # or
303
+
304
+ product.update(name: 'Updated name')
305
+
306
+ # or
307
+
308
+ Magento::Product.update('sku-teste', name: 'Updated name')
309
+ ```
310
+
311
+ ### Delete
312
+
313
+ ```rb
314
+ product = Magento::Product.find('sku-teste')
315
+
316
+ product.delete
317
+
318
+ # or
319
+
320
+ Magento::Product.delete('sku-teste')
321
+ ```
322
+
323
+ ## GuestCart
324
+
325
+ Set payment information to finish the order
326
+ ```rb
327
+ cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
328
+
329
+ # or use "build" to not request information from the magento API
330
+ cart = Magento::GuestCart.build(
331
+ cart_id: 'aj8oUtY1Qi44Fror6UWVN7ftX1idbBKN'
332
+ )
333
+
334
+ cart.payment_information(
335
+ email: 'customer@gmail.com',
336
+ payment: { method: 'cashondelivery' }
337
+ )
338
+
339
+ >> "234575" # return the order id
340
+ ```
341
+
342
+ ## Invoice an Order
343
+
344
+ ```rb
345
+ Magento::Order.invoice(order_id)
346
+ >> 25 # return incoice id
347
+
348
+ # or from instance
349
+
350
+ order = Magento::Order.find(order_id)
351
+
352
+ invoice_id = order.invoice
353
+
354
+ # you can pass parameters too
355
+
356
+ invoice_id = order.invoice(
357
+ capture: false,
358
+ appendComment: true,
359
+ items: [{ order_item_id: 123, qty: 1 }], # pass items to partial invoice
360
+ comment: {
361
+ extension_attributes: { },
362
+ comment: "string",
363
+ is_visible_on_front: 0
364
+ },
365
+ notify: true
366
+ )
367
+ ```
368
+
369
+ [Complete Invoice Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdinvoice#operation/salesInvoiceOrderV1ExecutePost)
370
+
371
+ ## Create refund for invoice
372
+
373
+ ```rb
374
+ Magento::Invoice.invoice(invoice_id)
375
+ >> 12 # return refund id
376
+
377
+ # or from instance
378
+
379
+ invoice = Magento::Invoice.find(invoice_id)
380
+
381
+ refund_id = invoice.refund
382
+
383
+ # you can pass parameters too
384
+
385
+ invoice.refund(
386
+ items: [
387
+ {
388
+ extension_attributes: {},
389
+ order_item_id: 0,
390
+ qty: 0
391
+ }
392
+ ],
393
+ isOnline: true,
394
+ notify: true,
395
+ appendComment: true,
396
+ comment: {
397
+ extension_attributes: {},
398
+ comment: string,
399
+ is_visible_on_front: 0
400
+ },
401
+ arguments: {
402
+ shipping_amount: 0,
403
+ adjustment_positive: 0,
404
+ adjustment_negative: 0,
405
+ extension_attributes: {
406
+ return_to_stock_items: [0]
407
+ }
408
+ }
409
+ )
410
+ ```
411
+
412
+ [Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundInvoiceV1ExecutePost)
413
+
414
+
415
+ ## Create offline refund for order
416
+
417
+ ```rb
418
+ Magento::Order.refund(order_id)
419
+ >> 12 # return refund id
420
+
421
+ # or from instance
422
+
423
+ order = Magento::Order.find(order_id)
424
+
425
+ order.refund
426
+
427
+ # you can pass parameters too
428
+
429
+ order.refund(
430
+ items: [
431
+ {
432
+ extension_attributes: {},
433
+ order_item_id: 0,
434
+ qty: 0
435
+ }
436
+ ],
437
+ notify: true,
438
+ appendComment: true,
439
+ comment: {
440
+ extension_attributes: {},
441
+ comment: string,
442
+ is_visible_on_front: 0
443
+ },
444
+ arguments: {
445
+ shipping_amount: 0,
446
+ adjustment_positive: 0,
447
+ adjustment_negative: 0,
448
+ extension_attributes: {
449
+ return_to_stock_items: [0]
450
+ }
451
+ }
452
+ )
453
+ ```
454
+
455
+ [Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundOrderV1ExecutePost)
456
+
457
+ ## Other Invoice methods
458
+
459
+ ```rb
460
+ invoice = Magento::Invoice.find(invoice_id)
461
+
462
+ invoice.capture # or
463
+ Magento::Invoice.capture(invoice_id)
464
+
465
+ invoice.void # or
466
+ Magento::Invoice.void(invoice_id)
467
+
468
+ invoice.send_email # or
469
+ Magento::Invoice.send_email(invoice_id)
470
+
471
+ Magento::Invoice.comments(invoice_id).all
472
+ Magento::Invoice.comments(invoice_id).where(created_at_gt: Date.today.prev_day).all
473
+ ```
474
+
475
+ ## Creates new Shipment for given Order.
476
+
477
+ ```rb
478
+ Magento::Order.ship(order_id)
479
+ >> 25 # return shipment id
480
+
481
+ # or from instance
482
+
483
+ order = Magento::Order.find(order_id)
484
+
485
+ order.ship
486
+
487
+ # you can pass parameters too
488
+
489
+ order.ship(
490
+ capture: false,
491
+ appendComment: true,
492
+ items: [{ order_item_id: 123, qty: 1 }], # pass items to partial shipment
493
+ tracks: [
494
+ {
495
+ extension_attributes: { },
496
+ track_number: "string",
497
+ title: "string",
498
+ carrier_code: "string"
499
+ }
500
+ ]
501
+ notify: true
502
+ )
503
+ ```
504
+
505
+ [Complete Shipment Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdship#operation/salesShipOrderV1ExecutePost)
506
+
507
+
508
+ ## Cancel an Order
509
+
510
+ ```rb
511
+ order = Magento::Order.find(order_id)
512
+
513
+ order.cancel # or
514
+
515
+ Magento::Order.cancel(order_id)
516
+ ```
517
+
518
+ ## Generate Sales Rules and Coupons
519
+
520
+ ```rb
521
+ rule = Magento::SalesRule.create(
522
+ name: 'Discount name',
523
+ website_ids: [1],
524
+ customer_group_ids: [0,1,2,3],
525
+ uses_per_customer: 1,
526
+ is_active: true,
527
+ stop_rules_processing: true,
528
+ is_advanced: false,
529
+ sort_order: 0,
530
+ discount_amount: 100,
531
+ discount_step: 1,
532
+ apply_to_shipping: true,
533
+ times_used: 0,
534
+ is_rss: true,
535
+ coupon_type: 'specific',
536
+ use_auto_generation: true,
537
+ uses_per_coupon: 1
538
+ )
539
+
540
+ rule.generate_coupon(quantity: 1, length: 10)
541
+ ```
542
+
543
+ Renarate by class method
544
+ ```rb
545
+ Magento::SalesRule.generate_coupon(
546
+ couponSpec: {
547
+ rule_id: 7,
548
+ quantity: 1,
549
+ length: 10
550
+ }
551
+ )
552
+ ```
553
+ see all params in:
554
+ - [Magento docs Coupon](https://magento.redoc.ly/2.3.5-admin/tag/couponsgenerate#operation/salesRuleCouponManagementV1GeneratePost)
555
+ - [Magento docs SalesRules](https://magento.redoc.ly/2.3.5-admin/tag/salesRules#operation/salesRuleRuleRepositoryV1SavePost)
556
+
557
+ ### First result
558
+ ```rb
559
+ Magento::Product.first
560
+ >> <Magento::Product @sku="some-sku" ...>
561
+
562
+ Magento::Product.where(name_like: 'some name%').first
563
+ >> <Magento::Product @sku="some-sku" ...>
564
+ ```
565
+
566
+ ### Count result
567
+ ```rb
568
+ Magento::Product.count
569
+ >> 7855
570
+ Magento::Product.where(name_like: 'some name%').count
571
+ >> 15
572
+ ```
573
+
574
+ ___
575
+
576
+ ##TODO:
577
+
578
+ ### Search products
579
+ ```rb
580
+ Magento::Product.search('tshort')
581
+ ```
582
+
583
+ ### Last result
584
+ ```rb
585
+ Magento::Product.last
586
+ >> <Magento::Product @sku="some-sku" ...>
587
+
588
+ Magento::Product.where(name_like: 'some name%').last
589
+ >> <Magento::Product @sku="some-sku" ...>
590
+ ```
591
+
592
+ ### Tests