Soon-to-be Angular2+ wrapper for the WooCommerce API
Please note this library is not yet pubished or active, it is currently being developed. Visit the Discord Channel if you have an idea or feature request for this project.
To install this library, run:
$ npm install woo-wrapper --save (not yet published)
Import woo-wrapper in any Angular application by running:
$ npm install woo-wrapper (not yet published)
and then from your Angular AppModule
:
...
// Import woo-wrapper
import { WooApiModule, WooApiService, WooCartService } from 'woo-wrapper';
// Add your WooCommerce Credentials
const WooCommerceConfig = {
url: 'your_site_url',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
wpAPI: true,
version: 'wc/v1'
};
@NgModule({
declarations: [...],
imports: [
...
// Specify woo-wrapper as an import and pass in your config
WooApiModule.forRoot(WooCommerceConfig)
],
// Add the Api service to the provider
// Add the Cart service if you wish to use it
providers: [WooApiService, WooCartService]
})
export class AppModule { }
Once woo-wrapper is imported, you can use the WooApiService
:
import { Component, OnInit } from '@angular/core';
// Import the services
import { WooApiService, WooCartService } from 'woo-wrapper';
@Component({...})
export class HomePage implements OnInit {
products: any;
// Inject the service
constructor(private woo: WooApiService, private cart: WooCartService) { }
ngOnInit(): void {
// Fetch all products
this.woo.fetchItems('products')
.then(products => console.log(products));
}
addToCart(product:any): void {
this.cart.add(product)
.then(response => console.log(response))
.catch(error => console.log(error));
}
}
- Accepts:
string
(required) - the type of WooCommerce item you want to fetch (products, orders, customers, categories). Accepts query parameters, see the WooCommerce Api docs for a full list of query parameters. - Returns:
Promise
Example
this.woo.fetchItems('products')
.then(products => console.log(products));
- Accepts:
object
(required) - your product, in object format. - Accepts:
number
(optional) - the quantity of the product. (default:1
) - Accepts:
object
(optional) - the selected product attributes for variable products (TODO: add format) - Returns:
Promise
Example
this.cart.add(this.product, this.qty, this.meta)
.then(response => console.log(response))
.catch(error => /* Error responses sent here - eg: 'Product already in cart' */);
- Gets current cart contents
- Returns array of cart items (
[{...},{...},{...}]
) - Returns:
Observable
Example
this.cart.get()
.subscribe(data => console.log(data));
- Clears the cart Array
- Returns:
Observable
Example
this.cart.clear()
.subscribe(data => console.log(data));
Example
this.woo.createCustomer(this.user)
.then(response => console.log(response));
Example
this.woo.updateCustomer(this.newData)
.then(response => console.log(response));
Example
this.woo.createOrder(this.products)
.then(response => console.log(response));
Example
this.woo.updateOrder(this.orderId, this.newData)
.then(response => console.log(response));
To generate all *.js
, *.js.map
and *.d.ts
files:
$ npm run tsc
To lint all *.ts
files:
$ npm run lint
MIT © Michael Doye