Craft Posted August 28, 2022 Share Posted August 28, 2022 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? 0 Quote Link to comment Share on other sites More sharing options...
leemahoney3 Posted August 28, 2022 Share Posted August 28, 2022 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. 0 Quote Link to comment Share on other sites More sharing options...
Craft Posted August 29, 2022 Author Share Posted August 29, 2022 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! 0 Quote Link to comment Share on other sites More sharing options...
leemahoney3 Posted August 29, 2022 Share Posted August 29, 2022 8 hours ago, Craft said: 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! If my logic suits, I can try put together a hook at some stage tomorrow for you. 0 Quote Link to comment Share on other sites More sharing options...
Craft Posted August 30, 2022 Author Share Posted August 30, 2022 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 🙂 0 Quote Link to comment Share on other sites More sharing options...
leemahoney3 Posted August 30, 2022 Share Posted August 30, 2022 Heya Craft, No problem, give me a day or two and I'll throw together a hook for that 🙂 0 Quote Link to comment Share on other sites More sharing options...
Craft Posted August 30, 2022 Author Share Posted August 30, 2022 1 hour ago, leemahoney3 said: Heya Craft, No problem, give me a day or two and I'll throw together a hook for that 🙂 Thanks mate 🙂 I'm waiting for your awesome hook 🙂 0 Quote Link to comment Share on other sites More sharing options...
leemahoney3 Posted August 30, 2022 Share Posted August 30, 2022 (edited) 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 August 30, 2022 by leemahoney3 1 Quote Link to comment Share on other sites More sharing options...
Craft Posted August 31, 2022 Author Share Posted August 31, 2022 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 ❤️🙂 0 Quote Link to comment Share on other sites More sharing options...
leemahoney3 Posted August 31, 2022 Share Posted August 31, 2022 Hi Craft, Thanks for the coffee! Much appreciated. If you find the addon does not work as intended, or need it tweaked, then let me know. 1 Quote Link to comment Share on other sites More sharing options...
Craft Posted August 31, 2022 Author Share Posted August 31, 2022 1 hour ago, leemahoney3 said: Hi Craft, Thanks for the coffee! Much appreciated. If you find the addon does not work as intended, or need it tweaked, then let me know. It works fine, thank you 🙂 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.