【问题标题】:PHP WooCommerce Rest API Client Library Create Multiple CouponsPHP WooCommerce Rest API 客户端库创建多个优惠券
【发布时间】: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


    【解决方案1】:

    传递整个优惠券数组不起作用的原因是 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 );

    我已经在本地进行了测试,并且可以正常工作。

    【讨论】:

    • 是的,正如我所提到的,WooCommerce REST API 确实定义了一个 bulk 端点,它是不使用该端点的 Kloon 客户端库。
    • 感谢您的帮助,我已经尝试过这种方法,但我的问题是我需要一次创建超过 20000 张优惠券,您能建议我解决问题的最佳方法吗?在 WooCommerce Rest API 文档中,他们提到通过发送 cURL 命令一次创建多个优惠券,但正如您所说“REST 客户端库没有定义优惠券/批量端点”有没有一种方法可以发送 cURL 命令从我的 php 代码中解决这个问题?
    • 另一种方法是生成包含 20000 张优惠券的 CSV,然后使用服务器上的代码将其导入。
    • 正如你所提到的,除非我必须创建数千张优惠券,否则没关系,但在我的情况下,即使创建 100 张优惠券也需要很多时间,比如超过 80 秒左右,一次创建 200 张优惠券在中间崩溃。
    • 有没有一种方法可以让我直接使用 WC Rest API 并将我的 PHP 代码中的命令发送给它来创建优惠券?
    【解决方案2】:

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

    1:将我的 Woo Commerce 版本从 2.3.10 更新到 2.4.7,因为以下版本中的 REST API 不支持批量操作模式。

    2: 更新后我需要对“class-wc-api-coupons.php”做一些小改动 这是更新的 WC REST API 类,它提供了一个批量方法来创建多个优惠券,但有 MAX 100 的限制方法里面的批量操作,我把限制增加到2000。(我可以在installed plugins/woocommerce/includes/api下找到这个api类)。

    3:最后我按照@Anand 的说明进行操作,例如 WC 客户端 API 不支持批量端点,因此我们需要修改/扩展客户端 API,因此我在“class-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 提供的许多帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 2019-04-24
      • 2018-04-12
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      相关资源
      最近更新 更多