WC_API_Coupons::bulk( array $data )
Bulk update or insert coupons Accepts an array with coupons in the formats supported by WC_API_Coupons->create_coupon() and WC_API_Coupons->edit_coupon()
Description Description
Parameters Parameters
- $data
-
(Required)
Return Return
(array|WP_Error)
Source Source
File: includes/legacy/api/v2/class-wc-api-coupons.php
public function bulk( $data ) {
try {
if ( ! isset( $data['coupons'] ) ) {
throw new WC_API_Exception( 'woocommerce_api_missing_coupons_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'coupons' ), 400 );
}
$data = $data['coupons'];
$limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'coupons' );
// Limit bulk operation
if ( count( $data ) > $limit ) {
throw new WC_API_Exception( 'woocommerce_api_coupons_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), 413 );
}
$coupons = array();
foreach ( $data as $_coupon ) {
$coupon_id = 0;
// Try to get the coupon ID
if ( isset( $_coupon['id'] ) ) {
$coupon_id = intval( $_coupon['id'] );
}
// Coupon exists / edit coupon
if ( $coupon_id ) {
$edit = $this->edit_coupon( $coupon_id, array( 'coupon' => $_coupon ) );
if ( is_wp_error( $edit ) ) {
$coupons[] = array(
'id' => $coupon_id,
'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),
);
} else {
$coupons[] = $edit['coupon'];
}
} else {
// Coupon don't exists / create coupon
$new = $this->create_coupon( array( 'coupon' => $_coupon ) );
if ( is_wp_error( $new ) ) {
$coupons[] = array(
'id' => $coupon_id,
'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),
);
} else {
$coupons[] = $new['coupon'];
}
}
}
return array( 'coupons' => apply_filters( 'woocommerce_api_coupons_bulk_response', $coupons, $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.4.0 | Introduced. |