Last active
October 7, 2018 16:01
-
-
Save shakeeb-mts/9ebb49927553cb578265703169eee98b to your computer and use it in GitHub Desktop.
Merge Subscriptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove woocommerce and woo-subscriptions delete actions. | |
* @return [type] [description] | |
*/ | |
function mts_remove_delete_actions() { | |
remove_action( 'delete_user', 'WC_Subscriptions_Manager::trash_users_subscriptions' ); | |
remove_action( 'deleted_user', 'wc_reset_order_customer_id_on_deleted_user' ); | |
} | |
add_action( 'init', 'mts_remove_delete_actions', 0 ); | |
/** | |
* Re-assign order and subscriptions to new user. | |
* @param [type] $user_id [description] | |
* @param [type] $reassign [description] | |
*/ | |
function mts_reset_customer_id_on_delete_user( $user_id, $reassign ) { | |
global $wpdb; | |
$reassign = is_null( $reassign ) ? 0 : $reassign; | |
// $wpdb->update( $wpdb->postmeta, array( 'meta_value' => $reassign ), array( 'meta_key' => '_customer_user', 'meta_value' => $user_id ) ); | |
// Get User Objects | |
$user = get_userdata( $user_id ); | |
$reassign_user = get_userdata( $reassign ); | |
// Re-Associate Orders | |
$user_orders = get_posts( | |
array( | |
'post_type' => array_merge( wc_get_order_types(), array( 'shop_subscription' ) ), | |
'post_status' => 'any', | |
'numberposts' => -1, | |
'meta_key' => '_customer_user', | |
'meta_value' => $user_id, | |
) | |
); | |
if ( ! empty( $user_orders ) ) { | |
foreach ( $user_orders as $order ) { | |
// Get Order Meta | |
$order_meta = get_post_meta( $order->ID ); | |
// Associate New User | |
update_post_meta( $order->ID, '_customer_user', $reassign, $user_id ); | |
// Update WC Information | |
update_post_meta( $order->ID, '_billing_first_name', $reassign_user->first_name, $user->first_name ); | |
update_post_meta( $order->ID, '_billing_last_name', $reassign_user->last_name, $user->last_name ); | |
update_post_meta( $order->ID, '_billing_email', $reassign_user->user_email, $user->user_email ); | |
update_post_meta( $order->ID, '_shipping_first_name', $reassign_user->first_name, $user->first_name ); | |
update_post_meta( $order->ID, '_shipping_last_name', $reassign_user->last_name, $user->last_name ); | |
} | |
} | |
// Re-Associate Product Downloadable Permissions | |
$user_downloads = wc_get_customer_available_downloads( $user_id ); | |
if ( ! empty( $user_downloads ) ) { | |
foreach ( $user_downloads as $download ) { | |
$download_order = new WC_Order( $download['order_id'] ); | |
wc_downloadable_file_permission( $download['download_id'], $download['product_id'], $download_order ); | |
} | |
} | |
} | |
add_action( 'deleted_user', 'mts_reset_customer_id_on_delete_user', 15, 2 ); | |
/** | |
* Re-assign user form on deleteion screen. | |
* @param [type] $current_user [description] | |
* @param [type] $userids [description] | |
* @return [type] [description] | |
*/ | |
function mts_delete_user_form( $current_user, $userids ) { | |
wp_enqueue_style( 'woocommerce_admin_styles' ); | |
wp_enqueue_style( 'select-2' ); | |
wp_enqueue_script( 'wc-enhanced-select' ); | |
?> | |
<fieldset id="mts-reassign-user-fieldset"> | |
<p><legend><?php _e( 'What should be done with content owned by this user?' ); ?></legend></p> | |
<ul style="list-style:none;"> | |
<li><label><input type="radio" id="delete_option0" name="delete_option" value="delete" /> | |
<?php _e('Delete all content.'); ?></label></li> | |
<li><input type="radio" id="delete_option1" name="delete_option" value="reassign" /> | |
<?php echo '<label for="delete_option1">' . __( 'Attribute all content to:' ) . '</label> '; ?> | |
<select class="wc-customer-search" id="customer_user" name="reassign_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-allow_clear="true" style="width:50%;"> | |
<option value="" selected="selected"></option> | |
</select> | |
</li> | |
</ul> | |
</fieldset> | |
<script> | |
jQuery(document).ready( function($) { | |
$('#mts-reassign-user-fieldset').prev().remove(); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'delete_user_form', 'mts_delete_user_form', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment