Jump to content

How to offer a Free Trial for my service


Craft

Recommended Posts

Hello,

How can I offer a Free Trial (1 month) for my Annual web hosting plan?

Before 1 month finishes by 1 week,  I need the system to switch the customer to the Annual plan and generate an invoice with the Annual price!

I have tried the option (Auto Terminate/Fixed Term) in the products/services section, but it has an issue where it terminates the account automatically without generating an invoice to the customer for the Annual plan.

Any suggestions?

 

Free Trial.png

Link to comment
Share on other sites

Heya Craft,

Don't think the Auto Terminate is what you want since you wish for the customers to continue on after the trial.

Perhaps a hook for the daily cron where it checks for certain products (specified by you) that are on a monthly basis, and if it's a certain amount of days before the due date, it updates the billing cycle and amount?

This would only work if the monthly price is set to 0.00 and the product does not have a monthly billing cycle normally, just for the trial.

 

Link to comment
Share on other sites

19 hours ago, leemahoney3 said:

Heya Craft,

Don't think the Auto Terminate is what you want since you wish for the customers to continue on after the trial.

Perhaps a hook for the daily cron where it checks for certain products (specified by you) that are on a monthly basis, and if it's a certain amount of days before the due date, it updates the billing cycle and amount?

This would only work if the monthly price is set to 0.00 and the product does not have a monthly billing cycle normally, just for the trial.

 

I know that with Hooks, I can do anything 🙂

But, I need an easier way to do it because I don't have enough knowledge to create hooks!

Link to comment
Share on other sites

10 hours ago, leemahoney3 said:

If my logic suits, I can try put together a hook at some stage tomorrow for you.

Yes, your idea is correct.

1. Certain amount of days before the due date = 10 days ( we can set it manually in the hook)

2. Update the Billing Cycle from "Monthly" to "Annually"

3. Update the Recurring Amount to the Annual Price

4. Generate the Invoice

Note: Whatever the monthly price is, I prefer to use coupon code to override the monthly price to ZERO for Trials, because using a coupon code is having another advantage where I can limit the using of this coupon to only 1 per customer 🙂

Thanks in advance for your time and effort 🙂

Link to comment
Share on other sites

Heya Craft,

So I was thinking, the whole 10 days thing doesn't really make sense as you'd be generating the next invoice automatically already, right? So long as the service is updated straight at the end of the cart, the next invoice that gets generated (within the free month) would be for the new Annual price.

If this doesn't suit, I can create another hook to check and generate, though this might conflict with your invoices that are already being generated.

 

Anyways on to the hook, first create a new coupon code (e.g. FREETRIAL) and set it to 100%, you can configure which product you want it to apply to if you wish, and ensure to set it to be applicable to monthly payments only. Use the option to limit it to once per client.

 

Once you have that done, create a new php file in your /includes/hooks/ folder and add the following code:

<?php

<?php

use WHMCS\Carbon;
use WHMCS\Database\Capsule;

function free_trial_for_product($vars) {

    $promoCode = 'FREETRIAL'; // The exact promo code that you use for the free trial (must be 100% and best to be only applicable to the relevant products and set to one time use per client and order)

    $applicableProducts = [1,2,3]; // Product ID's separated by commas (can find the ID's in the tblproducts database table)

    /* ----------------------------------------------------------- */

	# Grab order details and a list of related services
    $orderDetails = Capsule::table('tblorders')->where('id', $vars['orderid'])->first();
    $services = Capsule::table('tblhosting')->where('orderid', $orderDetails->id)->get();

	# Loop through each service
    foreach ($services as $service) {

		# Only run the code below if the product is in the array above
        if (in_array($service->packageid, $applicableProducts)) {

			# Grab the promo code
            $orderPromoCode = $orderDetails->promocode;

			# Only run the code below if the code matches the one specified above
            if ($promoCode == $orderPromoCode) {

				# Grab the pricing for the product
                $getPricing = Capsule::table('tblpricing')->where('type', 'product')->where('relid', $service->packageid)->first();
	
				# Update the service so its set to the new billing cycle, amount and the next due date is correct (as WHMCS updates this to a year from the date automatically)
                Capsule::table('tblhosting')->where('id', $service->id)->update([
                    'billingcycle'  => 'Annually',
                    'amount'        => $getPricing->annually,
                    'nextduedate'   => Carbon::now()->addMonth()->format('Y-m-d'),
                ]);

            }

        }

    }

}

add_hook('ShoppingCartCheckoutCompletePage', 1, 'free_trial_for_product');

(Excuse the formatting of the comments in the code, the community is doing this)

Replace $promoCode with the code you've created and add the product ID's for the relevant products you want this to work on in the $applicableProducts array (separated by commas, can find the ID's in the tblproducts database table)

That's basically it, when the client adds the relevant product to their cart, chooses monthly payment and enters the correct promo code, it will change the total to zero.

The hook will then run on the completed cart page and will update the billing cycle to Annually on the product and set the correct amount (that you've set for annual payments) as well as the correct next due date (a month from the date the order is placed).

What didn't work: So WHMCS generates the order confirmation email and there is no hook before this is generated to update the details before the email is sent. Due to this, the order confirmation email will show the billing cycle as Monthly and the amount as the monthly amount. My hook kicks in after this is sent so the values are not true and do not reflect the new values.

I can't see anyway around this unfortunately, I cannot reference the order ID before the email is sent as the only hook available is the PreShoppingCartCheckout hook and that's before the order is created. The PreEmailSend hook needs the order ID, so thank you WHMCS... 🙄

Edited by leemahoney3
Link to comment
Share on other sites

First of all I would like to thank you for your time and effort 🙂

 

4 hours ago, leemahoney3 said:

So I was thinking, the whole 10 days thing doesn't really make sense as you'd be generating the next invoice automatically already, right? So long as the service is updated straight at the end of the cart, the next invoice that gets generated (within the free month) would be for the new Annual price.

 

Yes, that's correct.

 

4 hours ago, leemahoney3 said:

What didn't work: So WHMCS generates the order confirmation email and there is no hook before this is generated to update the details before the email is sent. Due to this, the order confirmation email will show the billing cycle as Monthly and the amount as the monthly amount. My hook kicks in after this is sent so the values are not true and do not reflect the new values.

 

Yes, that's not bad, I want the confirmation email to be sent as Monthly with the amount ZERO because it's a Free Trial 🙂

 

Finally, that's what I need exactly, thanks again and enjoy your coffee ❤️🙂

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated