PHP WooCommerce Rest API客户端库创建多个优惠券

时间:2015-10-07 06:47:16

标签: php arrays woocommerce

我正在尝试从我的PHP网站外部在我的WordPress网站上创建多个优惠券,我正在使用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->优惠券 - >创建($ coupons [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 )  

请帮忙吗?

2 个答案:

答案 0 :(得分:1)

传递整个优惠券数组的原因是因为REST客户端库没有定义sp端点。

更简单的方法是修改您正在使用的代码,调整代码如下

coupons/bulk

另一种方法是修改REST客户端库,但这将是一个耗时的过程。从技术上讲,无论您是在客户端代码中循环并一次创建优惠券,还是将整个优惠券阵列移交给WooCommerce,让WooCommerce通过循环创建优惠券都会产生相同的效果。

唯一的区别在于效率,一次创建优惠券的第一种方法效率较低,但除非您有数千张优惠券可供创建,否则无所谓。

修改

这是解决方案

1.编辑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 并将以下代码添加到其中

lib/woocommerce-api/resources/class-wc-api-client-coupons.php

2.现在拨打public function create_bulk( $data ) { $this->object_namespace = 'coupons'; $this->set_request_args( array( 'method' => 'POST', 'body' => $data, 'path' => 'bulk', ) ); return $this->do_request(); }

我已经在本地进行了测试,但它确实有效。

答案 1 :(得分:1)

我已按照以下步骤解决了我的问题。

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-”中客户资源优惠券“class

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的许多帮助。