• Hi,
    I need to know how to generate automatically an order with completed status with particular courses, relative a user, when a custom event occurs.
    There is an hook or some functions of the plugin I can execute?
    In other words I need to auto enroll an user to courses when a custom event occurs. The user need to see these courses in his purchased courses list.
    I think I need to use ‘wp_insert_post’ and create a post with post type ‘lp_order’ can you help me posting me the structure of the post I have to insert, how to insert courses to the order?
    Thanks

Viewing 1 replies (of 1 total)
  • Thread Starter steplab

    (@steplab)

    Waiting for your answer, this is a first draft of my code:

    
    function payment_complete( $order_id, $old_status, $new_status ){
            if( $new_status == "completed" ) {
                
    			
    			$order = wc_get_order( $order_id );
    			
    			$subscribe_to_lp_courses = array();
    			
    			//get order user id
    				$user_order = $order->get_user_id();
    			
    			
    			//loop order products - start
    			
    			$order_items = $this->get_order_report_data(
    				array(
    					'data'         => array(
    						'_product_id' => array(
    							'type'            => 'order_item_meta',
    							'order_item_type' => 'line_item',
    							'function'        => '',
    							'name'            => 'product_id',
    						),
    						'_line_total' => array(
    							'type'            => 'order_item_meta',
    							'order_item_type' => 'line_item',
    							'function'        => 'SUM',
    							'name'            => 'order_item_amount',
    						),
    						'post_date'   => array(
    							'type'     => 'post_data',
    							'function' => '',
    							'name'     => 'post_date',
    						),
    					),
    					'group_by'     => 'ID, product_id, post_date',
    					'query_type'   => 'get_results',
    					'filter_range' => true,
    				)
    			);
    
    			if ( is_array( $order_items ) ) {
    
    				foreach ( $order_items as $order_item ) {
    					
    					//get post_id of order products
    					//$product_ID = $order_item->ID;
    					$product_id = $order_item->product_id;
    					
    						//get post meta of product lp_course
    						$lp_course = get_post_meta( $product_id, 'lp_course', true);
    						
    							//if get post meta is set add to an array $subscribe_to_lp_courses
    							if( $lp_course !== false && !empty( $lp_course ) && $lp_course != array() ) $subscribe_to_lp_courses[] = $lp_course;
    				}
    			}	
    			//loop order products - end	
    			
    			//if is set $subscribe_to_lp_courses - start
    			if ( count( $subscribe_to_lp_courses ) > 0 ) {
    				
    				//create learnpress order  lp_order
    				
    				//learn_press_auto_enroll_user_to_courses?
    				
    				
    				// Create order
    						//$order_id = $this->create_order();
    				
    				$order = new LP_Order();
    				
    				$order_data = array();
    				
    				$order_id = $order->get_id();
    				
    				$order->set_status( learn_press_default_order_status( 'lp-' ) );
    				
    				//assign to the order user id
    					$order->set_user_id( absint( $user_order ) );
    					
    				$order_id = $order->save();
    				
    				
    				//add order items (with $subscribe_to_lp_courses)
    				
    					foreach( $subscribe_to_lp_courses as $lp_course_id ){
    						
    						$item['item_id'] = $lp_course_id;
    						
    						$course = learn_press_get_course( $item );
    						
    						$item['order_item_name'] = $course->get_title();
    						
    						$item_id = $order->add_item( $item );
    					
    					}
    					
    					
    				//other settings order
    					//...
    				//$order->set_payment_method( $this->payment_method );
    				
    				$order_id = $order->save();
    				
    				if ( ! $order_id || is_wp_error( $order_id ) ) {
    					//echo 'Error Order creation failed.';
    				}
    					
    					
    			}		
    			//if is set $subscribe_to_lp_courses - end		
            }
    }
    add_action( 'woocommerce_order_status_changed', array( $this, 'payment_complete'), 99, 3 );
    
    
Viewing 1 replies (of 1 total)

The topic ‘New Order Automatically’ is closed to new replies.