我们正在使用脚本在我们的woocommerce安装中创建产品 . API调用似乎没问题 - 请参阅下面的请求和响应 .

但是,当我们在后端查看产品时 - 它处于草稿模式,并且某些属性不会像图像和类别一样保存 .

正在返回占位符图像作为响应 . 我已经检查过我们的安装中没有W3 Total Cache Plugin .

要求:{"title":"KOOVS Fur Panda Back Pack","type":"external","id":"554541381","p_id":"13318","m_id":"669929","status":” ","价格":" 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 "," REGULAR_PRICE ":" 1295.00 ","描述":" KOOVS毛皮熊猫背包"," SHORT_DESCRIPTION ":" ","类别":[2044,2047],"图像":" [{\ "src":\” http://images.kooves.com/uploads/products/79609_c00b504538416f48d4a78cfa53fd0f7c_image1_default.jpg \ ","位置\ ":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":[{_ 471169:23,"option":"Black"},{"name":48117 3,"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 “ ”类型“: ”外部“, ”状态“: ”草案“, ”下载“:假, ”虚拟“:假, ”永久“:” 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 > 1,295.00 </ Span > “ ”应纳税“:真 ”tax_status“: ”应纳税“, ”tax_class“: ”“, ”managing_stock“:假 ”stock_quantity“:NULL, ”IN_STOCK“:真” backorders_allowed “:假的,” 延期交货 “:假的,” sold_individually “:假的,” 可购买 “:假的,” 特色 “:假的,” 可见 “:真实的,” catalog_visibility “:” 可见 “ ”on_sale“:假的,” 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” :“购买产品”,“重量”:null,“尺寸”:{“长度”:“”,“宽度”:“”,“高度”:“”,“单位”:“cm”},“shipping_required” :真正的 “shipping_taxable”:真实的, “shipping_class”: “”,“shipping_class _id “:空,” 说明 “:”

KOOVS毛皮熊猫背包</ 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, “类别”:[ “离合器”, “手提包”], “标签”:[], “图像” :[{ “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 “:””, “属性”:[{ “名称”: “商标”, “塞”: “商标”, “位置”:0, “可见”:真, “变异”:真, “选项”: [ “KOOVS”]},{ “名称”: “大小”, “塞”: “大小”, “位置”:0, “可见”:真, “变异”:真, “选项”:[ “”] },{ “名称”: “色彩”, “弹头”: “色彩”, “位置”:0, “可见”:真, “变异”:真, “选项”:[ “”]}],“下载“:[],” download_limit “:0”,download_expiry “:0”,download_type “:””, “purchase_note”: “”, “TOTAL_SALES”:0, “变体”:[], “亲本”:[] “grouped_products”:[], “menu_order”:0}}

以下是代码段:

Cron电话:

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;