Last active
March 16, 2024 14:51
-
-
Save koko-ng/8dc40a83b23c9e74ab1847208a8f21dc to your computer and use it in GitHub Desktop.
Synchronize Woocommerce memberships with linked subsciptions
This file contains hidden or 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 | |
$user_membership_ids = new \WP_Query( array( | |
'post_type' => 'wc_user_membership', | |
'post_status' => wc_memberships_get_user_membership_statuses( false ), | |
'fields' => 'ids', | |
'nopaging' => true, | |
'suppress_filters' => 1, | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => '_subscription_id', | |
'type' => 'numeric', | |
'compare' => 'EXISTS' | |
), | |
), | |
) ); | |
$date_types = array_keys(wcs_get_subscription_date_types()); | |
if ( ! empty( $user_membership_ids->posts ) ) { | |
foreach ( $user_membership_ids->posts as $user_membership_id ) { | |
$subscription_id = get_post_meta($user_membership_id, '_subscription_id', true); | |
echo "Doing " . $subscription_id . "\n"; | |
$subscription = wcs_get_subscription( $subscription_id ); | |
$status = $subscription->get_status(); | |
var_dump($status); | |
do_action('woocommerce_subscription_status_updated', $subscription, $status, $status, 'Sync subscriptions' ); | |
foreach ( $date_types as $type ) { | |
$date = $subscription->get_date($type); | |
var_dump($type, $date); | |
do_action('woocommerce_subscription_date_updated', $subscription, $type, $date); | |
} | |
do_action('woocommerce_subscription_status_updated', $subscription, $status, $status, 'Sync subscriptions' ); | |
echo "\n"; | |
} | |
} else { | |
var_dump("No memberships linked with subscriptions"); | |
} |
This file contains hidden or 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
# Can be run with wp-cli | |
wp eval "$(cat fix.php)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment