Problem when enrolling courses

I bought some courses, and for some reason, it does not enroll some of them.
For example, if I buy three courses, it doesn't enroll the second one. If I buy five, it doesn't enroll the second and the fourth. How can I fix this?

  • Vaughan
    • Support/SLS MockingJay

    Hi Danilo,

    Hope you're well?

    could you try updating to the latest 1.2.4.4 release as this may have fixed this issue, Also, can you make sure that marketpress is also updated to the latest release if you are actually selling courses.

    If that fails, can you please grant support access from the dashboard plugin so we can take a closer look.

    Thanks

    Hope this helps

  • Danilo
    • Flash Drive

    Hi Vaughan,
    Thanks by your answer, but we need to solve this problem. Please help us, because our store with Marketpress and Coursepress is already working, and we need to check out all the orders to enroll manually our custommers.

    There was 2 problems, one of them was already solved by a hardcode (that I think u need to ping the Coursepress Dev Team).

    1. First of all: the method "enroll_on_payment_confirmation" was:

    function enroll_on_payment_confirmation( $cart, $session ) {
    	if ( count( $cart ) > 0 ) {
    		$product_id	 = array_keys( $cart );
    		$product_id	 = end( $product_id );
    
    		$course_id = get_post_meta( $product_id, 'cp_course_id', true );
    
    		if ( !empty( $course_id ) ) {
    			$student			 = new Student( get_current_user_id() );
    			$existing_student	 = $student->has_access_to_course( $course_id );
    			if ( !$existing_student ) {
    				$student->enroll_in_course( $course_id );
    			}
    		}
    	} else {
    		cp_write_log( 'Error in cart. This should not happen.' );
    	}
    }

    At this function above, some times when it is called the student isn't enrolled at the course, so I did some changes like below:

    function enroll_on_payment_confirmation( $cart, $session ) {
    	if ( count( $cart ) > 0 ) {
    		$product_id = array_keys( $cart );
    		$course_id = array();
    
    		foreach ($product_id as $product) {
    			$course_id[] = get_post_meta( $product, 'cp_course_id', true );
    		}
    
    		foreach ($course_id as $course) {
    			if ( !empty($course) ) {
    				$student = new Student( get_current_user_id() );
    				$existing_student = $student->has_access_to_course( $course );
    				if ( !$existing_student ) {
    					$student->enroll_in_course( $course );
    				}
    			} else {
    				cp_write_log( 'Error in cart. This should not happen.' );
    			}
    		}
    	}
    }

    So now, the function is ok.

    So now, we have the second problem: The trigger. I think this method needs to be called when the order turns to "paid". At a moment, when create a new order at my payment gateway, the order can be created as "paid" or "not paid" like the sample:

    $paid = false;
    $mp->create_order($_SESSION['mp_order'], $cart, $shipping_info, $payment_info, $paid);

    So, when the order is created like the code above, the system automatically enrolls the student, but it can't happens. The enroll need to happen when the trigger "mp_order_paid" (when the order status is changed) or when create the order like this:

    $paid = true;
    $mp->create_order($_SESSION['mp_order'], $cart, $shipping_info, $payment_info, $paid);

    So, how can fix this?
    Please help us so fast as you can.

  • Michael Bissett
    • Recruit

    Hey @Danilo,

    We've released updates to MarketPress & CoursePress in the meantime, could you please update those, to see if that gets things going here?

    If not, then it sounds like we'll need to have a deeper look at this, could you please send in the following:

    - Mark to my attention, the subject line should contain only: ATTN: Michael Bissett
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - Include WordPress admin access details (login address, username & password)
    - Include FTP log-in details (hostname, username & password)
    - Include any relevant URLs for your site

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    https://premium.wpmudev.org/contact/

    (I realize that details were sent elsewhere, but I was not able to log in with those. If you could check to make sure that the details I'm presently asking for work on your end, and send them in the fashion noted, that will help make things quicker here)

    Thanks a bunch! :slight_smile:

    Kind Regards,
    Michael

  • Vaughan
    • Support/SLS MockingJay

    Hi Danillo,

    I have flagged one of our developers to have a look into this issue for you, hopefully he will know more regarding the code you have highlighted. He should respond shortly, but could take a little longer, i'll see if I can get him to take a quick look for you though.

    Thanks

  • Hoang Ngo
    • Code Slayer

    Hi there,

    I hope you are well today.

    Can you please upgrade to newest version (1.2.4.6), we have some issues with MarketPress hook in old version, the 1.2.4.6 has fixed it.

    I'm sorry for this inconvenience and many thanks for your patience.

    Best regards,
    Hoang

  • Danilo
    • Flash Drive

    The button's shortcode is ok, but the course one is still wrong. I'll send you the solution.

    function listen_for_paid_status_for_courses( $order ) {
    	global $mp;
    	$course_id = array();
    	$cart_info = array_keys($order->mp_cart_info);
    
    	foreach ($cart_info as $ids) {
    		$course_id[] =  get_post_meta( $ids, 'cp_course_id', true );
    	}
    
    	if ( $course_id && !empty( $course_id ) ) {
    		foreach ($course_id as $course) {
    			$student = new Student( $order->post_author );
    			$student->enroll_in_course( $course );
    		}
    	}
    }

    The issue is that it is getting only the first one of some courses. After I changed, it was fixed.

    Thank You!

  • Danilo
    • Flash Drive

    Sorry , I fixed a bug in the code.

    function listen_for_paid_status_for_courses( $order ) {
    			global $mp;
    			$course_id = array();
    			$cart_info = array_keys($order->mp_cart_info);
    
    			foreach ($cart_info as $ids) {
    	  			$course_id[] =  Course::get_course_id_by_marketpress_product_id($ids);
    			}
    
    			$qty_course_id = count($course_id);
    
    			if ( $course_id && !empty( $course_id ) ) {
    				        for($i=0; $i<$qty_course_id; $i++){
    	          				if ($course_id[$i] != false){
    	         	  				$student = new Student( $order->post_author );
    	 	          				$student->enroll_in_course( $course_id[$i] );
    	         				}
    	       			}
    			}
    	}

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.