Skip to content

Instantly share code, notes, and snippets.

@PluginRepublicSupport
Last active September 4, 2023 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PluginRepublicSupport/69c37ef2a8b0ca0e81f9ad0fa0f456a5 to your computer and use it in GitHub Desktop.
Save PluginRepublicSupport/69c37ef2a8b0ca0e81f9ad0fa0f456a5 to your computer and use it in GitHub Desktop.
Some compatibility functions between Deposits and Part Payments and Tutor LMS
<?php
/**
* This function updates a 'tutor_enrolled' post type after it is created by a scheduled order. Changing the status prevents them from being counted towards totals.
*/
function wcdpp_tutor_after_enrolled( $course_id, $user_id, $enrolled_id ) {
// get the order_id attached to this tutor_enrolled record
$order_id = get_post_meta( $enrolled_id, '_tutor_enrolled_by_order_id', true );
if ( $order_id ) {
// check if order is a scheduled order
$is_scheduled_order = get_post_meta( $order_id, '_is_scheduled_order', true );
if ( 'yes' == $is_scheduled_order ) {
$post = get_post( $enrolled_id );
if ( 'completed' == $post->post_status ) {
// change to something else since this is attached to a scheduled order
$post->post_status = "wcdpp_scheduled";
wp_update_post( $post );
}
}
}
// maybe also check previous enrolled records
wcdpp_fix_tutor_enrolled_by_scheduled_orders();
}
add_action('tutor_after_enrolled', 'wcdpp_tutor_after_enrolled', 11, 3);
/**
* This function updates existing/previously added 'tutor_enrolled' posts that were added by a scheduled order, so that they don't count towards totals.
*/
function wcdpp_fix_tutor_enrolled_by_scheduled_orders() {
global $wpdb;
$query = sprintf( "SELECT a.ID as `enrolled_id`, a.`post_status`, b.`meta_value` AS `order_id`, c.*
FROM %s a
LEFT JOIN %s b ON a.`ID` = b.`post_id`
LEFT JOIN %s c ON b.`meta_value` = c.`post_id`
WHERE
a.`post_type` = 'tutor_enrolled' AND
a.`post_status` != 'wcdpp_scheduled' AND
b.`meta_key` = '_tutor_enrolled_by_order_id' AND
c.`meta_key` = '_is_scheduled_order' AND
c.`meta_value` = 'yes' ",
$wpdb->prefix.'posts',
$wpdb->prefix.'postmeta',
$wpdb->prefix.'postmeta'
);
$results = $wpdb->get_results( $query, ARRAY_A );
if ( @count( $results ) > 0 ) {
foreach ( $results as $row ) {
$query2 = sprintf( " UPDATE %s SET `post_status` = 'wcdpp_scheduled' WHERE `ID` = %d ", $wpdb->prefix.'posts', $row['enrolled_id'] );
$wpdb->get_results( $query2 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment