kang28ivan Posted July 26, 2020 Share Posted July 26, 2020 Dear, Is there a way to deactivate domain registration when the client not have active product hosting service? I have a lot of clients who register domains without buying hosting services so I want to prevent them from having hosting services controlled Thank you 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted July 26, 2020 Share Posted July 26, 2020 Question: what if a "first-time" customer places an order for hosting & domain? 🤔 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted July 26, 2020 Share Posted July 26, 2020 This hook should work. It does two things: Allow domain purchase only when Customer has an active product/service (Pending and Terminated don't count) OR has a product/service in WHMCS cart Bonus: allows to sell "one-off" products/services <?php /** * One-off Products/Services & Domain purchase require Product/Service * * @writtenby Kian */ use WHMCS\Database\Capsule; add_hook('ClientAreaHeadOutput', 1, function($vars) { $onetimeProducts = array(); // The following Product/Service IDs are treated as "one-off" (each customer can purchase them only once) $onetimeProductGroups = array(); // Same as above but works on Product Group IDs ("one-off" concept extends to all products/services in such groups) $domainRequiresProduct = false; // Allow domain purchase only when if any of the following conditions is met: a) Customer has an existing Product/Service (Pending and Terminated don't count) b) Customer is purchasing a domain and a Product/Service if ($_SESSION['uid']) { if ($_SESSION['cart']['products'] AND ($onetimeProductGroups OR $onetimeProducts)) { $disallowedPids = Capsule::table('tblproducts')->whereIn('gid', $onetimeProductGroups)->orWhereIn('id', $onetimeProducts)->pluck('id'); $userProducts = Capsule::table('tblhosting')->where('userid', '=', $_SESSION['uid'])->WhereIn('packageid', $disallowedPids)->groupBy('packageid')->pluck('packageid'); foreach ($_SESSION['cart']['products'] as $k => $v) { if (in_array($v['pid'], $userProducts)) { $removedFromCart = true; unset($_SESSION['cart']['products'][$k]); } } if ($removedFromCart) { header('Location: cart.php?a=view&disallowed=1'); die(); } } elseif ($_SESSION['cart']['domains'] AND $domainRequiresProduct) { $userHasProduct = Capsule::table('tblhosting')->where('userid', '=', $_SESSION['uid'])->whereNotIn('domainstatus', array('Pending', 'Terminated'))->pluck('id'); if (!$userHasProduct AND !$_SESSION['cart']['products']) { unset($_SESSION['cart']['domains']); header('Location: cart.php?a=view&requireProduct=1'); die(); } } } }); add_hook('ClientAreaHeadOutput', 1, function($vars) { if ($_SESSION['uid'] AND $vars['filename'] == 'cart' AND $_GET['a'] == 'view') { if ($_GET['disallowed']) { return <<<HTML <script type="text/javascript"> $(document).ready(function() { $("form[action='/cart.php?a=view']").prepend('<div class="alert alert-warning text-center" role="alert">The Product/Service can be purchased only once.</div>'); }); </script> HTML; } elseif ($_GET['requireProduct']) { return <<<HTML <script type="text/javascript"> $(document).ready(function() { $("form[action='/cart.php?a=view']").prepend('<div class="alert alert-warning text-center" role="alert">Domain purchase require an active Product/Service.</div>'); }); </script> HTML; } } }); And here's the preview. 5 hours ago, Kian said: Question: what if a "first-time" customer places an order for hosting & domain? 🤔 I answer to my own question. He's allowed to order a domain only if there's also a product/service in WHMCS cart. The above hook already supports it. 1 Quote Link to comment Share on other sites More sharing options...
kang28ivan Posted July 27, 2020 Author Share Posted July 27, 2020 14 hours ago, Kian said: Question: what if a "first-time" customer places an order for hosting & domain? 🤔 8 hours ago, Kian said: This hook should work. It does two things: Allow domain purchase only when Customer has an active product/service (Pending and Terminated don't count) OR has a product/service in WHMCS cart Bonus: allows to sell "one-off" products/services <?php /** * One-off Products/Services & Domain purchase require Product/Service * * @writtenby Kian */ use WHMCS\Database\Capsule; add_hook('ClientAreaHeadOutput', 1, function($vars) { $onetimeProducts = array(); // The following Product/Service IDs are treated as "one-off" (each customer can purchase them only once) $onetimeProductGroups = array(); // Same as above but works on Product Group IDs ("one-off" concept extends to all products/services in such groups) $domainRequiresProduct = false; // Allow domain purchase only when if any of the following conditions is met: a) Customer has an existing Product/Service (Pending and Terminated don't count) b) Customer is purchasing a domain and a Product/Service if ($_SESSION['uid']) { if ($_SESSION['cart']['products'] AND ($onetimeProductGroups OR $onetimeProducts)) { $disallowedPids = Capsule::table('tblproducts')->whereIn('gid', $onetimeProductGroups)->orWhereIn('id', $onetimeProducts)->pluck('id'); $userProducts = Capsule::table('tblhosting')->where('userid', '=', $_SESSION['uid'])->WhereIn('packageid', $disallowedPids)->groupBy('packageid')->pluck('packageid'); foreach ($_SESSION['cart']['products'] as $k => $v) { if (in_array($v['pid'], $userProducts)) { $removedFromCart = true; unset($_SESSION['cart']['products'][$k]); } } if ($removedFromCart) { header('Location: cart.php?a=view&disallowed=1'); die(); } } elseif ($_SESSION['cart']['domains'] AND $domainRequiresProduct) { $userHasProduct = Capsule::table('tblhosting')->where('userid', '=', $_SESSION['uid'])->whereNotIn('domainstatus', array('Pending', 'Terminated'))->pluck('id'); if (!$userHasProduct AND !$_SESSION['cart']['products']) { unset($_SESSION['cart']['domains']); header('Location: cart.php?a=view&requireProduct=1'); die(); } } } }); add_hook('ClientAreaHeadOutput', 1, function($vars) { if ($_SESSION['uid'] AND $vars['filename'] == 'cart' AND $_GET['a'] == 'view') { if ($_GET['disallowed']) { return <<<HTML <script type="text/javascript"> $(document).ready(function() { $("form[action='/cart.php?a=view']").prepend('<div class="alert alert-warning text-center" role="alert">The Product/Service can be purchased only once.</div>'); }); </script> HTML; } elseif ($_GET['requireProduct']) { return <<<HTML <script type="text/javascript"> $(document).ready(function() { $("form[action='/cart.php?a=view']").prepend('<div class="alert alert-warning text-center" role="alert">Domain purchase require an active Product/Service.</div>'); }); </script> HTML; } } }); And here's the preview. I answer to my own question. He's allowed to order a domain only if there's also a product/service in WHMCS cart. The above hook already supports it. Thank you very much sir, I will try your code soon Hope this helps me 0 Quote Link to comment Share on other sites More sharing options...
kang28ivan Posted July 27, 2020 Author Share Posted July 27, 2020 Thank you very much for the code you provided, sir. The code works well in Six templates, but it doesn't work in Lagom templates just redirect to cart view no message show on the page 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted July 27, 2020 Share Posted July 27, 2020 Eh eh... 😁 I hate templates. I never used Lagom but I think you can simply update the following jQuery selector to get the job done. $("form[action='/cart.php?a=view']") 0 Quote Link to comment Share on other sites More sharing options...
kang28ivan Posted July 27, 2020 Author Share Posted July 27, 2020 (edited) 1 hour ago, Kian said: Eh eh... 😁 I hate templates. I never used Lagom but I think you can simply update the following jQuery selector to get the job done. $("form[action='/cart.php?a=view']") Already try but not working 😄 I modified code like this <?php /** * One-off Products/Services & Domain purchase require Product/Service * * @writtenby Kian */ use WHMCS\Database\Capsule; add_hook('ClientAreaHeadOutput', 1, function($vars) { $onetimeProducts = array(); // The following Product/Service IDs are treated as "one-off" (each customer can purchase them only once) $onetimeProductGroups = array(); // Same as above but works on Product Group IDs ("one-off" concept extends to all products/services in such groups) $domainRequiresProduct = true; // Allow domain purchase only when if any of the following conditions is met: a) Customer has an existing Product/Service (Pending and Terminated don't count) b) Customer is purchasing a domain and a Product/Service if ($_SESSION['uid']) { if ($_SESSION['cart']['products'] AND ($onetimeProductGroups OR $onetimeProducts)) { $disallowedPids = Capsule::table('tblproducts')->whereIn('gid', $onetimeProductGroups)->orWhereIn('id', $onetimeProducts)->pluck('id'); $userProducts = Capsule::table('tblhosting')->where('userid', '=', $_SESSION['uid'])->WhereIn('packageid', $disallowedPids)->groupBy('packageid')->pluck('packageid'); foreach ($_SESSION['cart']['products'] as $k => $v) { if (in_array($v['pid'], $userProducts)) { $removedFromCart = true; unset($_SESSION['cart']['products'][$k]); } } if ($removedFromCart) { header('Location: cart.php?a=checkout&disallowed=1'); die(); } } elseif ($_SESSION['cart']['domains'] AND $domainRequiresProduct) { $userHasProduct = Capsule::table('tblhosting')->where('userid', '=', $_SESSION['uid'])->whereNotIn('domainstatus', array('Pending', 'Terminated'))->pluck('id'); if (!$userHasProduct AND !$_SESSION['cart']['products']) { unset($_SESSION['cart']['domains']); header('Location: cart.php?a=checkout&requireProduct=1'); die(); } } } }); add_hook('ClientAreaHeadOutput', 1, function($vars) { if ($_SESSION['uid'] AND $vars['filename'] == 'cart' AND $_GET['a'] == 'checkout') { if ($_GET['disallowed']) { return <<<HTML <script type="text/javascript"> $(document).ready(function() { $("form[action='/cart.php?a=checkout']").prepend('<div class="alert alert-warning text-center" role="alert">The Product/Service can be purchased only once.</div>'); }); </script> HTML; } elseif ($_GET['requireProduct']) { return <<<HTML <script type="text/javascript"> $(document).ready(function() { $("form[action='/cart.php?a=checkout']").prepend('<div class="alert alert-warning text-center" role="alert">Domain purchase require an active Product/Service.</div>'); }); </script> HTML; } } }); Code just appears in the header not in the body 😅 Edited July 27, 2020 by kang28ivan 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted July 28, 2020 Share Posted July 28, 2020 Okay, I evolved the hook to support 3 different types of alerts. And I coded everything from scratch to add two new features regarding "one-off" products: First-timer tollerance. New customers are allowed to order everything they want. Once they register the fun is over An option to restrict purchases even further. Here's an example. Let's say that Bronze and Silver two "one-off" products off - Customer can purchase Bronze and Silver then they can no longer purchase them on - As soon as customer orders Bronze, he can't order Silver (and another Bronze of course) I'm still perfecting and changing the code so you can find it on github. I'm looking for bugs at the moment 🙏 2 Quote Link to comment Share on other sites More sharing options...
kang28ivan Posted July 28, 2020 Author Share Posted July 28, 2020 8 minutes ago, Kian said: Okay, I evolved the hook to support 3 different types of alerts. And I coded everything from scratch to add two new features regarding "one-off" products: First-timer tollerance. New customers are allowed to order everything they want. Once they register the fun is over An option to restrict purchases even further. Here's an example. Let's say that Bronze and Silver two "one-off" products off - Customer can purchase Bronze and Silver then they can no longer purchase them on - As soon as customer orders Bronze, he can't order Silver (and another Bronze of course) I'm still perfecting and changing the code so you can find it on github. I'm looking for bugs at the moment 🙏 Hi sir @Kian I immediately tried the code that you updated and the result on lagom templates... I really thank you very much for the hook you developed, it was very useful 😍 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted July 28, 2020 Share Posted July 28, 2020 🥳 now have fun restricting people 😛 1 Quote Link to comment Share on other sites More sharing options...
kang28ivan Posted July 28, 2020 Author Share Posted July 28, 2020 😅 Right, because in our many clients buy a domain without buying hosting just because the .com domain has a slightly cheaper price than other providers in my country Once again, thank you so much 🙏 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.