Lookign for help with a form/script

Hoping to get some help (PHP is not my strong suit!) I have a form+script currently working using Gravity Forms Product Add-Ons, and I am moving to using GravityWiz Product Configurator. Got pointed in the right direction by GravityWiz on what needs to change, I have a general idea where, but no real idea on implementation.

The script takes a user's name and email from a child form and uses it to register a student via the Canvas (Instructure) API.

The notes from GaravityWiz:

"It looks like you might need to replace the order item meta keys to work with GS Product Configurator. GSPC adds the following meta keys to the woocommerce_order_itemmeta table:

  • gspc_gf_form_id
  • gspc_gf_entry_ids "

The existing script:

/**--------------------------------------

Canvas API Integration

--------------------------------------**/

//Function to filter the mail content type

function set_html_mail_content_type() {

return 'text/html';

}

add_action( 'woocommerce_order_status_completed', 'canvas_enroll', 10, 2 );

//add_action( 'woocommerce_thankyou', 'canvas_enroll', 20, 2 );

function canvas_enroll($order_id) {

$order = wc_get_order( $order_id);

$order_id = array(

'orderby' => 'date',

'order' => 'DESC',

'return' => 'ids',

);

if(!empty($order) && isset($order)){

// Loop through order line items

foreach( $order->get_items() as $item ){

// get order item data (in an unprotected array)

//Use the variables below to check if product is an Online course

//$url\_string = $item->get\_meta( '\_gravity\_forms\_history')\["\_gravity\_form\_lead"\]\["source\_url"\];

//$online = 'online';



//$product = $item->get\_product();

$product = $item\['variation\_id'\];



$canvas\_id\_check = get\_post\_meta( $product, 'canvas\_id', true);

$canvas\_ids = explode(',', get\_post\_meta( $product, 'canvas\_id', true)); //Using an array so we can accept multiple values 

	

if($canvas\_id\_check != null) {

	

	require\_once(get\_stylesheet\_directory().'/canvas\_api.class.php');

	$canvas = new CanvasAPI();



	if(isset($item->get\_meta( '\_gravity\_forms\_history')\["\_gravity\_form\_lead"\]\["form\_id"\]) && !empty($item->get\_meta( '\_gravity\_forms\_history')\["\_gravity\_form\_lead"\]\["form\_id"\])){ 

$linked_entry=$item->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];

$entry_id = $linked_entry;

$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form

if(isset($item->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21']) && !empty($item->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21'])){

$linked_nested_value = $item->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21'];

$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries

$child_entry_amt = substr_count($linked_nested_value, ",") + 1;

if ($child_entry_amt > 0){

for ($n = 0; $n < $child_entry_amt; $n++) {

$entry_id_nest[$n]=$nested_value_array[$n];

$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry

$firstname[$n] = $entry_nest[$n]['102.3'];//replace 102.3 with nested field id of first name

$lastname[$n] = $entry_nest[$n]['102.6'];//replace 102.6 with nested field id of last name

$email[$n] = $entry_nest[$n]['11']; //11 is the GF nested field id of email

$user_id = $canvas->find_or_create_user($email[$n], $firstname[$n].' '.$lastname[$n]);

foreach ($canvas_ids as $canvas_id) {

if ($canvas->enroll($user_id, $canvas_id)) {

//Send an email to report Success

} else {

//Send an email to report the error

}

}

}

}

} //end of child form section

		}//end of parent form section		  

	} // end of canvas id check	

  }	// do something for each item in order		

} // check that we've placed an order

} // function wrapper

Thank you for any insights!

Posted Aug 30, 2024, 5:12 PM UTC

10
Loading comments…