magento 0.12.0 → 0.15.1

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: b651100da8ee0ed5d608d29b8cd3125559c8dd028f1f2ea966f525581d27472b
4
+ data.tar.gz: d9e35baa70a8350c84e92bd24a6a51e858541bef44a031cd7da8bf0161cfe937
5
5
  SHA512:
6
- metadata.gz: 1125477eea00eafac5824a696777dea28b88dfe3bf9c0ea1161c2c98c286cbfa2198052669d6fbdb1b2041dc3fa987d4120167b1f4708859e1283ed018585d8b
7
- data.tar.gz: 7db7a194488212a7d5b050d2c0acc5ec5f7f573995d291435a6a5743d39cba78320ea464f1fa2493f7672f59c5775f061f1fb9899be5ddb188b3dc9868a1c80b
6
+ metadata.gz: 9ed628cdfda56d1dc7261040d6ae3e2fed3e8cfe9f0c0885be0a49e12184092879b33e6f1c3966a0b0a10bf27f286c6362a15adededb5c24ecb8eec70062eae8
7
+ data.tar.gz: a2b87ff7931425bf212e52816b4bf3df8d74946bfbdeca890ed66951e55a4d1bf610bd0334af2b9f3fd22704cd9fb558670899b82eeab203fa50d4ce330f46a8
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,616 @@
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.15.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
+ Add coupon to cart
343
+ ```rb
344
+ cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
345
+
346
+ cart.add_coupon('COAU4HXE0I')
347
+ # You can also use the class method
348
+ Magento::GuestCart.add_coupon('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7', 'COAU4HXE0I')
349
+
350
+ >> true # return true on success
351
+ ```
352
+
353
+ Delete coupon from cart
354
+ ```rb
355
+ cart = Magento::GuestCart.find('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
356
+
357
+ cart.delete_coupon()
358
+ # You can also use the class method
359
+ Magento::GuestCart.delete_coupon('gXsepZcgJbY8RCJXgGioKOO9iBCR20r7')
360
+
361
+ >> true # return true on success
362
+ ```
363
+
364
+ ## Invoice an Order
365
+
366
+ ```rb
367
+ Magento::Order.invoice(order_id)
368
+ >> 25 # return incoice id
369
+
370
+ # or from instance
371
+
372
+ order = Magento::Order.find(order_id)
373
+
374
+ invoice_id = order.invoice
375
+
376
+ # you can pass parameters too
377
+
378
+ invoice_id = order.invoice(
379
+ capture: false,
380
+ appendComment: true,
381
+ items: [{ order_item_id: 123, qty: 1 }], # pass items to partial invoice
382
+ comment: {
383
+ extension_attributes: { },
384
+ comment: "string",
385
+ is_visible_on_front: 0
386
+ },
387
+ notify: true
388
+ )
389
+ ```
390
+
391
+ [Complete Invoice Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdinvoice#operation/salesInvoiceOrderV1ExecutePost)
392
+
393
+ ## Create refund for invoice
394
+
395
+ ```rb
396
+ Magento::Invoice.invoice(invoice_id)
397
+ >> 12 # return refund id
398
+
399
+ # or from instance
400
+
401
+ invoice = Magento::Invoice.find(invoice_id)
402
+
403
+ refund_id = invoice.refund
404
+
405
+ # you can pass parameters too
406
+
407
+ invoice.refund(
408
+ items: [
409
+ {
410
+ extension_attributes: {},
411
+ order_item_id: 0,
412
+ qty: 0
413
+ }
414
+ ],
415
+ isOnline: true,
416
+ notify: true,
417
+ appendComment: true,
418
+ comment: {
419
+ extension_attributes: {},
420
+ comment: string,
421
+ is_visible_on_front: 0
422
+ },
423
+ arguments: {
424
+ shipping_amount: 0,
425
+ adjustment_positive: 0,
426
+ adjustment_negative: 0,
427
+ extension_attributes: {
428
+ return_to_stock_items: [0]
429
+ }
430
+ }
431
+ )
432
+ ```
433
+
434
+ [Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundInvoiceV1ExecutePost)
435
+
436
+
437
+ ## Create offline refund for order
438
+
439
+ ```rb
440
+ Magento::Order.refund(order_id)
441
+ >> 12 # return refund id
442
+
443
+ # or from instance
444
+
445
+ order = Magento::Order.find(order_id)
446
+
447
+ order.refund
448
+
449
+ # you can pass parameters too
450
+
451
+ order.refund(
452
+ items: [
453
+ {
454
+ extension_attributes: {},
455
+ order_item_id: 0,
456
+ qty: 0
457
+ }
458
+ ],
459
+ notify: true,
460
+ appendComment: true,
461
+ comment: {
462
+ extension_attributes: {},
463
+ comment: string,
464
+ is_visible_on_front: 0
465
+ },
466
+ arguments: {
467
+ shipping_amount: 0,
468
+ adjustment_positive: 0,
469
+ adjustment_negative: 0,
470
+ extension_attributes: {
471
+ return_to_stock_items: [0]
472
+ }
473
+ }
474
+ )
475
+ ```
476
+
477
+ [Complete Refund Documentation](https://magento.redoc.ly/2.4-admin/tag/invoicescomments#operation/salesRefundOrderV1ExecutePost)
478
+
479
+ ## Other Invoice methods
480
+
481
+ ```rb
482
+ invoice = Magento::Invoice.find(invoice_id)
483
+
484
+ invoice.capture # or
485
+ Magento::Invoice.capture(invoice_id)
486
+
487
+ invoice.void # or
488
+ Magento::Invoice.void(invoice_id)
489
+
490
+ invoice.send_email # or
491
+ Magento::Invoice.send_email(invoice_id)
492
+
493
+ Magento::Invoice.comments(invoice_id).all
494
+ Magento::Invoice.comments(invoice_id).where(created_at_gt: Date.today.prev_day).all
495
+ ```
496
+
497
+ ## Creates new Shipment for given Order.
498
+
499
+ ```rb
500
+ Magento::Order.ship(order_id)
501
+ >> 25 # return shipment id
502
+
503
+ # or from instance
504
+
505
+ order = Magento::Order.find(order_id)
506
+
507
+ order.ship
508
+
509
+ # you can pass parameters too
510
+
511
+ order.ship(
512
+ capture: false,
513
+ appendComment: true,
514
+ items: [{ order_item_id: 123, qty: 1 }], # pass items to partial shipment
515
+ tracks: [
516
+ {
517
+ extension_attributes: { },
518
+ track_number: "string",
519
+ title: "string",
520
+ carrier_code: "string"
521
+ }
522
+ ]
523
+ notify: true
524
+ )
525
+ ```
526
+
527
+ [Complete Shipment Documentation](https://magento.redoc.ly/2.4-admin/tag/orderorderIdship#operation/salesShipOrderV1ExecutePost)
528
+
529
+
530
+ ## Cancel an Order
531
+
532
+ ```rb
533
+ order = Magento::Order.find(order_id)
534
+
535
+ order.cancel # or
536
+
537
+ Magento::Order.cancel(order_id)
538
+ ```
539
+
540
+ ## Generate Sales Rules and Coupons
541
+
542
+ ```rb
543
+ rule = Magento::SalesRule.create(
544
+ name: 'Discount name',
545
+ website_ids: [1],
546
+ customer_group_ids: [0,1,2,3],
547
+ uses_per_customer: 1,
548
+ is_active: true,
549
+ stop_rules_processing: true,
550
+ is_advanced: false,
551
+ sort_order: 0,
552
+ discount_amount: 100,
553
+ discount_step: 1,
554
+ apply_to_shipping: true,
555
+ times_used: 0,
556
+ is_rss: true,
557
+ coupon_type: 'specific',
558
+ use_auto_generation: true,
559
+ uses_per_coupon: 1
560
+ )
561
+
562
+ rule.generate_coupon(quantity: 1, length: 10)
563
+ ```
564
+
565
+ Generate by class method
566
+ ```rb
567
+ Magento::SalesRule.generate_coupon(
568
+ couponSpec: {
569
+ rule_id: 7,
570
+ quantity: 1,
571
+ length: 10
572
+ }
573
+ )
574
+ ```
575
+ see all params in:
576
+ - [Magento docs Coupon](https://magento.redoc.ly/2.3.5-admin/tag/couponsgenerate#operation/salesRuleCouponManagementV1GeneratePost)
577
+ - [Magento docs SalesRules](https://magento.redoc.ly/2.3.5-admin/tag/salesRules#operation/salesRuleRuleRepositoryV1SavePost)
578
+
579
+ See [how to add coupons to cart](#guestcart)
580
+
581
+ ### First result
582
+ ```rb
583
+ Magento::Product.first
584
+ >> <Magento::Product @sku="some-sku" ...>
585
+
586
+ Magento::Product.where(name_like: 'some name%').first
587
+ >> <Magento::Product @sku="some-sku" ...>
588
+ ```
589
+
590
+ ### Count result
591
+ ```rb
592
+ Magento::Product.count
593
+ >> 7855
594
+ Magento::Product.where(name_like: 'some name%').count
595
+ >> 15
596
+ ```
597
+
598
+ ___
599
+
600
+ ##TODO:
601
+
602
+ ### Search products
603
+ ```rb
604
+ Magento::Product.search('tshort')
605
+ ```
606
+
607
+ ### Last result
608
+ ```rb
609
+ Magento::Product.last
610
+ >> <Magento::Product @sku="some-sku" ...>
611
+
612
+ Magento::Product.where(name_like: 'some name%').last
613
+ >> <Magento::Product @sku="some-sku" ...>
614
+ ```
615
+
616
+ ### Tests