【发布时间】:2015-10-07 06:47:16
【问题描述】:
我正在尝试从我的 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 方法的第二行没有' t 创建任何优惠券并向我返回以下错误 错误:缺少参数代码[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 )
有什么帮助吗?
【问题讨论】:
标签: php arrays woocommerce