From 6021b6c80beb35a5bddaaa05103219915400760d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Ramirez=20Giraldo?= <52968528+julianramirez2@users.noreply.github.com> Date: Wed, 27 Sep 2023 16:48:23 -0500 Subject: [PATCH] feat: unenroll request when a refund is processed (#41) * feat: unenroll request when a refund is processed * refactor: deleted underscore to product and course variable * refactor: variable refunded defaults to false --- ...class-openedx-woocommerce-plugin-admin.php | 87 ++++++++++++++----- includes/class-openedx-woocommerce-plugin.php | 1 + 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/admin/class-openedx-woocommerce-plugin-admin.php b/admin/class-openedx-woocommerce-plugin-admin.php index bb644ea..5527bc7 100644 --- a/admin/class-openedx-woocommerce-plugin-admin.php +++ b/admin/class-openedx-woocommerce-plugin-admin.php @@ -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 ); @@ -354,12 +358,12 @@ 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' ); } } } @@ -367,24 +371,33 @@ public function process_order_data( $order_id ) { /** * 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, + ); + } } } @@ -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, @@ -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 here for details." ); + + if ( 'enroll' === $request_type ) { + wc_create_order_note( $order_id, 'Enrollment Request ID: ' . $enrollment_id->ID . " Click here for details." ); + } else { + wc_create_order_note( $order_id, 'Unenroll Request ID: ' . $enrollment_id->ID . " Click here for details." ); + } } } @@ -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' ); + } + } + } } diff --git a/includes/class-openedx-woocommerce-plugin.php b/includes/class-openedx-woocommerce-plugin.php index afb30f2..083512b 100644 --- a/includes/class-openedx-woocommerce-plugin.php +++ b/includes/class-openedx-woocommerce-plugin.php @@ -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 ); } /**