我们正在使用脚本在我们的woocommerce安装中创建产品。 API调用似乎没问题 - 请参阅下面的请求和响应。
然而,当我们在后端查看产品时 - 它处于草稿模式,并且某些属性不会像图像和类别一样被保存。
正在回复占位符图片。我已经检查过我们的安装中没有W3 Total Cache Plugin。
要求: {" title":" KOOVS Fur Panda Back Pack"," type":" external"," id": " 554541381"" P_ID":" 13318"" M_ID":" 669929""状态& #34;:"""价格":" 1295.00"" SALE_PRICE":" 1295.00" " product_url":" http://clk.omgt5.com/?AID=976182&PID=13318&Type=12&r=http://www.koovs.com/koovs-fur-panda-back-pack-79609.html%3Fal%3D1""品牌":" KOOVS"" CATEGORY_NAME" :"背包"" category_path_as_string":"根|背包|"" price_html":" INR&#34 ;, " regular_price":" 1295.00"," description":" KOOVS Fur Panda Back Pack"," short_description": """分类":[2044,2047],"图像":" [{\" SRC \" :\" http://images.kooves.com/uploads/products/79609_c00b504538416f48d4a78cfa53fd0f7c_image1_default.jpg \" \"位置\":0}]""属性":[{&# 34;名称":"颜色""位置":0,"可见":真,"变异":真,& #34;选择":[空]},{"名称":"尺寸""位置":0,"可见&#34 ;:真,"变异":TRU即,"选择":[空]},{"名称":"品牌""蛞蝓":" pa_brand&# 34;,"可见":真,"变异":真,"选择":[" KOOVS"]}],&#34 ; default_attributes":[{" ID":23,"选项":"黑色"},{"名称":&# 34;尺寸""选项":" S"}]}
响应对象:
{" product":{" title":" KOOVS Fur Panda Back Pack"," id":4568,&#34 ; created_at":" -0001-11-30T00:00:00Z""的updated_at":" -0001-11-30T00:00:00Z" "类型":"外部""状态":"草案""可下载":假,&# 34;虚拟":假,"固定链接":" HTTP://dev.appilyever.com/newsite/ post_type =产物&安培; p = 4568"" SKU":"""价格":" 1295.00"" REGULAR_PRICE":" 1295.00&#34 ;," SALE_PRICE":" 1295.00"" price_html":"₹< /跨度> 1,295.00< /跨度>" "征税":真," tax_status":"征税"" tax_class":""&# 34; managing_stock":假," stock_quantity":空," IN_STOCK":真," backorders_allowed":假,"缺货" :假的,#&34; sold_individually":假,"可购买":假,"功能":假,"可见":真,&#34 ; catalog_visibility&#3 4;:"可见"" on_sale":假," product_url":" HTTP://clk.omgt5.com/ AID = 976182&安培; PID = 13318&安培;类型= 12&安培; R = HTTP://www.koovs.com/koovs-fur-panda-back-pack-79609.htmlal1"," BUTTON_TEXT":&#34 ;购买产品","重量":null,"尺寸":{"长度":"","宽度":"""高度":"""单元":"厘米"} " shipping_required":真," shipping_taxable":真," shipping_class":""" shipping_class_id&#34 ;: null," description":"
KOOVS Fur Panda Back Pack< / p> \ n"," short_description":" #&34;," reviews_allowed":真," AVERAGE_RATING":" 0.00"" RATING_COUNT":0," related_ids":4126,4116,4132,4120,4108]" upsell_ids":[]," cross_sell_ids":[]," PARENT_ID&#34 ;: 0,"分类":["离合器及#34;"手袋及#34],"标记":[],"图像&#34 ;:[{" ID":0," created_at ":" 2016-11-23T04:19:14Z""的updated_at":" 2016-11-23T04:19:14Z"&# 34; SRC":" HTTP://dev.appilyever.com/newsite/wp-content/plugins/woocommerce/assets/images/placeholder.png","标题" :"占位符"" ALT":"占位符""位置":0}]," featured_src&#34 ;: """属性":[{"名称":"品牌""蛞蝓":&#34 ;品牌""位置":0,"可见":真,"变异":真,"选择":[& #34; KOOVS"]},{"名称":"尺寸""蛞蝓":"尺寸"&#34 ;位置":0,"可见":真,"变异":真,"选择":[""]} ,{"名称":"颜色""蛞蝓":"颜色""位置":0,& #34;可见":真,"变异":真,"选择":[""]}],"下载&#34 ;:[]," download_limit":0," download_expiry":0," download_type":""" purchase_note& #34;:"& #34;" TOTAL_SALES":0,"变化":[],"父":[]," grouped_products":[] " menu_order":0}}
以下是代码段:
Cron Call:
wooCommerce.addProductToWooCommerce(buildWoocommerceObject(product), function response(res) {
if (res.data && res.data.data.body && res.data.data.body.length > 0) {
var parsedProduct = JSON.parse(res.data.data.body).product;
if (parsedProduct) {
Product.findOneAndUpdate({ ProductID: product.ProductID },
{
$set: {
ProductPrice: parsedProduct.price, price: parsedProduct.price,
sale_price: parsedProduct.sale_price, images: parsedProduct.images,
attributes: parsedProduct.attributes, categories: parsedProduct.categories,
wpId: parsedProduct.id, lastUpdatedDate: Date.now(),
"status": "MAPPED", "action": "NEW"
}
}, function (err, doc) {
if (err) {
console.error("failed to update product");
} else {
console.log('Product updated in Mapped Collection');
}
});
}
}
});
请求建设:
function buildWoocommerceObject(product) {
var wcProduct = {
title: product.title,
type: product.type,
id: product.ProductID,
p_id: product.PID,
m_id: product.MID,
status: '',
price: product.price,
sale_price: product.sale_price,
product_url: product.ProductURL,
brand: product.Brand,
category_name: product.CategoryName,
category_path_as_string: product.CategoryPathAsString,
price_html: product.ProductPriceCurrency,
regular_price: product.regular_price,
description: product.description,
short_description: '',
categories: product.categories,
images: JSON.stringify(product.image),
attributes: product.attributes,
default_attributes: [{
"id": 23,
"option": "Black"
}, {
"name": "Size",
"option": "S"
}]
};
return wcProduct;
}
控制器定义:
var addProductToWooCommerce = function (product, cb) {
var data = {
"product": product
};
wooCommerce.post('products', data, function (err, data, result) {
if (err) {
cb({ 'code': 400, data: result, "message": "Failed to insert object to WooCommerce" });
} else {
console.log(result);
cb({ 'code': 200, data:{"data":data, "result":result} , "message": "Insert object to WooCommerce succeeded" })
}
});
};
module.exports.addProductToWooCommerce = addProductToWooCommerce;