In case someone needs this, I solved it myself after looking at the documentation.
/**
* Autocomplete orders and enroll students
*
*/
function auto_update_orders_status_from_processing_to_completed(){
global $wpdb;
// Get all current "processing" customer orders
$processing_orders = wc_get_orders( $args = array(
'numberposts' => -1,
'post_status' => 'wc-processing',
) );
// If there are processing orders
if(!empty($processing_orders)) {
// Browse orders
foreach($processing_orders as $order)
// Update the order to completed
$order->update_status( 'completed' );
// Get order ID and WooCommerce order ID
$order_id = $order->ID;
$order = wc_get_order( $order_id );
// Get User ID From Order
$user = $order->get_user();
$user_id = $order->get_user_id();
// Get product from order
$items = $order->get_items();
// Browse products
foreach ( $items as $item ) {
// Product ID
$product_id = $item->get_product_id();
echo $user_id;
// Retrive Related Course ID
$sql = "SELECT post_id FROM jungle_postmeta WHERE meta_key = '_tutor_course_product_id' AND meta_value = $product_id";
$results = $wpdb->get_results($sql);
$course_id = $results[0]->post_id;
// Enroll user
$title = __('Course Enrolled', 'tutor')." – ".date_i18n(get_option('date_format')) .' @ '.date_i18n(get_option('time_format') ) ;
$enrolment_status = 'completed';
$enroll_data = apply_filters('tutor_enroll_data',
array(
'post_type' => 'tutor_enrolled',
'post_title' => $title,
'post_status' => $enrolment_status,
'post_author' => $user_id,
'post_parent' => $course_id,
)
);
// Insert the post into the database
$isEnrolled = wp_insert_post( $enroll_data );
update_user_meta( $user_id, '_is_tutor_student', time() );
update_post_meta( $isEnrolled, '_tutor_enrolled_by_order_id', $order_id );
update_post_meta( $isEnrolled, '_tutor_enrolled_by_product_id', $product_id );
update_post_meta( $order_id, '_is_tutor_order_for_course', time() );
update_post_meta( $order_id, '_tutor_order_for_course_id_'.$course_id, $order_id );
}
}
}
add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' );
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
if ( ! $order->has_status('completed') && $order->get_meta('_order_processed') != 'yes') {
$order->update_meta_data('_order_processed', 'yes');
$status = 'completed';
}
return $status;
}
Hope it can help someone !
Cheers and merry xmas ??