スクリプトを使用して、woocommerce インストールで製品を作成しています。API 呼び出しは正常に行われているようです。以下の要求と応答を参照してください。
しかし、バックエンドで製品を見ると、ドラフト モードであり、画像やカテゴリなどの一部の属性が保存されていません。
応答でプレースホルダー画像が返されています。インストールに W3 Total Cache Plugin がないことを確認しました。
リクエスト :
{"title":"KOOVS Fur Panda Back Pack","type":"external","id":"554541381","p_id":"13318","m_id":"669929","status":"","price":"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","brand":"KOOVS","category_name":"Backpacks","category_path_as_string":"Root|Backpacks|","price_html":"INR","regular_price":"1295.00","description":"KOOVS Fur Panda Back Pack","short_description":"","categories":[2044,2047],"images":"[{\"src\":\"http://images.kooves.com/uploads/products/79609_c00b504538416f48d4a78cfa53fd0f7c_image1_default.jpg\",\"position\":0}]","attributes":[{"name":"Color","position":0,"visible":true,"variation":true,"options":[null]},{"name":"Size","position":0,"visible":true,"variation":true,"options":[null]},{"name":"Brand","slug":"pa_brand","visible":true,"variation":true,"options":["KOOVS"]}],"default_attributes":[{"id":23,"option":"Black"},{"name":"Size","option":"S"}]}
応答オブジェクト:
{"product":{"title":"KOOVS Fur Panda Back Pack","id":4568,"created_at":"-0001-11-30T00:00:00Z","updated_at":"-0001-11-30T00:00:00Z","type":"external","status":"draft","downloadable":false,"virtual":false,"permalink":"http:\/\/dev.appilyever.com\/newsite\/?post_type=product&p=4568","sku":"","price":"1295.00","regular_price":"1295.00","sale_price":"1295.00","price_html":"<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">₹<\/span>1,295.00<\/span>","taxable":true,"tax_status":"taxable","tax_class":"","managing_stock":false,"stock_quantity":null,"in_stock":true,"backorders_allowed":false,"backordered":false,"sold_individually":false,"purchaseable":false,"featured":false,"visible":true,"catalog_visibility":"visible","on_sale":false,"product_url":"http:\/\/clk.omgt5.com\/?AID=976182&PID=13318&Type=12&r=http:\/\/www.koovs.com\/koovs-fur-panda-back-pack-79609.htmlal1","button_text":"Buy product","weight":null,"dimensions":{"length":"","width":"","height":"","unit":"cm"},"shipping_required":true,"shipping_taxable":true,"shipping_class":"","shipping_class_id":null,"description":"<p>KOOVS Fur Panda Back Pack<\/p>\n","short_description":"","reviews_allowed":true,"average_rating":"0.00","rating_count":0,"related_ids":[4126,4116,4132,4120,4108],"upsell_ids":[],"cross_sell_ids":[],"parent_id":0,"categories":["Clutches","Handbags"],"tags":[],"images":[{"id":0,"created_at":"2016-11-23T04:19:14Z","updated_at":"2016-11-23T04:19:14Z","src":"http:\/\/dev.appilyever.com\/newsite\/wp-content\/plugins\/woocommerce\/assets\/images\/placeholder.png","title":"Placeholder","alt":"Placeholder","position":0}],"featured_src":"","attributes":[{"name":"Brand","slug":"Brand","position":0,"visible":true,"variation":true,"options":["KOOVS"]},{"name":"Size","slug":"Size","position":0,"visible":true,"variation":true,"options":[""]},{"name":"Color","slug":"Color","position":0,"visible":true,"variation":true,"options":[""]}],"downloads":[],"download_limit":0,"download_expiry":0,"download_type":"","purchase_note":"","total_sales":0,"variations":[],"parent":[],"grouped_products":[],"menu_order":0}}
コード スニペットは次のとおりです。
クロンコール:
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;