Skip to content

Commit

Permalink
feat: enrollment creation when an order is processed (#40)
Browse files Browse the repository at this point in the history
* feat: enrollment request when order succeeds

* feat: added logs in woocommerce order

* refactor: enrollment created message fixed

* docs: added docs to new functions

* refactor: course array as an associative array

* docs: check api specifies where the logs are displayed
  • Loading branch information
julianramirez2 authored Sep 25, 2023
1 parent 871b871 commit 729ae47
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
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

0 comments on commit 729ae47

Please sign in to comment.