Jump to content
kang28ivan

Disable Domain Reg if client not have product service eg hostin

Recommended Posts

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

Share this post


Link to post
Share on other sites

Question: what if a "first-time" customer places an order for hosting & domain? 🤔

Share this post


Link to post
Share on other sites

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.

whmcs-domain-require-product-one-off-products.png.a1f718572ad68f7860d362ff14d75b96.png

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.

Share this post


Link to post
Share on other sites
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.

whmcs-domain-require-product-one-off-products.png.a1f718572ad68f7860d362ff14d75b96.png

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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']")

 

Share this post


Link to post
Share on other sites
Posted (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 😅

Screenshot_2.thumb.png.f59bf51ea25b9b5ac313e575b04c3eda.png

Edited by kang28ivan

Share this post


Link to post
Share on other sites

Okay, I evolved the hook to support 3 different types of alerts.

whmcs-domain-require-product-one-off-products.png.1397eb986f124ab9a21c95ccf6e19912.png

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 🙏

Share this post


Link to post
Share on other sites
8 minutes ago, Kian said:

Okay, I evolved the hook to support 3 different types of alerts.

whmcs-domain-require-product-one-off-products.png.1397eb986f124ab9a21c95ccf6e19912.png

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...

Screenshot_2.png.17a807ca33e7d0e9d33f19d8d87ead84.png

I really thank you very much for the hook you developed, it was very useful 😍

Share this post


Link to post
Share on other sites

🥳 now have fun restricting people 😛

Share this post


Link to post
Share on other sites

😅 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 🙏

Share this post


Link to post
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