Skip to content

Instantly share code, notes, and snippets.

Created April 30, 2017 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5d1abc445122059f7dc83af6894557a7 to your computer and use it in GitHub Desktop.
Save anonymous/5d1abc445122059f7dc83af6894557a7 to your computer and use it in GitHub Desktop.
function add_student_to_course($order_id) {
// Lets grab the order and the user id
$order = wc_get_order( $order_id );
$user_id = $order->user_id;
// This is how to grab line items from the order
$line_items = $order->get_items();
// This loops over line items
foreach ( $line_items as $item ) {
// This will be a product
$product = $order->get_product_from_item( $item );
// This is the product's id
$pid = $product->id;
// 2702 is the product id from woocommerce. 2429 is the course id from sensei
// If the product id is the one I'm looking for, then put it in the sensei course.
if($pid == 2702) {
WooThemes_Sensei_Utils::user_start_course($user_id, 2429);
}
}
}
@timgrahl
Copy link

You'll also need this:

add_action( 'woocommerce_order_status_completed', 'add_student_to_course' );

@kaypitre
Copy link

@timgrahl Where should I place this extra line of code? Thanks!

@l33t-daniel
Copy link

I tested this code and it worked out 100%. Perfect!

I will create a plugin to be used in the course settings page, so it is easier to configure, a metabox equal to what sensei woocommercer offers.

@l33t-daniel
Copy link

l33t-daniel commented May 10, 2019

@timgrahl Where should I place this extra line of code? Thanks!

add_action( 'woocommerce_order_status_completed', 'add_student_to_course' );
function add_student_to_course($order_id) {
	// Lets grab the order and the user id
	$order = wc_get_order( $order_id );
	$user_id = $order->user_id;
	
	// This is how to grab line items from the order 
	$line_items = $order->get_items();

	// This loops over line items
	foreach ( $line_items as $item ) {
		// This will be a product
		$product = $order->get_product_from_item( $item );
  
		// This is the product's id
		$pid = $product->id;
		
		// 2702 is the product id from woocommerce. 2429 is the course id from sensei
    // If the product id is the one I'm looking for, then put it in the sensei course.
		if($pid == 2702) {
			WooThemes_Sensei_Utils::user_start_course($user_id, 2429);
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment