2015-10-07 126 views
2

我想在我的PHP網站的外部創建多個優惠券,我正在使用woocommerce-api客戶端庫。 我正在準備一組優惠券代碼以傳遞給創建優惠券方法,以便我可以一次創建多個優惠券。但它不是真正的工作,因爲它返回我以下錯誤消息「錯誤:缺少參數代碼woocommerce_api_missing_coupon_code]」這裏是我的代碼PHP WooCommerce Rest API客戶端庫創建多個優惠券

 foreach ($tags->result() as $row) { 
      $coupons[$i]['code'] = $row->id_tag; 
      $coupons[$i]['type'] = 'fixed_cart'; 
      $coupons[$i]['amount'] = 5; 
      $i++; 
     } 

     print_r($coupons); 
     print_r($coupons[0]); 
     require_once '/application/lib/woocommerce-api.php'; 
     $consumer_key = 'ck_consumerKey'; // Add your own Consumer Key here 
     $consumer_secret = 'cs_ConsumeSecret'; // Add your own Consumer Secret here 
     $store_url = 'http://mySiteUrl'; // Add the home URL to the store you want to connect to here    
     try 
     { 
      $client = new WC_API_Client($store_url, $consumer_key, $consumer_secret); 

      $client->coupons->create($coupons[0]); 
      $client->coupons->create($coupons); 
     } 
     catch (WC_API_Client_Exception $e) 
     { 
      echo $e->getMessage() . PHP_EOL; 
      echo $e->getCode() . PHP_EOL; 
      if ($e instanceof WC_API_Client_HTTP_Exception) 
      { 
       print_r($e->get_request()); 
       print_r($e->get_response()); 
      } 

     } 

這$客戶 - > coupons->創建($券[0])在那裏我只通過數組的第一個索引創建一個單一的優惠券成功,但第二行我將整個數組傳遞給創建方法不會創建任何優惠券並返回給我以下錯誤 錯誤:缺少參數代碼[woocommerce_api_missing_coupon_code]

我已經打印了優惠券[]陣列,它包含以下數據

Array ([0] => Array ([code] => AA12B001 [type] => fixed_cart [amount] => 5) [1] => Array ([code] => AA12B002 [type] => fixed_cart [amount] => 5)) 

那裏,如果我打印的優惠券[0]它包含以下數據

Array ([code] => AA12B001 [type] => fixed_cart [amount] => 5) 

任何幫助嗎?

回答

1

傳遞整個優惠券陣列不起作用的原因是因爲REST客戶端庫沒有定義coupons/bulk端點。

更簡單的方法是修改您所使用的代碼,調整你的代碼如下

require_once '/application/lib/woocommerce-api.php'; 
$consumer_key = 'ck_consumerKey'; // Add your own Consumer Key here 
$consumer_secret = 'cs_ConsumeSecret'; // Add your own Consumer Secret here 
$store_url = 'http://mySiteUrl'; // Add the home URL to the store you want to connect to here    

try 
{ 
    $client = new WC_API_Client($store_url, $consumer_key, $consumer_secret); 

    foreach ($tags->result() as $row) { 
     $coupons = array(); 
     $coupons['code'] = $row->id_tag; 
     $coupons['type'] = 'fixed_cart'; 
     $coupons['amount'] = 5; 
     $client->coupons->create($coupons); 
    } 

    .... //continue with the rest of the code  

另一種方法是修改REST客戶端庫,但是這將是一個耗時的過程。從技術上講,無論您是循環客戶端代碼並一次創建優惠券,還是將整個優惠券數組移交給WooCommerce,並讓WooCommerce通過循環創建優惠券,都會產生相同的效果。

唯一的區別是效率,一次創建優惠券的第一種方法效率較低,但是除非您有成千上萬的優惠券才能創建,否則無關緊要。

編輯

這裏的解決方案

1.Edit lib/woocommerce-api/resources/class-wc-api-client-coupons.php和下面的代碼添加到它

public function create_bulk($data) { 

    $this->object_namespace = 'coupons'; 

    $this->set_request_args(array(
     'method' => 'POST', 
     'body' => $data, 
     'path' => 'bulk', 
    )); 

    return $this->do_request(); 
} 

2.Now撥打電話到$client->coupons->create_bulk($coupons);

我有在本地進行測試,並且工作正常

+0

是的,正如我所提到的,WooCommerce REST API確實定義了一個'bulk'端點,它是不使用該端點的Kloon客戶端庫。 –

+0

感謝您的幫助,我已經嘗試了這種方法,但我的問題是我需要一次創建超過20000個優惠券,您能否建議我解決問題的最佳方法?在WooCommerce Rest API Docs中,他們提到通過發送cURL命令一次創建多個優惠券,但正如您所說的「REST客戶端庫不定義優惠券/批量端點」有沒有辦法可以發送cURL命令從我的PHP代碼來解決這個問題? – umer

+0

另一種方法是生成包含20000個優惠券的CSV,然後使用服務器上的代碼導入它。 –

1

我按照以下步驟解決了我的問題。

1:將我的Woo Commerce版本從2.3.10更新到2.4.7,因爲以下版本中的REST API不利於批量操作模式。

2:更新後我需要對「class-wc-api-coupons.php」進行細微更改這是更新後的WC REST API類,它提供了一種批量方法來創建多個優惠券,但是MAX的限制在方法裏面進行100批量操作,我把限制增加到了2000。(我可以在安裝的插件/ woocommerce/includes/api下找到這個api類)。

3:最後我遵循@Anand的指令,例如WC客戶端API不支持批量端點,所以我們需要修改/擴展客戶端API,所以我將下面的函數添加到我的「class-wc-api -client資源優惠券」級

public function create_bulk($data) { 

$this->object_namespace = 'coupons'; 

$this->set_request_args(array(
    'method' => 'POST', 
    'body' => $data, 
    'path' => 'bulk', 
)); 

return $this->do_request(); 
} 

現在我可以調用這個函數中的任何地方我的客戶端代碼,並通過優惠券代碼數組批量創建數百個優惠券。

感謝@Anand提供了很多幫助。