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: unenroll request when a refund is processed #41

Merged
merged 3 commits into from
Sep 27, 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
87 changes: 67 additions & 20 deletions admin/class-openedx-woocommerce-plugin-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,22 @@ public function add_custom_column_order_items() {
* Create a custom input in the new column in order items table
* to store the enrollment id and a link to the enrollment request
*
* @param array $_product Product object.
* @param array $product Product object.
* @param array $item Order item.
* @param int $item_id Order item id.
*
* @return void
*/
public function add_admin_order_item_values( $_product, $item, $item_id = null ) {
public function add_admin_order_item_values( $product, $item, $item_id = null ) {

// Check if the product has a non-empty "_course_id" metadata.
$_course_id = get_post_meta( $_product->get_id(), '_course_id', true );
$course_id = '';

if ( ! empty( $_course_id ) ) {
if ( $product ) {
$course_id = get_post_meta( $product->get_id(), '_course_id', true );
}

if ( ! empty( $course_id ) ) {

$order_id = method_exists( $item, 'get_order_id' ) ? $item->get_order_id() : $item['order_id'];
$input_value = get_post_meta( $order_id, 'enrollment_id' . $item_id, true );
Expand Down Expand Up @@ -354,37 +358,46 @@ public function process_order_data( $order_id ) {
if ( 'processing' === $status ) {

$billing_email = $order->get_billing_email();

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

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 );
$enrollment_id = $this->items_enrollment_request( $courses, $order_id, $billing_email, 'enroll' );
}
}
}

/**
* Select the items that are courses in the order.
*
* @param object $order Order object.
* @param array $items Order items array.
* @param bool $is_refunded Flag variable to know if the order is refunded.
*
* @return array $courses Array of courses.
*/
public function select_course_items( $order ) {
public function select_course_items( $items, $is_refunded = false ) {

$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,
);

if ( $is_refunded ) {
$courses[] = array(
'course_item' => $item,
'course_item_id' => $item->get_meta( '_refunded_item_id' ),
);
} else {
$courses[] = array(
'course_item' => $item,
'course_item_id' => $item_id,
);
}
}
}

Expand All @@ -397,17 +410,17 @@ public function select_course_items( $order ) {
* @param array $courses Array of courses.
* @param int $order_id Order id.
* @param string $billing_email Billing email.
* @param string $request_type Request type.
*
* @return void
*/
public function items_enrollment_request( $courses, $order_id, $billing_email ) {
public function items_enrollment_request( $courses, $order_id, $billing_email, $request_type ) {

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';
$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 );
$action = 'enrollment_process';

$enrollment_arr = array(
'enrollment_course_id' => $course_id,
Expand All @@ -419,7 +432,12 @@ public function items_enrollment_request( $courses, $order_id, $billing_email )

$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." );

if ( 'enroll' === $request_type ) {
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." );
} else {
wc_create_order_note( $order_id, 'Unenroll Request ID: ' . $enrollment_id->ID . " Click <a href='" . admin_url( 'post.php?post=' . intval( $enrollment_id->ID ) . '&action=edit' ) . "'>here</a> for details." );
}
}
}

Expand Down Expand Up @@ -458,4 +476,33 @@ public function check_api_response( $response ) {
return 'API did not provide a response';
}
}

/**
* Process unenrollment requests for courses in a refunded order.
* Loop through the refunded items, select courses, and send unenrollment requests to Open edX API.
*
* @param int $order_id Order id.
*
* @return void
*/
public function unenroll_course_refund( $order_id ) {

$order = wc_get_order( $order_id );
$billing_email = $order->get_billing_email();

if ( ! $order ) {
return;
}

$refunds = $order->get_refunds();

foreach ( $refunds as $refund ) {
$items = $refund->get_items();
$courses = $this->select_course_items( $items, true );
if ( ! empty( $courses ) ) {
wc_create_order_note( $order_id, 'Order items that are courses and refunded, obtained. ' );
$enrollment_id = $this->items_enrollment_request( $courses, $order_id, $billing_email, 'unenroll' );
}
}
}
}
1 change: 1 addition & 0 deletions includes/class-openedx-woocommerce-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ private function define_admin_hooks() {
);

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

/**
Expand Down