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

标签 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->create( $coupons[0]) ,其中我仅传递数组的第一个索引,成功创建了单个优惠券,但我将整个数组传递给创建方法的第二行却没有成功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 )  

请问有什么帮助吗?

最佳答案

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

我已经在本地测试过了,它有效。

关于PHP WooCommerce Rest API 客户端库创建多个优惠券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32985443/

相关文章:

php - 无法在 MySQL-PHP 中的数据库列日期时间类型中存储值

java - 从 Firestore 数据库检索 GeoPoint 数组添加 map 多边形

php - 仅在 Woocommerce 商店页面添加 DIV

css - 在 Woocommerce 中将结帐字段标签移到左侧

C++如何对 vector 指针执行项目分配?

php - WordPress如何覆盖wc_admin_report中的函数get_order_report_data

PHP 引用和性能

php join 多个查询结果 laravel

php - 使用 Laravel 或 PHP 在 mysql 表上建立条件关系

java - 从数组中删除第一个元素