Title: Programmatically Enroll Student
Last modified: March 16, 2018

---

# Programmatically Enroll Student

 *  [cartpauj](https://wordpress.org/support/users/cartpauj/)
 * (@cartpauj)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/programmatically-enroll-student/)
 * I’m trying to enroll a student programmatically when a payment occurs from a 
   3rd party service.
 * I dug through the code quite a bit and this is what I’ve come up with. Can you
   please tell me if there’s a better/easier or more correct way?
 *     ```
           $course_id = 31;
           $user = learn_press_get_user($obj->user_id, false);
   
           $item = array(
             'order_item_name' => 'ADHD Course',
             'item_id' => $course_id,
             'quantity' => 1,
             'subtotal' => $obj->amount,
             'total' => $obj->total
           );
   
           $order = learn_press_create_order();
           $order->set_customer_note( '' );
           $order->set_status( 'lp-completed' );
           $order->set_total( $obj->total );
           $order->set_subtotal( $obj->amount );
           $order->set_user_ip_address( learn_press_get_ip() );
           $order->set_user_agent( learn_press_get_user_agent() );
           $order->set_created_via( 'external' );
           $order->set_user_id( $obj->user_id );
           $order->save();
           $order->add_item( $item );
   
           $user->enroll($course_id, $order->id);
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Contributor [Ken Nguyen](https://wordpress.org/support/users/kendy73/)
 * (@kendy73)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/programmatically-enroll-student/#post-10088920)
 * Hi cartpauj,
 * I’m discussing with our team about your case 😉
 * Regards,
 * Ken
 *  [scarllettlins](https://wordpress.org/support/users/scarllettlins/)
 * (@scarllettlins)
 * [8 years ago](https://wordpress.org/support/topic/programmatically-enroll-student/#post-10327173)
 * Ken, could you please help me with a similar problem? There’s a whole month I
   contact Learnpress Support, and they didn’t help me yet. I losing money. I can’t
   create orders from my website backend. Every time I click on create order, and
   try changing client or course, don’t work. The only thing that happens, is the
   order code that turn into another number, but nothing happens.
 *  Plugin Contributor [Ken Nguyen](https://wordpress.org/support/users/kendy73/)
 * (@kendy73)
 * [8 years ago](https://wordpress.org/support/topic/programmatically-enroll-student/#post-10334237)
 * Hi scarllettlins,
 * Sorry to hear that. I will need to check your site more carefully. You can email
   me to [edusupport@thimpress.com](https://wordpress.org/support/topic/programmatically-enroll-student/edusupport@thimpress.com?output_format=md)
   with subject “scarllettlins – LearnPress Support”.
 *  [Gravity Extra](https://wordpress.org/support/users/efetech/)
 * (@efetech)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-enroll-student/#post-10609635)
 * Hi LearnPress team,
 * Looks like this is still not resolved yet right?
 * Thanks,
    Bryan.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Programmatically Enroll Student’ is closed to new replies.

 * ![](https://ps.w.org/learnpress/assets/icon-256x256.gif?rev=3254420)
 * [LearnPress - WordPress LMS Plugin for Create and Sell Online Courses](https://wordpress.org/plugins/learnpress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/learnpress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/learnpress/)
 * [Active Topics](https://wordpress.org/support/plugin/learnpress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/learnpress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/learnpress/reviews/)

## Tags

 * [Enroll](https://wordpress.org/support/topic-tag/enroll/)
 * [order](https://wordpress.org/support/topic-tag/order/)

 * 4 replies
 * 4 participants
 * Last reply from: [Gravity Extra](https://wordpress.org/support/users/efetech/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/programmatically-enroll-student/#post-10609635)
 * Status: not a support question