Reputation: 1316
I am trying to create multiple coupons in my WordPress site externally from my PHP site and I am making a use of woocommerce-api client library. I am preparing an array of coupon codes to pass to the Create Coupon method so that I can create multiple coupons at once. but its not really working as it returns me the following Error message " Error: Missing parameter code [woocommerce_api_missing_coupon_code]" Here is my 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() );
}
}
This $client->coupons->create( $coupons[0]) where I am passing only the first index of array creates a single coupon successfully but the second line where I pass the whole array to the create method doesn't create any coupon and returns me the following error Error: Missing parameter code[woocommerce_api_missing_coupon_code]
I have printed the coupons[] array and it contains the following data
Array ( [0] => Array ( [code] => AA12B001 [type] => fixed_cart [amount] => 5 ) [1] => Array ( [code] => AA12B002 [type] => fixed_cart [amount] => 5 ))
where as if i print the coupons[0] it contains the following data
Array ( [code] => AA12B001 [type] => fixed_cart [amount] => 5 )
Any Help Please?
Upvotes: 2
Views: 1510
Reputation: 1316
I have resolved my problem by following the steps below.
1: Updated my Woo Commerce version from 2.3.10 to 2.4.7 because the REST API in below versions doesn't facilitate bulk operations mode.
2: After update I need to make minor changes into "class-wc-api-coupons.php" This is the updated WC REST API class which offers a bulk method to create multiple coupons but there is a restriction of MAX 100 bulk operation inside the method,I increased the limit to 2000.(I could find this api class under installed plugins/woocommerce/includes/api).
3:Finally I followed the instruction by @Anand e.g WC client API doesn't support bulk end point so we need to modify/extend the client API, so I added the following function into my "class-wc-api-client-resource-coupons" 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();
}
Now I can make call to this function anywhere in my client code and pass an array of coupon codes to create hundreds of coupons in bulk.
Thanks to @Anand for many help.
Upvotes: 1
Reputation: 14913
The reason why passing the entire array of coupons doesn't work is because the REST client library doesn't define the coupons/bulk
endpoint.
The easier approach is to modify the code you are using, tweak your code as follows
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
Another approach would be to modify the REST client library, but this is going to be a time consuming process. Technically, whether you loop in the client code and create a coupon at a time, or you hand over the entire coupons array to WooCommerce and let WooCommerce create coupons by looping will have the same effect.
The only difference is efficiency, the first approach of creating a coupon a time is less efficient, however unless you have thousands of coupons to create, it shouldn't matter.
EDIT
Here's the solution
1.Edit lib/woocommerce-api/resources/class-wc-api-client-coupons.php
and add the following code to it
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.Now make a call to $client->coupons->create_bulk( $coupons );
I have tested this locally and it works.
Upvotes: 1