From c557b33792de0b59f909d2661e30f6706a89f8c9 Mon Sep 17 00:00:00 2001 From: Joshua Davis Date: Wed, 19 Apr 2023 05:43:07 -0400 Subject: [PATCH] Fix and improve support for PMPro (Paid Memberships Pro) While Tutor supports PMPro as a monetization engine, the current implementation of get_orders_by_user_id() returns an empty orders array. Fix this function to return orders captured in PMPro, implemented and tested by Joshua Davis (@a11smiles) - Add condition for PMPro and WooCommerce (both return shop_order for the post type). - Add support for querying PMPro subscriptions - Make the second inner join optional (for PMPro orders, the meta key _is_tutor_order_for_course isn't saved). Was originally opened by Joshua Davis @a11smiles as #419, - commits and log messages squashed, - rebased to current dev - and spaces at end of lines fixed by me. Originally-From: Joshua Davis --- classes/Utils.php | 60 +++++++++++++++++++++--- templates/dashboard/purchase_history.php | 10 +++- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/classes/Utils.php b/classes/Utils.php index 2545fa8a13..b39d6c14ce 100644 --- a/classes/Utils.php +++ b/classes/Utils.php @@ -2657,6 +2657,49 @@ public function get_course_enrolled_ids_by_order_id( $order_id ) { return false; } + /** + * @param $order_id + * + * @return array + * + */ + public function get_subscriptions_by_order_id( $order_id ) { + global $wpdb; + + // Return all children of a subscription + $args = array( + 'post_parent' => $order_id, + 'post_type' => 'shop_subscription' + ); + return get_children($args); + } + + /** + * @param $subscription_id + * + * @return object + * + */ + public function get_subscription_by_subscription_id( $subscription_id ) { + global $wpdb; + + // Returns subscription information + $subscription = $wpdb->get_row( + $wpdb->prepare( + "SELECT subscriptions.* + FROM {$wpdb->prefix}woocommerce_order_items items + LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta meta + ON items.order_item_id = meta.order_item_id + AND meta.meta_key = '_product_id' + LEFT JOIN $wpdb->posts subscriptions + ON meta.meta_value = subscriptions.ID + WHERE `order_id` = %s + AND `order_item_type` = 'line_item'", $subscription_id + ) + ); + return $subscription; + } + /** * Get wc product in efficient query * @@ -6020,7 +6063,7 @@ public function get_orders_by_user_id( $user_id = 0, $period = '', $start_date = $post_type = ''; $user_meta = ''; - if ( 'wc' === $monetize_by ) { + if ( 'wc' === $monetize_by || 'pmpro' === $monetize_by ) { $post_type = 'shop_order'; $user_meta = '_customer_user'; } elseif ( 'edd' === $monetize_by ) { @@ -6028,6 +6071,13 @@ public function get_orders_by_user_id( $user_id = 0, $period = '', $start_date = $user_meta = '_edd_payment_user_id'; } + $add_join = ''; + if ($monetize_by !== 'pmpro') { + $add_join = "INNER JOIN {$wpdb->postmeta} tutor_order + ON id = tutor_order.post_id + AND tutor_order.meta_key = '_is_tutor_order_for_course'"; + } + $period_query = ''; if ( '' !== $period ) { @@ -6054,11 +6104,9 @@ public function get_orders_by_user_id( $user_id = 0, $period = '', $start_date = "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} customer - ON id = customer.post_id - AND customer.meta_key = '{$user_meta}' - INNER JOIN {$wpdb->postmeta} tutor_order - ON id = tutor_order.post_id - AND tutor_order.meta_key = '_is_tutor_order_for_course' + ON id = customer.post_id + AND customer.meta_key = '{$user_meta}' + {$add_join} WHERE post_type = %s AND customer.meta_value = %d {$period_query} diff --git a/templates/dashboard/purchase_history.php b/templates/dashboard/purchase_history.php index 11768ad28e..8ce094274c 100644 --- a/templates/dashboard/purchase_history.php +++ b/templates/dashboard/purchase_history.php @@ -105,7 +105,7 @@ ID ); $price = tutor_utils()->tutor_price( $wc_order->get_total() ); $raw_price = $wc_order->get_total(); @@ -161,6 +161,14 @@ echo '
' . esc_html( get_the_title( $course['course_id'] ) ) . '
'; } } + if ( $monetize_by === 'pmpro') { + $subscriptions = tutor_utils()->get_subscriptions_by_order_id( $order->ID ); + if ( tutor_utils()->count( $subscriptions ) ) { + foreach ( $subscriptions as $subscription ) { + echo '
Subscription - ' . esc_html( tutor_utils()->get_subscription_by_subscription_id( $subscription->ID )->post_title ) . '
'; + } + } + } ?>