Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active March 4, 2026 18:23
Show Gist options
  • Select an option

  • Save yuriinalivaiko/2d3d15a49aba9f8ef5758c0ffc7b157f to your computer and use it in GitHub Desktop.

Select an option

Save yuriinalivaiko/2d3d15a49aba9f8ef5758c0ffc7b157f to your computer and use it in GitHub Desktop.
Code snippets for the "WooCommerce" extension
<?php
// Enqueue the "select2" script earlier to avoid a conflict with "selectWoo".
add_action( 'admin_enqueue_scripts', 'um_fix_selectWoo_conflict' );
add_action( 'wp_enqueue_scripts', 'um_fix_selectWoo_conflict' );
function um_fix_selectWoo_conflict() {
if ( wp_script_is( 'select2' ) && wp_script_is( 'selectWoo' ) ) {
$wp_scripts = wp_scripts();
if ( ! in_array( 'select2', $wp_scripts->queue, true ) && isset( $wp_scripts->registered[ 'select2' ] ) ) {
array_unshift( $wp_scripts->queue, 'select2' );
$wp_scripts->add_data( 'selectWoo', 'group', 1 );
$wp_scripts->add_data( 'selectWoo', 'strategy', 'defer' );
}
}
}
<?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
// Change the default WooCommerce login redirect URL.
function um_woocommerce_login_redirect( $redirect, $user ) {
if ( function_exists( 'um_get_core_page' ) ) {
$redirect = um_get_core_page( 'account' );
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'um_woocommerce_login_redirect', 10, 2 );
<?php
// Redirect from the WooCommerce "My account" page to the Ultimate Member "Account" page.
function um_woocommerce_myaccount_redirect() {
global $wp_query;
if ( $wp_query->is_singular( 'page' ) && function_exists( 'um_get_core_page' ) ) {
$queried_page = $wp_query->get_queried_object();
$myaccount_page_id = (int) get_option( 'woocommerce_myaccount_page_id' );
if ( $queried_page->ID === $myaccount_page_id ) {
exit( wp_safe_redirect( um_get_core_page( 'account' ) ) );
}
}
}
add_action( 'template_redirect', 'um_woocommerce_myaccount_redirect' );
<?php
// Remove other roles when a new role is assigned on purchase.
add_action( 'um_woocommerce_after_member_role_upgrade', function( $new_roles, $old_roles, $order ) {
$assigned_roles = array_diff( (array) $new_roles, (array) $old_roles );
if ( $assigned_roles ) {
$assigned_role = end( $assigned_roles );
$user = $order->get_user();
$user->set_role( $assigned_role );
}
}, 20, 3 );
<?php
// Remove other roles when assigning a new role during a 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