[英]PHP WooCommerce Rest API Client Library Create Multiple Coupons
我正在嘗試從我的 PHP 站點外部在我的 WordPress 站點中創建多個優惠券,並且我正在使用 woocommerce-api 客戶端庫。 我正在准備一系列優惠券代碼以傳遞給 Create Coupon 方法,以便我可以一次創建多個優惠券。 但它並沒有真正起作用,因為它向我返回以下錯誤消息“錯誤:缺少參數代碼 [woocommerce_api_missing_coupon_code]”這是我的代碼
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() );
}
}
這個 $client->coupons->create( $coupons[0]) 我只傳遞數組的第一個索引成功創建了一個優惠券,但是我將整個數組傳遞給 create 方法的第二行沒有創建任何優惠券並返回以下錯誤錯誤:缺少參數代碼[woocommerce_api_missing_coupon_code]
我已經打印了coupons[]數組,它包含以下數據
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 )
請問有什么幫助嗎?
傳遞整個優惠券數組不起作用的原因是 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.編輯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.現在調用$client->coupons->create_bulk( $coupons );
我已經在本地測試過它並且它有效。
我已按照以下步驟解決了我的問題。
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-resource -優惠券”類
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 提供了很多幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.