Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enrollment creation when an order is processed #40

Merged
merged 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions admin/class-openedx-woocommerce-plugin-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,126 @@ public function save_custom_product_fields( $post_id ) {
update_post_meta( $post_id, '_course_id', $course_id );
update_post_meta( $post_id, '_mode', $mode );
}

/**
* This function is called when an order payment is completed.
*
* @param int $order_id Order id.
*
* @return void
*/
public function process_order_data( $order_id ) {

$order = wc_get_order( $order_id );
$status = $order->get_status();
$enrollment_id = '';
$courses = array();

if ( 'processing' === $status ) {

$billing_email = $order->get_billing_email();

$courses = $this->select_course_items( $order, $billing_email );

if ( ! empty( $courses ) ) {
wc_create_order_note( $order_id, 'Order items that are courses, obtained. ' );
$enrollment_id = $this->items_enrollment_request( $courses, $order_id, $billing_email );
}
}
}

/**
* Select the items that are courses in the order.
*
* @param object $order Order object.
*
* @return array $courses Array of courses.
*/
public function select_course_items( $order ) {

$items = $order->get_items();
$courses = array();

foreach ( $items as $item_id => $item ) {
$product_id = $item->get_product_id();
$course_id = get_post_meta( $product_id, '_course_id', true );

if ( '' !== $course_id ) {
$courses[] = array(
'course_item' => $item,
'course_item_id' => $item_id,
);
}
}

return $courses;
}

/**
* This function processes the enrollment request for each course in the order.
*
* @param array $courses Array of courses.
* @param int $order_id Order id.
* @param string $billing_email Billing email.
*
* @return void
*/
public function items_enrollment_request( $courses, $order_id, $billing_email ) {

foreach ( $courses as $item_id => $item ) {

$course_id = get_post_meta( $item['course_item']->get_product_id(), '_course_id', true );
$course_mode = get_post_meta( $item['course_item']->get_product_id(), '_mode', true );
$request_type = 'enroll';
$action = 'enrollment_process';

$enrollment_arr = array(
'enrollment_course_id' => $course_id,
'enrollment_email' => $billing_email,
'enrollment_mode' => $course_mode,
'enrollment_request_type' => $request_type,
'enrollment_order_id' => $order_id,
);

$enrollment_id = $this->openedx_enrollment->insert_new( $enrollment_arr, $action, $order_id );
update_post_meta( $order_id, 'enrollment_id' . $item['course_item_id'], $enrollment_id->ID );
wc_create_order_note( $order_id, 'Enrollment Request ID: ' . $enrollment_id->ID . " Click <a href='" . admin_url( 'post.php?post=' . intval( $enrollment_id->ID ) . '&action=edit' ) . "'>here</a> for details." );
}
}

/**
* Shows the API logs in the order notes.
*
* @param int $order_id Order id.
* @param array $enrollment_api_response The API response.
*
* @return void
*/
public function show_enrollment_logs( $order_id, $enrollment_api_response ) {
$response = $this->check_api_response( $enrollment_api_response );
wc_create_order_note( $order_id, $response );
}

/**
* Check the API response from the Open edX API to show a simple message about the response.
* The message will be displayed in order notes.
*
* @param array $response The API response.
*
* @return string $response The API response.
*/
public function check_api_response( $response ) {

switch ( $response[0] ) {

case 'error':
return 'Open edX platform response: ' . $response[1];

case 'success':
return 'The Open edX platform processed the request.';

default:
return 'API did not provide a response';
}
}
}
3 changes: 3 additions & 0 deletions includes/class-openedx-woocommerce-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use App\admin\Openedx_Woocommerce_Plugin_Admin;
use App\public\Openedx_Woocommerce_Plugin_Public;
use App\admin\views\Openedx_Woocommerce_Plugin_Settings;
use App\model\Openedx_Woocommerce_Plugin_Enrollment;

/**
* This class contains the function to register a new custom post type.
Expand Down Expand Up @@ -241,6 +242,8 @@ private function define_admin_hooks() {
$plugin_admin,
'save_custom_product_fields'
);

$this->loader->add_action( 'woocommerce_order_status_changed', $plugin_admin, 'process_order_data', 10, 2 );
}

/**
Expand Down
16 changes: 13 additions & 3 deletions includes/model/class-openedx-woocommerce-plugin-enrollment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace App\model;

use App\model\Openedx_Woocommerce_Plugin_Log;
use App\Openedx_Woocommerce_Plugin;
use App\admin\Openedx_Woocommerce_Plugin_Admin;
use App\model\Openedx_Woocommerce_Plugin_Api_Calls;

if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -273,10 +275,11 @@ public function save_action( $post_id, $post ) {
*
* @param array $enrollment_arr An array containing the enrollment info.
* @param string $enrollment_action The API action to perform once the wp process is done.
* @param int $order_id The order ID in case the enrollment is created from an order.
*
* @return object $post The post object.
*/
public function insert_new( $enrollment_arr, $enrollment_action = '' ) {
public function insert_new( $enrollment_arr, $enrollment_action = '', $order_id = null ) {
$this->unregister_save_hook();

$new_enrollment = array(
Expand All @@ -286,7 +289,7 @@ public function insert_new( $enrollment_arr, $enrollment_action = '' ) {
$post_id = wp_insert_post( $new_enrollment );
$post = get_post( $post_id );

$this->save_enrollment( $post, $enrollment_arr, $enrollment_action );
$this->save_enrollment( $post, $enrollment_arr, $enrollment_action, $order_id );
return $post;
}

Expand All @@ -296,8 +299,9 @@ public function insert_new( $enrollment_arr, $enrollment_action = '' ) {
* @param post $post The post object.
* @param array $enrollment_arr An array containing the enrollment info.
* @param string $enrollment_action The API action to perform once the wp process is done.
* @param int $order_id The order ID in case the enrollment is created from an order.
*/
public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) {
public function save_enrollment( $post, $enrollment_arr, $enrollment_action, $order_id = null ) {

$post_id = $post->ID;

Expand Down Expand Up @@ -339,6 +343,12 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) {
$api = new Openedx_Woocommerce_Plugin_Api_Calls();
$enrollment_api_response = $api->enrollment_send_request( $enrollment_data, $enrollment_action );
$this->log_manager->create_change_log( $post_id, $old_data, $enrollment_data, $enrollment_action, $enrollment_api_response );

if ( null !== $order_id ) {
$plugin_class = new Openedx_Woocommerce_Plugin();
$admin_class = new Openedx_Woocommerce_Plugin_Admin( $plugin_class->get_plugin_name(), $plugin_class->get_version() );
$admin_class->show_enrollment_logs( $order_id, $enrollment_api_response );
}
}


Expand Down