0
votes

I have to create a service in order to publish products to a wordpress via woocommerce REST API.
We are creating products in site by using the batch update "function".
All nice and fine, but I cannot search if a product already exist in order to not multiply (or get error on insert) by anything else except the id.
Is there a way to search for a product by sku?
tks

2

2 Answers

0
votes

You can by using the GET method.
'products' with param sku
very important to use "GET", if you use POST you add one empty product in word press.

0
votes

its old question, but for future viewers here is solution

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'));
    }
    
}

create your product

function Create_Product($product){
 // create your product here
}

update product

function Update_Product($product){
     // update your product here
}

$product is the information base on which you will create or update.am getting it from an api