Skip to content

Soon-to-be Angular2+ wrapper for the WooCommerce API

Notifications You must be signed in to change notification settings

michaeldoye/woo-wrapper

Repository files navigation

woo-wrapper (In development)

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.

GitHub version Build Status dependencies Status GitHub issues Discord Chat

Installation

To install this library, run:

$ npm install woo-wrapper --save (not yet published)

Usage

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));
  }

}

Methods

fetchItems(itemType)

  • 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));

add(product, quantity, productMeta)

  • 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' */);

get()

  • Gets current cart contents
  • Returns array of cart items ([{...},{...},{...}])
  • Returns: Observable

Example

this.cart.get()
  .subscribe(data => console.log(data));

clear()

  • Clears the cart Array
  • Returns: Observable

Example

this.cart.clear()
  .subscribe(data => console.log(data));

createCustomer(user: Object)

Example

this.woo.createCustomer(this.user)
  .then(response => console.log(response));

updateCustomer(user: Object)

Example

this.woo.updateCustomer(this.newData)
  .then(response => console.log(response));

createOrder(items: Array<any>)

Example

this.woo.createOrder(this.products)
  .then(response => console.log(response));

updateOrder(orderId: Number, newData: Object)

Example

this.woo.updateOrder(this.orderId, this.newData)
  .then(response => console.log(response));

Development

To generate all *.js, *.js.map and *.d.ts files:

$ npm run tsc

To lint all *.ts files:

$ npm run lint

License

MIT © Michael Doye

About

Soon-to-be Angular2+ wrapper for the WooCommerce API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published