Home Arrow Icon Knowledge base Arrow Icon WordPress Code Related Arrow Icon WooCommerce Subscriptions with custom code samples

WooCommerce Subscriptions with custom code samples

Recurring revenue models have become a cornerstone for sustainable growth. WooCommerce Subscriptions, a powerful extension of the popular WooCommerce platform, enables online store owners to seamlessly manage and automate subscription-based services. Whether you're selling monthly subscription boxes, digital memberships, or recurring services, WooCommerce Subscriptions offers a robust solution tailored to meet diverse business needs.

What is WooCommerce Subscriptions?

WooCommerce Subscriptions is a premium plugin that allows businesses to create and manage products with recurring payments. It integrates smoothly with the WooCommerce ecosystem, providing a flexible and customizable platform for subscription management. The plugin supports various billing schedules, including daily, weekly, monthly, and annually, making it suitable for a wide range of subscription models.

Key Features of WooCommerce Subscriptions

  1. Flexible Billing Options

    • WooCommerce Subscriptions provides extensive billing flexibility, allowing merchants to set up multiple billing schedules for a single product. This feature is particularly useful for businesses offering tiered subscription plans.
  2. Automatic Recurring Payments

    • The plugin supports automatic recurring payments, ensuring that subscribers are billed on time without manual intervention. It integrates with numerous payment gateways, including PayPal, Stripe, and Authorize.Net, to provide a seamless checkout experience.
  3. Subscription Management

    • Customers have the ability to manage their subscriptions directly from their accounts. They can upgrade or downgrade plans, change payment methods, and even pause or cancel their subscriptions. This level of control enhances customer satisfaction and reduces churn rates.
  4. Proration and Synchronization

    • WooCommerce Subscriptions offers proration and synchronization features, which are essential for managing mid-cycle upgrades or downgrades. Proration ensures that customers are charged fairly based on the time remaining in their billing cycle.
  5. Free Trials and Sign-Up Fees

    • Businesses can offer free trials to attract new customers or charge a one-time sign-up fee. These options provide flexibility in crafting promotional strategies to drive subscription growth.
  6. Advanced Reporting

    • The plugin includes advanced reporting capabilities that provide insights into subscription performance. Store owners can track key metrics such as renewal rates, churn rates, and revenue, helping them make informed business decisions.

Setting Up WooCommerce Subscriptions

  1. Installation and Activation

    • To get started, purchase and download the WooCommerce Subscriptions plugin from the WooCommerce marketplace. Install and activate the plugin via the WordPress admin dashboard.
  2. Creating Subscription Products

    • Navigate to the Products section in WooCommerce and create a new product. Select the product type as "Simple Subscription" or "Variable Subscription" based on your requirements. Set the subscription price, billing interval, and other relevant details.
  3. Configuring Payment Gateways

    • Configure the payment gateways you wish to use for recurring payments. Ensure that your chosen gateways support recurring billing to avoid any payment issues.
  4. Managing Subscriptions

    • Once your subscription products are live, manage them through the Subscriptions tab in WooCommerce. Here, you can view all active, pending, and canceled subscriptions, as well as customer details and payment history.

Best Practices for WooCommerce Subscriptions

  1. Clear Communication

    • Ensure that your subscription terms, pricing, and billing intervals are clearly communicated to customers. Transparency helps build trust and reduces the likelihood of disputes.
  2. Customer Support

    • Provide robust customer support to address any issues related to subscriptions. A dedicated support system can help resolve queries quickly and enhance customer satisfaction.
  3. Regular Updates

    • Keep your WooCommerce and WooCommerce Subscriptions plugin updated to benefit from the latest features and security enhancements. Regular updates also ensure compatibility with other plugins and themes.
  4. Engagement and Retention

    • Implement strategies to engage subscribers and reduce churn. Offer exclusive content, discounts, and personalized experiences to keep customers subscribed and loyal to your brand.

WooCommerce Subscriptions is a powerful tool that can transform your e-commerce business by introducing a steady stream of recurring revenue. With its flexible billing options, robust subscription management features, and seamless integration with WooCommerce, it provides everything you need to launch and manage successful subscription products. By following best practices and leveraging the advanced capabilities of WooCommerce Subscriptions, you can build a sustainable and profitable subscription business in 2024 and beyond.

