Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom order data example - Save order data
<?php
/*
Step 4. Once the order is successfully paid for ...
The $order parameter contains all the data contained here (scroll down to the Order webhook): https://help.shopify.com/api/reference/webhook
*/
function namespace_checkouts_order_paid($order) {
// Check to make sure note attributes exists and has a value
if (isset($order->note_attributes) && $order->note_attributes) {
$dynamicValues = $order->note_attributes;
} else {
$dynamicValues = 'No dynamic values found!';
}
// Check to make sure first name exists and has a value
if (isset($order->customer->first_name) && $order->customer->first_name) {
$customerFirstName = $order->customer->first_name; // Andrew
} else {
$customerFirstName = '';
}
// Check to make sure last name exists and has a value
if (isset($order->customer->last_name) && $order->customer->last_name) {
$customerLastName = $order->customer->last_name; // Robbins
} else {
$customerLastName = '';
}
// Check to make sure email exists and has a value
if (isset($order->customer->email) && $order->customer->email) {
$customerEmail = $order->customer->email; // arobbins@simpleblend.net
} else {
return; // Return immediately if email isn't set
}
// Create a new WordPres user based on the customer information
if ( !username_exists($customerEmail) && !email_exists($customerEmail) ) {
// Generates a random password 20 characters long and with special characters
$password = wp_generate_password(20, true);
// Creates the actual user using the email as the username
$userID = wp_create_user($customerEmail, $password, $customerEmail);
// Update the new user with the appropriate data from order
wp_update_user(array(
'ID' => $userID,
'nickname' => $customerFirstName . ' ' . $customerLastName,
'first_name' => $customerFirstName,
'last_name' => $customerLastName,
'description' => maybe_serialize($dynamicValues)
));
// Sets the user role
$user = new WP_User($userID);
$user->set_role('contributor');
// Sends the user an email with their new password
wp_mail($customerEmail, 'Welcome!', 'Your password is: ' . $password);
} else {
// User already exists, don't do anything
}
}
add_action('wps_webhook_checkouts_order_paid', 'namespace_checkouts_order_paid');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.