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