我正在尝试从我的WordPress站点外部在我的站点中创建多个优惠券,并且我正在使用woocommerce-api客户端库。我正在准备一组优惠券代码,以传递给Create方法,以便我可以一次创建多个优惠券。但是它并没有真正起作用,因为它返回了以下错误消息“Error:缺失参数代码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->优惠券-> create ( $coupons),我只传递数组的第一个索引,成功地创建了单个优惠券,但是将整个数组传递给create方法的第二行没有创建任何优惠券,而是返回以下错误错误:缺少参数codewoocommerce_api_missing_coupon_code
我已经打印了coupons[]数组,它包含以下数据
Array ( [0] => Array ( [code] => AA12B001 [type] => fixed_cart [amount] => 5 ) [1] => Array ( [code] => AA12B002 [type] => fixed_cart [amount] => 5 )) 当我打印优惠券时,它包含以下数据
Array ( [code] => AA12B001 [type] => fixed_cart [amount] => 5 ) 有什么帮助吗?
发布于 2015-10-07 08:48:05
传递整个优惠券数组的原因是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 );
我已经在本地测试过了它的工作原理。
发布于 2015-10-08 07:19:22
我已经按照以下步骤解决了我的问题。
1:将我的Woo版本从2.3.10更新到2.4.7,因为下面版本中的REST不能方便批量操作模式。
2:更新后,我需要对“类- WC Coupons.php”做一些小修改--这是更新后的increased类,它提供了一种批量创建多个优惠券的方法,但是在方法中有一个对MAX 100批量操作的限制,我将限制提高到2000。(我可以在installed /woocommerce/include/ API下找到这个api类)。
3:最后,我遵循了@Anand的指令,例如WC客户端API不支持大容量端点,因此我们需要修改/扩展客户端API,所以我在我的“类-wc-api-客户机-资源-优惠券”类中添加了以下函数
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的帮助。
https://stackoverflow.com/questions/32985443
复制相似问题