Give_Customer::create PHP Method

create() public method

Creates a customer
Since: 1.0
public create ( array $data = [] ) : mixed
$data array Array of attributes for a customer.
return mixed False if not a valid creation, Customer ID if user is found or valid creation.
    public function create($data = array())
    {
        if ($this->id != 0 || empty($data)) {
            return false;
        }
        $defaults = array('payment_ids' => '');
        $args = wp_parse_args($data, $defaults);
        $args = $this->sanitize_columns($args);
        if (empty($args['email']) || !is_email($args['email'])) {
            return false;
        }
        if (!empty($args['payment_ids']) && is_array($args['payment_ids'])) {
            $args['payment_ids'] = implode(',', array_unique(array_values($args['payment_ids'])));
        }
        do_action('give_customer_pre_create', $args);
        $created = false;
        // The DB class 'add' implies an update if the customer being asked to be created already exists
        if ($this->db->add($data)) {
            // We've successfully added/updated the customer, reset the class vars with the new data
            $customer = $this->db->get_customer_by('email', $args['email']);
            // Setup the customer data with the values from DB
            $this->setup_customer($customer);
            $created = $this->id;
        }
        do_action('give_customer_post_create', $created, $args);
        return $created;
    }

Usage Example

Ejemplo n.º 1
0
 public function test_update_customer()
 {
     $test_email = '*****@*****.**';
     $customer = new Give_Customer($test_email);
     $customer_id = $customer->create(array('email' => $test_email));
     $this->assertEquals($customer_id, $customer->id);
     $data_to_update = array('email' => '*****@*****.**', 'name' => 'Test Account');
     $customer->update($data_to_update);
     $this->assertEquals('*****@*****.**', $customer->email);
     $this->assertEquals('Test Account', $customer->name);
     // Verify if we have an empty array we get false
     $this->assertFalse($customer->update());
 }
All Usage Examples Of Give_Customer::create