Skip to content

WooCommerce + GoToWebinar API + Piggybacking On a Plugin

WooCommerce Custom Field in Product Varaition

The objective is simply to have a completed sale via WooCommerce automatically register the customer for a webinar at GoToWebinar.com. The two main complicating factors are 1) GTW’s complicated API, which requires multiple authentication steps, including the re-acquisition of time-limited tokens via a custom GTW app, and 2) addition of a custom field to WooCommerce product variations.

To achieve the first goal, rather than write a completely new application or fork the existing one, I found it more practical to install a plugin from the WordPress repo – GoToWP, – and utilize its already reasonably well-developed authentication and synchronization features. All of the code is therefore enclosed in a function_exists() conditional, and the main new function is a custom function that in turn relies on options settings derived from the plugin.

The downside of this approach is that it relies on GoToWP remaining reasonably well-updated. However, that will be a risk with any application based on an API, since those are subject to change – as anyone coding for Facebook or Instagram or Twitter, et al, can attest. The plugin authors have been in business since 2012, and maintain a site selling numerous GoTo… plugins, so they seem as good a bet as any, even if they haven’t updated this particular plugin in around a year. (If GoToWP falls behind, then a new set of functions to replace gotowp_personal_get_organizer_key(), gotowp_personal_get_access_token(), and also gotowp_personal_json_decode will have to be written, as well as new admin page code for setting and syncing if desired.)

//don't do any of this if Go to Webinar plugin is not installed
if ( function_exists( 'gotowp_personal_get_organizer_key' ) ) :

/**
 * Upon completion of WooCommerce payment, create Webinar Registrant 
 * using WooCommerce Order data and Product Variation Custom Field
**/
add_action( 'woocommerce_payment_complete', 'gotoweb_wooc_add_on', 10, 1 ) ;
 
function gotoweb_wooc_add_on( $order_id ) {
	 
	$order = wc_get_order( $order_id ); 
	$form_data = array( 
		
		'firstName'		=> $order->get_billing_first_name(),
		'lastName'		=> $order->get_billing_last_name(),
		'email'			=> $order->get_billing_email(),
	
	) ; 
	
	$items = $order->get_items();
	
	foreach ( $items as $item ) {
		 
		$webinar_id = get_post_meta( $item->get_variation_id(), 'webinar_id', true ) ;  
		
		if ( $webinar_id ) {
			
			$registrant = ckm_gotowp_personal_create_registrant( $webinar_id, $form_data ) ; 
		
		}
		
	} 

}
/**
 *Use "gotowp_personal' options for authentication purposes
**/
function ckm_gotowp_personal_create_registrant( $webinar_id, $form_data ) {

    //two critical GoToWP Personal functions 
    $organizer_key = gotowp_personal_get_organizer_key(); 
    $access_token = gotowp_personal_get_access_token(); 

    $gtw_url = 'https://api.getgo.com/G2W/rest/v2/organizers/' . $organizer_key . '/webinars/' . $webinar_id . '/registrants';

    $json_feed = wp_remote_post($gtw_url, array(
	
        'headers' => array(
            'Authorization' => $access_token,
            'Accept' => 'application/vnd.citrix.g2wapi-v1.1+json',
        ), 
        'body' => json_encode( $form_data ),
		
    ));

    $json_response = wp_remote_retrieve_response_code( $json_feed );  
	
    if ( $json_response == 201 ) {
		
        $json_data = wp_remote_retrieve_body(  $json_feed  ); 

       //critical GoToWP Personal function
        $registrant = gotowp_personal_json_decode( $json_data );
		
        if ( isset( $registrant->registrantKey ) ) {
			
            return $registrant;
			
       }
		
        return false;
		
    } else {
		
        return false;
		
    }
	
}
/** CREATE CUSTOM FIELD IN PRODUCT VARIATIONS
 * Code framework from https://businessbloomer.com/woocommerce-add-custom-field-product-variation/
 **/
// Add custom field input @ Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'ckm_add_custom_field_to_variations', 10, 3 );
 
function ckm_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
	
	woocommerce_wp_text_input( array( 
		'id' => 'webinar_id[' . $loop . ']',
		'class' => 'short',
		'label' => __( 'Webinar ID', 'woocommerce' ),
		'value' => get_post_meta( $variation->ID, 'webinar_id', true )
		)
	);
	
}
//Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'ckm_save_custom_field_variations', 10, 2 );
 
function ckm_save_custom_field_variations( $variation_id, $i ) {
	
	$custom_field = $_POST['webinar_id'][$i];
	
	if ( isset( $custom_field ) ) { 
		
		update_post_meta( $variation_id, 'webinar_id', esc_attr( $custom_field ) );
		
	}
	
}
//Store custom field value into variation data
 
add_filter( 'woocommerce_available_variation', 'ckm_add_custom_field_variation_data' );
 
function ckm_add_custom_field_variation_data( $variations ) {
	
	$variations['webinar_id'] = '<div class="woocommerce_custom_field">Webinar ID: <span>' . get_post_meta( $variations[ 'webinar_id' ], 'webinar_id', true ) . '</span></div>';
	
	return $variations;
	
}

endif; //if GTW plugin active

Leave a Reply