Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active October 31, 2025 11:19
Show Gist options
  • Select an option

  • Save yuriinalivaiko/3a075c256c06fbe1507ff8894e185366 to your computer and use it in GitHub Desktop.

Select an option

Save yuriinalivaiko/3a075c256c06fbe1507ff8894e185366 to your computer and use it in GitHub Desktop.
Code snippets for the "WooCommerce" extension
<?php
// Update the role configuration for existing subscriptions when the related product-subscription is updated.
if ( is_plugin_active( 'um-woocommerce/um-woocommerce.php' ) && function_exists( 'wcs_get_subscription' ) ) {
add_action( 'save_post', 'um_woo_product_settings_to_subscriptions', 20, 2 );
}
/**
* Updates saved role settings in existing subscriptions using the product's role settings.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
*/
function um_woo_product_settings_to_subscriptions( $post_id, $post ) {
// make this handler only on product form submit.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( empty( $_REQUEST['_wpnonce'] ) ) {
return $post_id;
}
if ( empty( $post->post_type ) || 'product' !== $post->post_type ) {
return $post_id;
}
$product_id = $post_id;
$product = wc_get_product( $product_id );
// Check the product type. Make sure this is a product-subscription.
if ( !$product->is_type( array('subscription', 'variable-subscription') ) ) {
return $post_id;
}
// Product settings as status => meta_key.
$inputs = array(
'active' => '_um_woo_product_activated_role',
'pending' => '_um_woo_product_downgrade_pending_role',
'on-hold' => '_um_woo_product_downgrade_onhold_role',
'expired' => '_um_woo_product_downgrade_expired_role',
'cancelled' => '_um_woo_product_downgrade_cancelled_role',
'pending-cancel' => '_um_woo_product_downgrade_pendingcancel_role',
);
// Get subscriptions that contain a certain product, specified by ID.
$subscription_ids = wcs_get_subscriptions_for_product( $product_id );
foreach ( $subscription_ids as $subscription_id ) {
$subscription = wcs_get_subscription( $subscription_id );
if ( $subscription ) {
$user_id = $subscription->get_customer_id();
foreach ( $inputs as $status => $key ) {
if ( isset( $_POST[$key] ) ) {
$meta_key = '_um_woo_subscription_' . $subscription_id . '_product_' . $product_id . '_' . $status . '_role';
$meta_value = sanitize_text_field( $_POST[$key] );
update_user_meta( $user_id, $meta_key, $meta_value );
}
}
}
}
}
<?php
// Remove other roles when the role is changed due to the subscription status change.
add_action( 'um_woocommerce_after_member_role_upgrade', function( $new_roles, $old_roles, $subscription, $hook_name ) {
if ( 'woocommerce_subscription_status_changed' === $hook_name ) {
$assigned_roles = array_diff( (array) $new_roles, (array) $old_roles );
if ( $assigned_roles ) {
$assigned_role = end( $assigned_roles );
$user = $subscription->get_user();
$user->set_role( $assigned_role );
}
}
}, 20, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment