#Overview
This helper function is available with Ontraport, Zoho, Salesforce, HubSpot, and other CRMs which use custom objects.
It’s a shortcut / alternative to using the wpf_crm_object_type filter, for cases where you just need to add a custom object one time, without changing the object type used for WP Fusion globally.
#Parameters
$data
: (array) An associative array of data to sync to the CRM, using CRM field IDs for the keys$object_type
: (string) The object type you wish to update
#Examples
#Create a new Lead in Salesforce
$data = array(
'Email' => '[email protected]',
'FirstName' => 'Jane',
'LastName' => 'Doe',
);
$lead_id = wp_fusion()->crm->add_object( $data, 'Lead' );
#Update an Event object in Zoho when an Event post type is updated in WordPress
// Runs on any post with post type "event" and updates the "Event" custom object with values Title and EventDate
function create_update_event_object( $post_id, $post, $update ) {
// Don't run if WP Fusion isn't active, otherwise you'll get an error
if ( ! function_exists( 'wp_fusion' ) ) {
return;
}
// This is the data to be sent to the CRM
$event_data = array(
'Title' => $post->post_title,
'EventDate' => get_post_meta( $post_id, 'event_date', true )
);
// See if this object has already been synced
$object_id = get_post_meta( $post_id, wp_fusion()->crm->slug . '_event_id', true );
if ( empty( $object_id ) {
// New event
$object_id = wp_fusion()->crm->add_object( $event_data, 'Event' );
if ( ! is_wp_error( $object_id ) ) {
// Save the ID of the new record for future updates.
update_post_meta( $post_id, wp_fusion()->crm->slug . '_event_id', $object_id );
} else {
// Error, log it.
wpf_log( 'error', 0, 'Error creating event:' . $object_id->get_error_message() );
}
} else {
// Existing event
wp_fusion()->crm->update_object( $object_id, $event_data, 'Event' );
}
}
// save_post_event runs whenever an "event" post type is created or updated (see https://developer.wordpress.org/reference/hooks/save_post_post-post_type/)
add_action( 'save_post_event', 'create_update_event_object', 10, 3 );
#Create a custom Car object in HubSpot and associate it with a contact
For more information on working with custom objects in HubSpot, see Custom Objects with HubSpot.
define( 'HUBSPOT_API_KEY', 'xx599590-7888-43ed-a896-5abbc2ef9aa2' );
$properties = array(
'condition' => 'used',
'date_received' => '1582416000000',
'year' => '2014',
'make' => 'Nissan',
'model' => 'Frontier',
'vin' => '4Y1SL65848Z411439',
'color' => 'White',
'mileage' => '80000',
'price' => '12000',
'notes' => 'Excellent condition. No accidents.',
);
$object_type_id = '2-4370788';
$object_id = wp_fusion()->crm->add_object( $properties, $object_type_id );
if ( is_wp_error( $object_id ) ) {
wpf_log( 'error', wpf_get_current_user_id(), 'Error adding object: ' . $object_id->get_error_message() );
return false;
}
// Do what you want with $object_id here.
// For example to associate it with a contact (https://developers.hubspot.com/docs/api/crm/crm-custom-objects).
$contact_id = '101';
$association_type_id = '3';
$request = "https://api.hubapi.com/crm/v3/objects/{$object_type_id}/{$object_id}/associations/contacts/{$contact_id}/{$association_type_id}/?hapikey=" . HUBSPOT_API_KEY;
$params = array( 'method' => 'PUT' );
$response = wp_safe_remote_request( $request, $params );
if ( is_wp_error( $response ) ) {
wpf_log( 'error', wpf_get_current_user_id(), 'Error associating object with contact: ' . $response->get_error_message() );
return false;
}
Was this helpful?
Let us know if you liked the post. That’s the only way we can improve.