Sample code for Customizing WooCommerce Subscriptions

Customizing WooCommerce Subscriptions can involve a variety of code snippets to tweak its functionality according to your needs. Here are some examples:

1. Customizing Subscription Price Display

This snippet customizes the way subscription prices are displayed on your product pages.

php
add_filter( 'woocommerce_subscriptions_product_price_string', 'custom_subscription_price_string', 10, 3 ); function custom_subscription_price_string( $subscription_string, $product, $include ) { // Modify the subscription price string $custom_string = sprintf( __( 'Only %s per %s!', 'text-domain' ), $product->get_price(), $product->get_period() ); return $custom_string; }

2. Adding Custom Fields to Subscription Product

This snippet adds a custom field to the subscription product data tab in the admin area.

php
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_subscription_fields' ); function add_custom_subscription_fields() { global $woocommerce, $post; echo '<div class="options_group">'; woocommerce_wp_text_input( array( 'id' => '_custom_subscription_field', 'label' => __( 'Custom Subscription Field', 'text-domain' ), 'placeholder' => 'Enter custom value', 'desc_tip' => 'true', 'description' => __( 'Enter a custom value for this subscription.', 'text-domain' ) ) ); echo '</div>'; } add_action( 'woocommerce_process_product_meta', 'save_custom_subscription_fields' ); function save_custom_subscription_fields( $post_id ) { $custom_field = isset( $_POST['_custom_subscription_field'] ) ? sanitize_text_field( $_POST['_custom_subscription_field'] ) : ''; update_post_meta( $post_id, '_custom_subscription_field', $custom_field ); }

3. Adding Custom Email Notification for Subscription Renewal

This snippet sends a custom email notification when a subscription renews.

php
add_action( 'woocommerce_subscription_renewal_payment_complete', 'send_custom_renewal_email' ); function send_custom_renewal_email( $subscription ) { $to = $subscription->get_billing_email(); $subject = 'Your Subscription Has Been Renewed'; $body = 'Hello, your subscription has been successfully renewed. Thank you for your continued support!'; $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail( $to, $subject, $body, $headers ); }

4. Adding a Custom Action to Subscription Actions

This snippet adds a custom action button to the subscription actions in the admin area.

php
add_filter( 'woocommerce_subscription_actions', 'add_custom_subscription_action', 10, 2 ); function add_custom_subscription_action( $actions, $subscription ) { $actions['custom_action'] = array( 'url' => wp_nonce_url( add_query_arg( 'custom_action', $subscription->get_id(), admin_url( 'edit.php?post_type=shop_subscription' ) ), 'custom_action' ), 'name' => __( 'Custom Action', 'text-domain' ), ); return $actions; } add_action( 'admin_init', 'process_custom_subscription_action' ); function process_custom_subscription_action() { if ( isset( $_GET['custom_action'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'custom_action' ) ) { $subscription_id = absint( $_GET['custom_action'] ); // Perform custom action here // Example: update subscription status $subscription = wcs_get_subscription( $subscription_id ); $subscription->update_status( 'on-hold' ); wp_safe_redirect( remove_query_arg( array( 'custom_action', '_wpnonce' ) ) ); exit; } }

5. Adding Custom Meta Data to Subscription Orders

This snippet adds custom meta data to subscription orders during checkout.

php
add_action( 'woocommerce_checkout_create_order', 'add_custom_meta_to_subscription_orders', 20, 2 ); function add_custom_meta_to_subscription_orders( $order, $data ) { if ( wcs_order_contains_subscription( $order ) ) { $order->update_meta_data( '_custom_order_meta', 'Custom Value' ); } } add_action( 'woocommerce_subscriptions_renewal_order_created', 'copy_custom_meta_to_renewal_orders', 10, 2 ); function copy_custom_meta_to_renewal_orders( $renewal_order, $original_order ) { $custom_meta = $original_order->get_meta( '_custom_order_meta' ); if ( $custom_meta ) { $renewal_order->update_meta_data( '_custom_order_meta', $custom_meta ); } }

Conclusion

These custom code snippets provide a starting point for tailoring WooCommerce Subscriptions to better fit your business needs. Always test custom code in a staging environment before deploying it to your live site to ensure it works as expected and does not cause conflicts with other plugins or themes.