skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search