|
| 1 | +# @vishnuchd/nativescript-woocommerce-api |
| 2 | +A wrapper that connects Nativescript to the WooCommerce API |
| 3 | +## Installation |
| 4 | +```javascript |
| 5 | +ns plugin add @vishnuchd/nativescript-woocommerce-api |
| 6 | +``` |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +// TODO |
| 11 | + |
| 12 | +## License |
| 13 | + |
| 14 | +Apache License Version 2.0 |
| 15 | + |
| 16 | + |
| 17 | +## Setup |
| 18 | + |
| 19 | +You will need a consumer key and consumer secret to call your store's WooCommerce API. You can find instructions [here](https://docs.woocommerce.com/document/woocommerce-rest-api/) |
| 20 | + |
| 21 | +Include the 'NativescriptWoocommerceApi' module within your script and instantiate it with a config: |
| 22 | + |
| 23 | +```javascript |
| 24 | +import { NativescriptWoocommerceApi} from '@vishnuchd/nativescript-woocommerce-api'; |
| 25 | + |
| 26 | +const WooCommerceAPI = { |
| 27 | + url: 'https://yourstore.com', // Your store URL |
| 28 | + ssl: true, |
| 29 | + consumerKey: 'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // Your consumer secret |
| 30 | + consumerSecret: 'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // Your consumer secret |
| 31 | + wpAPI: true, // Enable the WP REST API integration |
| 32 | + version: 'wc/v3', // WooCommerce WP REST API version |
| 33 | + queryStringAuth: true |
| 34 | +}; |
| 35 | +``` |
| 36 | + |
| 37 | +**Instantiating a WooCommerceAPI instance without a url, consumerKey or secret will result in an error being thrown** |
| 38 | + |
| 39 | +## Calling the API |
| 40 | + |
| 41 | +Your WooCommerce API can be called once the WooCommerceAPI object has been instantiated (see above). |
| 42 | + |
| 43 | +### GET |
| 44 | + |
| 45 | +```javascript |
| 46 | +let WooCommerceAPI = new NativescriptWoocommerceApi(options) |
| 47 | +WooCommerceAPI.invokeGet('products') |
| 48 | + .then(data => { |
| 49 | + console.log(data); |
| 50 | + }) |
| 51 | + .catch(error => { |
| 52 | + console.log(error); |
| 53 | + }); |
| 54 | +``` |
| 55 | + |
| 56 | +### GET WITH PARAMETER |
| 57 | + |
| 58 | +```javascript |
| 59 | +WooCommerceAPI.invokeGet('orders', { customer: userID, per_page: 100 }) |
| 60 | + .then(data => { |
| 61 | + console.log(data); |
| 62 | + }) |
| 63 | + .catch(error => { |
| 64 | + console.log(error); |
| 65 | + }); |
| 66 | +``` |
| 67 | + |
| 68 | +### POST |
| 69 | + |
| 70 | +For this example you have a [Order object](http://woocommerce.github.io/woocommerce-rest-api-docs/#create-an-order). |
| 71 | + |
| 72 | +```javascript |
| 73 | +WooCommerceAPI.invokePost('products', { |
| 74 | + product: { |
| 75 | + title: 'Premium Quality', |
| 76 | + type: 'simple', |
| 77 | + regular_price: '21.99' |
| 78 | + } |
| 79 | + }) |
| 80 | + .then(data => { |
| 81 | + console.log(data); |
| 82 | + }) |
| 83 | + .catch(error => { |
| 84 | + console.log(error); |
| 85 | + }); |
| 86 | +``` |
| 87 | + |
| 88 | +### PUT |
| 89 | + |
| 90 | +```javascript |
| 91 | +WooCommerceAPI.invokePut('orders/123', { |
| 92 | + order: { |
| 93 | + status: 'completed' |
| 94 | + } |
| 95 | + }) |
| 96 | + .then(data => { |
| 97 | + console.log(data); |
| 98 | + }) |
| 99 | + .catch(error => { |
| 100 | + console.log(error); |
| 101 | + }); |
| 102 | +``` |
| 103 | + |
| 104 | +### DELETE |
| 105 | + |
| 106 | +```javascript |
| 107 | +WooCommerceAPI.invokeDelete('coupons/123') |
| 108 | +.then(data => { |
| 109 | + console.log(data); |
| 110 | + }) |
| 111 | + .catch(error => { |
| 112 | + console.log(error); |
| 113 | + }); |
| 114 | +}); |
| 115 | +``` |
| 116 | + |
| 117 | + |
0 commit comments