我想在我的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)
任何幫助嗎?
是的,正如我所提到的,WooCommerce REST API確實定義了一個'bulk'端點,它是不使用該端點的Kloon客戶端庫。 –
感謝您的幫助,我已經嘗試了這種方法,但我的問題是我需要一次創建超過20000個優惠券,您能否建議我解決問題的最佳方法?在WooCommerce Rest API Docs中,他們提到通過發送cURL命令一次創建多個優惠券,但正如您所說的「REST客戶端庫不定義優惠券/批量端點」有沒有辦法可以發送cURL命令從我的PHP代碼來解決這個問題? – umer
另一種方法是生成包含20000個優惠券的CSV,然後使用服務器上的代碼導入它。 –