WC_API_Customers::bulk( array $data )

Bulk update or insert customers Accepts an array with customers in the formats supported by WC_API_Customers->create_customer() and WC_API_Customers->edit_customer()


Description Description


Parameters Parameters

$data

(Required)


Top ↑

Return Return

(array|WP_Error)


Top ↑

Source Source

File: includes/legacy/api/v2/class-wc-api-customers.php

780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
public function bulk( $data ) {
 
    try {
        if ( ! isset( $data['customers'] ) ) {
            throw new WC_API_Exception( 'woocommerce_api_missing_customers_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'customers' ), 400 );
        }
 
        $data  = $data['customers'];
        $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'customers' );
 
        // Limit bulk operation
        if ( count( $data ) > $limit ) {
            throw new WC_API_Exception( 'woocommerce_api_customers_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), 413 );
        }
 
        $customers = array();
 
        foreach ( $data as $_customer ) {
            $customer_id = 0;
 
            // Try to get the customer ID
            if ( isset( $_customer['id'] ) ) {
                $customer_id = intval( $_customer['id'] );
            }
 
            // Customer exists / edit customer
            if ( $customer_id ) {
                $edit = $this->edit_customer( $customer_id, array( 'customer' => $_customer ) );
 
                if ( is_wp_error( $edit ) ) {
                    $customers[] = array(
                        'id'    => $customer_id,
                        'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),
                    );
                } else {
                    $customers[] = $edit['customer'];
                }
            } else {
                // Customer don't exists / create customer
                $new = $this->create_customer( array( 'customer' => $_customer ) );
 
                if ( is_wp_error( $new ) ) {
                    $customers[] = array(
                        'id'    => $customer_id,
                        'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),
                    );
                } else {
                    $customers[] = $new['customer'];
                }
            }
        }
 
        return array( 'customers' => apply_filters( 'woocommerce_api_customers_bulk_response', $customers, $this ) );
    } catch ( WC_API_Exception $e ) {
        return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
    }
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.4.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.