Created
April 30, 2017 21:29
-
-
Save anonymous/5d1abc445122059f7dc83af6894557a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Where should I place this extra line of code? Thanks!
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.
@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
You'll also need this:
add_action( 'woocommerce_order_status_completed', 'add_student_to_course' );