我必须创建一个服务才能通过 woocommerce REST API 将产品发布到 wordpress。
我们正在使用批量更新“功能”在网站上创建产品。
一切都很好,但我无法搜索产品是否已经存在,以便不与除 id 之外的任何其他内容相乘(或在插入时出错)。
有没有办法按sku搜索产品?
tks
2 回答
1
您可以使用 GET 方法。
'products' with param sku
使用“GET”非常重要,如果您使用 POST,则在 word press 中添加一个空产品。
于 2020-07-02T06:55:57.987 回答
0
它的老问题,但对于未来的观众来说,这里是解决方案
function CreateOrUpdate ($product , $sku) {
$pid = wc_get_product_id_by_sku($sku);
$updateResp = Update_Product($product,$pid);
if ($updateResp -> code == "rest_no_route" or $updateResp -> code == "woocommerce_rest_product_invalid_id"){
$resp = Create_Product($product);
return $resp;
// return json_encode(array('update' => $updateResp));
} else {
// return json_encode(array('update' => $updateResp));
return json_encode(array('update' => 'success'));
}
}
创建您的产品
function Create_Product($product){
// create your product here
}
更新产品
function Update_Product($product){
// update your product here
}
$product 是您将创建或更新的信息库。我从 api 获取它
于 2021-08-07T19:42:04.600 回答