WooCommerce::add_earning_data( $item_id,  $item,  $order_id )

Add earning data and split the earning between instructors


Parameters

$item_id

(Required)

$item

(Required)

$order_id

(Required)


Source

File: classes/WooCommerce.php

	public function add_earning_data( $item_id, $item, $order_id ) {
		$enable_earning = (bool) dozent_get_option( 'enable_dozent_earning' );
		if ( ! $enable_earning ) {
			return;
		}

		global $wpdb;
		$item = new \WC_Order_Item_Product( $item );

		$product_id = $item->get_product_id();
		$course_ids = dozent_get_attached_course_ids_to_product( $product_id );

		if ( dozent_count( $course_ids ) ) {

			$total_courses    = count( $course_ids );
			$total_price      = $item->get_total();
			$per_course_price = $total_price;

			/**
			 * Divide Split course price into all courses
			 * so each attached course will get the earning based on equal amount
			 */

			if ( $per_course_price > 0 && $total_courses > 1 ) {
				$per_course_price = $total_price / $total_courses;
			}

			$enable_fees_deducting = (bool) dozent_get_option( 'dozent_earning_fees.enable_fees_deducting' );
			$fees_name             = dozent_get_option( 'dozent_earning_fees.fees_name' );
			$deduct_fees_rate      = dozent_get_option( 'dozent_earning_fees.fees_amount' );
			$fees_type             = dozent_get_option( 'dozent_earning_fees.fees_type' );

			$fees_deduct_data = array();

			if ( $enable_fees_deducting ) {
				$fees_amount = $deduct_fees_rate;

				if ( $deduct_fees_rate > 0 ) {
					if ( $fees_type === 'percent' ) {
						$fees_amount = ( $total_price * $deduct_fees_rate ) / 100;
					}

					if ( $fees_amount > 0 && $total_courses > 1 ) {
						$fees_amount = $fees_amount / $total_courses;
					}

					//If fis amount fixed, it will deduct direct $fees_amount

					$per_course_price = $per_course_price - $fees_amount;
				}

				$fees_deduct_data = array(
					'deduct_fees_rate'   => $deduct_fees_rate,
					'deduct_fees_amount' => $fees_amount,
					'deduct_fees_name'   => $fees_name,
					'deduct_fees_type'   => $fees_type,
				);
			}

			$instructor_rate = dozent_get_option( 'earning_instructor_commission' );
			$admin_rate      = dozent_get_option( 'earning_admin_commission' );

			$instructor_amount = 0;
			if ( $instructor_rate > 0 ) {
				$instructor_amount = ( $per_course_price * $instructor_rate ) / 100;
			}

			$admin_amount = 0;
			if ( $admin_rate > 0 ) {
				$admin_amount = ( $per_course_price * $admin_rate ) / 100;
			}

			foreach ( $course_ids as $course_id ) {

				$user_id      = $wpdb->get_var( "SELECT post_author FROM {$wpdb->posts} WHERE ID = {$course_id} " );
				$order_status = $wpdb->get_var( "SELECT post_status FROM {$wpdb->posts} WHERE ID = {$order_id} " );

				$order_status = str_replace( 'wc-', '', strtolower( $order_status ) );

				$earning_data = array(
					'user_id'                   => $user_id,
					'course_id'                 => $course_id,
					'order_id'                  => $order_id,
					'order_status'              => $order_status,
					'total_courses_under_order' => $total_courses,
					'course_price_total'        => $total_price,
					'per_course_price'          => $per_course_price,
					'instructor_amount'         => $instructor_amount,
					'instructor_rate'           => $instructor_rate,
					'admin_amount'              => $admin_amount,
					'admin_rate'                => $admin_rate,
					'commission_type'           => 'percent',
					'process_by'                => 'WooCommerce',
					'created_at'                => date( 'Y-m-d H:i:s', dozent_time() ),
				);

				$earning_data['raw_data'] = json_encode( $earning_data );

				$earning_data = apply_filters( 'dozent_new_earning_data',
					array_merge( $earning_data, $fees_deduct_data ) );

				$wpdb->insert( $wpdb->prefix . 'dozent_earnings', $earning_data );
			}
		}

	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.