我试图在我的WordPress网站外部创建多个优惠券从我的PHP网站,我正在使用woocommerce-api客户端库。我正在准备优惠券代码数组传递到创建优惠券方法,这样我就可以一次创建多个优惠券。但它并没有真正工作,因为它返回给我以下错误消息"错误:缺少参数代码[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->coupon ->create($coupon[0]),我只传递数组的第一个索引,成功创建了一个优惠券,但是第二行,我传递整个数组给create方法,没有创建任何优惠券,并返回以下错误错误:缺少参数代码[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 )
请帮忙好吗?
传递整个优惠券数组不起作用的原因是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 -coupon .php"进行微小的更改。这是更新的WC REST API类,它提供了一个批量方法来创建多个优惠券,但是该方法内部有MAX 100批量操作的限制,我将限制增加到2000。(我可以在installed plugins/woocommerce/includes/api下找到这个api类)
3:最后我遵循了@Anand的指示,例如WC客户端API不支持批量端点,所以我们需要修改/扩展客户端API,所以我在我的"class- WC - API -client-resource-coupon"类中添加了以下功能
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的许多帮助。