modules

ac-apps-minimal-api-woo

api/woo.js

Makes it easy to work with the WooCommerce API and perform tasks such as fetching and updating products. Fetches all records/products if page number not provided.

Example:
app.api.woo.getProducts(options);
app.api.woo.updateProduct(options, product);

A module for ac-bundle-app, published by ac-bundle-module. namespace: ac-apps-minimal

Install from:

https://www.npmjs.com/package/ac-apps-minimal-api-woo
https://github.com/animatedcreativity/ac-apps-minimal-api-woo

Available Methods: updateOrder, list, updateProduct, getLoadedProduct, getProduct, getProducts, getOrders

updateOrder

async function(options, order) {
  var url = options.wcApiUrl + "/orders/" + order.id;
  console.log(app.consoleColors.bgBlue, "Updating order:", order.id + " " + url);
  var result = await fetch(url, {
    method: "PUT",
    headers: {
      "Authorization": "Basic " + Buffer.from(options.wcConsumerKey + ":" + options.wcConsumerSecret).toString('base64'),
      "Content-Type": "application/json"
    },
    body: JSON.stringify(order)
  });
  if (result.status === 200) {
    return await result.json();
  }
  return false;
}

list

async function(options, page, key) {
  var fetchAll = !app.has(page) || page > 1;
  if (!app.has(page)) page = 1;
  var url = options.wcApiUrl + "/" + key + "s?page=" + page + "&per_page=100";
  console.log(app.consoleColors.bgBlue, "Fetching " + key + " page:", page + " " + url);
  var result = await fetch(url, {
    headers: {
      "Authorization": "Basic " + Buffer.from(options.wcConsumerKey + ":" + options.wcConsumerSecret).toString('base64'),
      "Content-Type": "application/json"
    }
  });
  if (result.status === 200) {
    var items = await result.json();
    if (items.length < 100 || fetchAll !== true) {
      return items;
    } else {
      var moreItems = await mod.list(options, page + 1, key);
      return app.has(moreItems) ? items.concat(moreItems) : undefined;
    }
  }
}

updateProduct

async function(options, product) {
  var url = options.wcApiUrl + "/products/" + product.id;
  console.log(app.consoleColors.bgBlue, "Updating product:", (app.has(product.sku) ? product.sku : product.id) + " " + url);
  var result = await fetch(url, {
    method: "PUT",
    headers: {
      "Authorization": "Basic " + Buffer.from(options.wcConsumerKey + ":" + options.wcConsumerSecret).toString('base64'),
      "Content-Type": "application/json"
    },
    body: JSON.stringify(product)
  });
  if (result.status === 200) {
    return true;
  }
  return false;
}

getLoadedProduct

function(list, key, value) {
  var filtered = [];
  for (var i=0; i<=list.length-1; i++) {
    var product = list[i];
    if (app.has(product[key]) && product[key] === value) {
      filtered.push(product);
    }
  }
  if (filtered.length === 0) return;
  if (filtered.length === 1) return filtered[0];
  return filtered;
}

getProduct

async function(options, sku) {
  var url = options.wcApiUrl + "/products?sku=" + sku;
  console.log(app.consoleColors.bgBlue, "Fetching product:", sku + " " + url);
  var result = await fetch(url, {
    headers: {
      "Authorization": "Basic " + Buffer.from(options.wcConsumerKey + ":" + options.wcConsumerSecret).toString('base64'),
      "Content-Type": "application/json"
    }
  });
  if (result.status === 200) {
    var json = await result.json();
    if (json.length > 0) return json[0];
  }
}

getProducts

async function(options, page) {
  return await mod.list(options, page, "product");
}

getOrders

async function(options, page) {
  return await mod.list(options, page, "order");
}

Happy Coding!

MIT License: https://choosealicense.com/licenses/mit/

Copyright 2022 Animated Creativity

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Leave a Reply

Your email address will not be published. Required fields are marked *