Creating a custom subscription interval

The Subscriptions plugin already gives you the ability to create a day-long subscription, a month-long subscription, and subscriptions that can renew every other week. While it has a lot of intervals, it can't possibly have every one. If you want to have a subscription that renews every tenth week, you'll have to create a custom subscription interval. Using filters in Subscriptions, we can easily add this.

Getting ready

You must have the WooCommerce Subscriptions plugin installed and activated on your site. You should also have a subscription product already created.

How to do it…

In order to create a custom subscription interval, go through the following steps:

  1. Open up your theme's functions.php file, located under wp-content/themes/your-theme-name/, or create a custom WooCommerce plugin and open up that file.
  2. Add the following line of code. This tells Subscriptions to look for your modifications to the possible intervals:
    add_filter( 'woocommerce_subscription_period_interval_strings', 'woocommerce_cookbook_subscription_intervals' );
  3. Now add the following code. This will actually add the extra intervals:
    function woocommerce_cookbook_subscription_intervals( $intervals ) { 
        $intervals[10] = sprintf( __( 'every %s', 'woocommerce-cookbook' ),WC_Subscriptions::append_numeral_suffix( 10 ) );
        return $intervals; 
    }

    Note

    Note that there were two places where we put in our custom interval (10) in that string. If you want a different interval, then you should replace both instances of 10 with your desired number.

Now that we've created our custom interval, we need to edit our subscription product to use that interval.

  1. In the WordPress admin, click on Products and navigate to your subscription product.
  2. On the edit page, if you scroll down to the Product Data panel, you can now choose your custom interval.
    How to do it…
  3. Save your product and you're all done.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset