Created
October 17, 2015 22:30
-
-
Save dtbaker/79b1d8704b5def478c8c to your computer and use it in GitHub Desktop.
Example import a customer into UCM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$customer_import = array( | |
'customer_name' => 'Test Customer Import', | |
'customer_extra' => array( | |
'Extra Key 1' => 'Extra Val 1', | |
'Extra Key 2' => 'Extra Val 2', | |
'Extra Key 3' => 'Extra Val 3', | |
), | |
'address' => array( | |
'line_1' => '123 Test Street', | |
'line_2' => '', | |
'suburb' => 'Test Suburb', | |
'state' => 'Test State', | |
'post_code' => '12345', | |
), | |
'contact' => array( | |
'name' => 'Joe', | |
'last_name' => 'Smith', | |
'email' => 'Phone', | |
'mobile' => '0412345678', | |
), | |
); | |
include('init.php'); // the UCM init code. | |
$customer_id = $plugins['customer']->save_customer('new',array( | |
'customer_name' => $customer_import['customer_name'] | |
)); | |
if(!$customer_id){ | |
echo 'Failed to create customer'; | |
exit; | |
} | |
if(!empty($customer_import['customer_extra'])) { | |
foreach ( $customer_import['customer_extra'] as $extra_key => $extra_val ) { | |
// Add the Medium extra field to that newly created customer | |
$extra_db = array( | |
'extra_key' => $extra_key, | |
'extra' => $extra_val, | |
'owner_table' => 'customer', | |
'owner_id' => $customer_id, | |
); | |
$extra_id = update_insert( 'extra_id', false, 'extra', $extra_db ); | |
} | |
} | |
if(!empty($customer_import['address'])) { | |
// Save the address for the customer | |
$customer_import['address']['owner_id'] = $customer_id; | |
$customer_import['address']['owner_table'] = 'customer'; | |
$customer_import['address']['address_type'] = 'physical'; | |
module_address::save_address( false, $customer_import['address'] ); | |
} | |
if(!empty($customer_import['contact'])) { | |
// add the contact details to this customer record | |
$customer_import['contact']['customer_id'] = $customer_id; | |
$contact_user_id = $plugins['user']->create_user( $customer_import['contact'], 'signup' ); | |
if($contact_user_id){ | |
module_customer::set_primary_user_id($customer_id, $contact_user_id); | |
} | |
} | |
echo "Created a customer with ID $customer_id and a contact with ID $contact_user_id "; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment