WC_API_Coupons::bulk()publicWC 2.4.0

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()

Метод класса: WC_API_Coupons{}

Возвращает

Массив|WP_Error.

Использование

$WC_API_Coupons = new WC_API_Coupons();
$WC_API_Coupons->bulk( $data );
$data(массив) (обязательный)
-

Список изменений

С версии 2.4.0 Введена.

Код WC_API_Coupons::bulk() WC 8.7.0

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'] );
			}

			if ( $coupon_id ) {

				// Coupon exists / edit coupon
				$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() ) );
	}
}