Okay, so It's just a task trying explaining what I'm trying to do here. I have a wordpress site with woocommerce products and also a rest api where I can create(and store) products which automaticly gets posted on the wordpress site. The problem is that their will be no real connection between the product in the api and the one in woocommerce(more than the name). So to create a simmularity between them I'm try to give them the same Id's. The problem is that the Woocommerce-product-id is randomly generated after that the prodcut have been created in the API. If anyone have a solution or "get-arround" to this and could share it I would very much appriciate it.
It is probbaly useful to know that I use webhooks to detect when a product is created in the api and should be "sent" to woocommerce.
This is how a product is "transfered to wordpress":
var WebSocket = require('ws');
var ws = new WebSocket("ws://localhost:5000/api/ws?token=xxxxxx");
ws.on('open', function open() {
console.log("Ready")
ws.on('message', function(data, flags) {
data=JSON.parse(data)
if (data.Kind == "modification") { //This will create or edit a product
PostOrEdit(data)
}
});
});
function PostOrEdit(data){
var options = {
url: 'http://localhost:5000/api/product/' + data.Ids[0], //the url to the prodcut that was delted in the api
headers: headers //not displayed in this code but it shouldnt be a problem
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
body=JSON.parse(body)
var price = body.data.purchasePrice.toString();
console.log(body.data.purchasePrice);
var Wdata = {
name: body.id,
type: 'simple',
regular_price: price ,
description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
short_description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
categories: [
{
id: 9
},
{
id: 14
}
],
images: [
{
src: 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg',
position: 0
},
{
src: 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg',
position: 1
}
]
};
WooCommerce.post('products', Wdata, function(err, data, res) {
//console.log(res);
});
}
}
request(options, callback);
}
via WilliamG
No comments:
Post a Comment