Connect to WooCommerce Rest API using Javascript – IT Creative Labs

PHOTO EMBED

Sat Jun 26 2021 03:35:55 GMT+0000 (Coordinated Universal Time)

Saved by @rstringa #javascript

const wooClientKey = 'ck_00000000000000000000000000';
const wooClientSecret = 'cs_00000000000000000000000000';
const wooUrl = 'https://yoursite.com/wp-json/wc/v3/products';

function basicAuth(key, secret) {
    let hash = btoa(key + ':' + secret);
    return "Basic " + hash;
}

let auth = basicAuth(wooClientKey, wooClientSecret);

function getData(url) {
    jQuery.ajax({
        url: url,
        method: 'GET',
        beforeSend: function (req) {
            req.setRequestHeader('Authorization', auth);
        }
    })
        .done(function (data) {
            console.log(data);
            return data;
        });
}

getData(wooUrl);
content_copyCOPY

https://www.itcreativelabs.com/blog/connect-to-woocommerce-rest-api-using-javascript/