Created
January 28, 2025 16:43
-
-
Save contemplate/bf60a4556d7feab9c703325725b5130d to your computer and use it in GitHub Desktop.
WooCommerce Subscriptions Conditional Reminder Email Ability
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
/** | |
* Add a new "Subscription Reminders" tab in the Product Data panel | |
* for Simple Subscription products only. | |
*/ | |
add_filter( 'woocommerce_product_data_tabs', 'wcs_add_subscriptions_reminders_tab_simple' ); | |
function wcs_add_subscriptions_reminders_tab_simple( $tabs ) { | |
// Only add this tab for subscription products (excluding variable-subscriptions). | |
// The "show_if_subscription" class ensures the tab is visible only for "simple subscription". | |
$tabs['wcs_sub_reminders_simple'] = array( | |
'label' => __( 'Subscription Reminders', 'your-textdomain' ), | |
'target' => 'wcs_sub_reminders_options_simple', | |
'class' => array( 'show_if_subscription' ), | |
'priority' => 90, | |
); | |
return $tabs; | |
} | |
/** | |
* Render the content for the "Subscription Reminders" tab in Simple Subscriptions. | |
*/ | |
add_action( 'woocommerce_product_data_panels', 'wcs_add_sub_reminders_tab_content_simple' ); | |
function wcs_add_sub_reminders_tab_content_simple() { | |
echo '<div id="wcs_sub_reminders_options_simple" class="panel woocommerce_options_panel hidden">'; | |
// We'll use WooCommerce's helper function to output checkboxes. | |
// Each meta key is unique to a specific reminder email type. | |
woocommerce_wp_checkbox( array( | |
'id' => '_include_customer_notification_manual_trial_expiration', | |
'label' => __( 'Manual Trial Expiration reminder email', 'your-textdomain' ), | |
'description' => __( 'Enable sending the "Manual Trial Expiration" reminder for this product.', 'your-textdomain' ), | |
) ); | |
woocommerce_wp_checkbox( array( | |
'id' => '_include_customer_notification_auto_trial_expiration', | |
'label' => __( 'Auto Trial Expiration reminder email', 'your-textdomain' ), | |
'description' => __( 'Enable sending the "Auto Trial Expiration" reminder for this product.', 'your-textdomain' ), | |
) ); | |
woocommerce_wp_checkbox( array( | |
'id' => '_include_customer_notification_manual_renewal', | |
'label' => __( 'Manual Renewal reminder email', 'your-textdomain' ), | |
'description' => __( 'Enable sending the "Manual Renewal" reminder for this product.', 'your-textdomain' ), | |
) ); | |
woocommerce_wp_checkbox( array( | |
'id' => '_include_customer_notification_auto_renewal', | |
'label' => __( 'Auto Renewal reminder email', 'your-textdomain' ), | |
'description' => __( 'Enable sending the "Auto Renewal" reminder for this product.', 'your-textdomain' ), | |
) ); | |
woocommerce_wp_checkbox( array( | |
'id' => '_include_customer_notification_subscription_expiration', | |
'label' => __( 'Subscription Expiration reminder email', 'your-textdomain' ), | |
'description' => __( 'Enable sending the "Subscription Expiration" reminder for this product.', 'your-textdomain' ), | |
) ); | |
echo '</div>'; | |
} | |
/** | |
* Save the checkbox values when a simple subscription product is saved. | |
*/ | |
add_action( 'woocommerce_process_product_meta_subscription', 'wcs_save_sub_reminders_fields_simple' ); | |
function wcs_save_sub_reminders_fields_simple( $post_id ) { | |
// Each checkbox either saves "yes" or "no". | |
$fields = array( | |
'_include_customer_notification_manual_trial_expiration', | |
'_include_customer_notification_auto_trial_expiration', | |
'_include_customer_notification_manual_renewal', | |
'_include_customer_notification_auto_renewal', | |
'_include_customer_notification_subscription_expiration', | |
); | |
foreach ( $fields as $field ) { | |
$value = isset( $_POST[ $field ] ) ? 'yes' : 'no'; | |
update_post_meta( $post_id, $field, $value ); | |
} | |
} | |
/** | |
* Show "Subscription Reminder" checkboxes for each variation | |
* in a variable subscription product. | |
*/ | |
add_action( 'woocommerce_product_after_variable_attributes', 'wcs_variation_subscription_reminders_fields', 10, 3 ); | |
function wcs_variation_subscription_reminders_fields( $loop, $variation_data, $variation ) { | |
// We'll create 5 checkboxes (auto/manual trial expiration, auto/manual renewal, subscription expiration). | |
// Each checkbox is stored as meta on the variation itself. | |
woocommerce_wp_checkbox( array( | |
'id' => "_include_customer_notification_manual_trial_expiration_{$variation->ID}", | |
'label' => __( 'Manual Trial Expiration Reminder', 'your-textdomain' ), | |
'description' => __( 'Enable the "Manual Trial Expiration" reminder for this variation.', 'your-textdomain' ), | |
'value' => get_post_meta( $variation->ID, '_include_customer_notification_manual_trial_expiration', true ), | |
) ); | |
woocommerce_wp_checkbox( array( | |
'id' => "_include_customer_notification_auto_trial_expiration_{$variation->ID}", | |
'label' => __( 'Auto Trial Expiration Reminder', 'your-textdomain' ), | |
'description' => __( 'Enable the "Auto Trial Expiration" reminder for this variation.', 'your-textdomain' ), | |
'value' => get_post_meta( $variation->ID, '_include_customer_notification_auto_trial_expiration', true ), | |
) ); | |
woocommerce_wp_checkbox( array( | |
'id' => "_include_customer_notification_manual_renewal_{$variation->ID}", | |
'label' => __( 'Manual Renewal Reminder', 'your-textdomain' ), | |
'description' => __( 'Enable the "Manual Renewal" reminder for this variation.', 'your-textdomain' ), | |
'value' => get_post_meta( $variation->ID, '_include_customer_notification_manual_renewal', true ), | |
) ); | |
woocommerce_wp_checkbox( array( | |
'id' => "_include_customer_notification_auto_renewal_{$variation->ID}", | |
'label' => __( 'Auto Renewal Reminder', 'your-textdomain' ), | |
'description' => __( 'Enable the "Auto Renewal" reminder for this variation.', 'your-textdomain' ), | |
'value' => get_post_meta( $variation->ID, '_include_customer_notification_auto_renewal', true ), | |
) ); | |
woocommerce_wp_checkbox( array( | |
'id' => "_include_customer_notification_subscription_expiration_{$variation->ID}", | |
'label' => __( 'Subscription Expiration Reminder', 'your-textdomain' ), | |
'description' => __( 'Enable the "Subscription Expiration" reminder for this variation.', 'your-textdomain' ), | |
'value' => get_post_meta( $variation->ID, '_include_customer_notification_subscription_expiration', true ), | |
) ); | |
} | |
/** | |
* Save our "Subscription Reminders" checkboxes for each variation. | |
*/ | |
add_action( 'woocommerce_save_product_variation', 'wcs_save_variation_subscription_reminders_fields', 10, 2 ); | |
function wcs_save_variation_subscription_reminders_fields( $variation_id, $i ) { | |
$fields = array( | |
'_include_customer_notification_manual_trial_expiration', | |
'_include_customer_notification_auto_trial_expiration', | |
'_include_customer_notification_manual_renewal', | |
'_include_customer_notification_auto_renewal', | |
'_include_customer_notification_subscription_expiration', | |
); | |
foreach ( $fields as $field_key ) { | |
// The input name in the HTML is the field name plus the variation ID, | |
// e.g. "_include_customer_notification_auto_renewal_123" | |
$field_name = $field_key . '_' . $variation_id; | |
// If checkbox is checked, store "yes," otherwise "no." | |
$value = isset( $_POST[ $field_name ] ) ? 'yes' : 'no'; | |
update_post_meta( $variation_id, $field_key, $value ); | |
} | |
} | |
/** | |
* Helper: Determine if at least one item in the subscription has the given meta key set to "yes." | |
* | |
* @param WC_Subscription $subscription | |
* @param string $meta_key e.g. '_include_customer_notification_auto_renewal' | |
* @return bool True if at least one product or variation has that meta = "yes." | |
*/ | |
function wcs_subscription_has_enabled_meta( $subscription, $meta_key ) { | |
foreach ( $subscription->get_items() as $item ) { | |
$product = $item->get_product(); | |
if ( ! $product ) { | |
continue; | |
} | |
// If this is a variation (from a variable subscription): | |
if ( $product->is_type( 'variation' ) ) { | |
// Variation-level meta | |
$value = $product->get_meta( $meta_key ); | |
} else { | |
// Simple subscription or some other product type: | |
$value = $product->get_meta( $meta_key ); | |
} | |
if ( 'yes' === $value ) { | |
// Found at least one item that wants this email enabled. | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Auto Trial Expiration | |
*/ | |
add_filter( 'woocommerce_email_enabled_customer_notification_auto_trial_expiration', 'wcs_limit_auto_trial_expiration_email', 10, 2 ); | |
function wcs_limit_auto_trial_expiration_email( $enabled, $subscription ) { | |
if ( ! $enabled || ! $subscription ) { | |
return $enabled; | |
} | |
return wcs_subscription_has_enabled_meta( $subscription, '_include_customer_notification_auto_trial_expiration' ); | |
} | |
/** | |
* Manual Trial Expiration | |
*/ | |
add_filter( 'woocommerce_email_enabled_customer_notification_manual_trial_expiration', 'wcs_limit_manual_trial_expiration_email', 10, 2 ); | |
function wcs_limit_manual_trial_expiration_email( $enabled, $subscription ) { | |
if ( ! $enabled || ! $subscription ) { | |
return $enabled; | |
} | |
return wcs_subscription_has_enabled_meta( $subscription, '_include_customer_notification_manual_trial_expiration' ); | |
} | |
/** | |
* Auto Renewal | |
*/ | |
add_filter( 'woocommerce_email_enabled_customer_notification_auto_renewal', 'wcs_limit_auto_renewal_email', 10, 2 ); | |
function wcs_limit_auto_renewal_email( $enabled, $subscription ) { | |
if ( ! $enabled || ! $subscription ) { | |
return $enabled; | |
} | |
return wcs_subscription_has_enabled_meta( $subscription, '_include_customer_notification_auto_renewal' ); | |
} | |
/** | |
* Manual Renewal | |
*/ | |
add_filter( 'woocommerce_email_enabled_customer_notification_manual_renewal', 'wcs_limit_manual_renewal_email', 10, 2 ); | |
function wcs_limit_manual_renewal_email( $enabled, $subscription ) { | |
if ( ! $enabled || ! $subscription ) { | |
return $enabled; | |
} | |
return wcs_subscription_has_enabled_meta( $subscription, '_include_customer_notification_manual_renewal' ); | |
} | |
/** | |
* Subscription Expiration | |
*/ | |
add_filter( 'woocommerce_email_enabled_customer_notification_subscription_expiration', 'wcs_limit_subscription_expiration_email', 10, 2 ); | |
function wcs_limit_subscription_expiration_email( $enabled, $subscription ) { | |
if ( ! $enabled || ! $subscription ) { | |
return $enabled; | |
} | |
return wcs_subscription_has_enabled_meta( $subscription, '_include_customer_notification_subscription_expiration' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Summary: Adds ability to enable woocommerce subscription reminders for only certain subscription products. First this feature must be enabled globally ( WooCommerce -> Settings -> Subscriptions (tab) ) and then each subscription product will show checkboxes to enable particular emails on a per product basis.
Simple Subscription: A custom “Subscription Reminders” tab in Product Data (only visible for type=subscription) for the parent product. The chosen checkboxes save as post meta (e.g. _include_customer_notification_auto_renewal = yes/no).
Variable Subscription: Each variation can be configured independently via five new checkboxes in the Variations panel. These also save as meta on the variation.
Email Filters: When a subscription triggers a reminder email, we check all items in the subscription. If any item (simple or variation) has the relevant meta set to yes, we allow that email. Otherwise, it’s disabled.