【问题标题】:Search product with woocommerce REST API by sku通过 sku 使用 woocommerce REST API 搜索产品
【发布时间】:2020-06-30 10:00:41
【问题描述】:

我必须创建一个服务才能通过 woocommerce REST API 将产品发布到 wordpress。
我们正在使用批量更新“功能”在网站上创建产品。
一切都很好,但我无法搜索产品是否已经存在,以便不与除 id 之外的任何其他内容相乘(或在插入时出错)。
有没有办法按 sku 搜索产品?
tks

【问题讨论】:

    标签: woocommerce-rest-api


    【解决方案1】:

    您可以使用 GET 方法。
    'products' with param sku
    使用“GET”非常重要,如果您使用 POST,则在 word press 中添加一个空产品。

    【讨论】:

      【解决方案2】:

      它的老问题,但对于未来的观众来说,这里是解决方案

      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 获取它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-04
        相关资源
        最近更新 更多