Jump to content

limit one product per customer


mindaugas

Recommended Posts

  • 2 weeks later...
What if they are logged in? I don't think there is anything that can be done about that...

 

usually implementing a very special features in templates I use {php}{/php}, not very intellegent but still working

so you will need to do couple queries to DB to define if there is already a product for certain client

or

you can use internal API, which would be the better solution I guess - http://docs.whmcs.com/API:Get_Clients_Products

 

remember to define variable via PHP directly in smarty template you'll need this:

$this->_tpl_vars['your_variable'] = 'your_value';

Link to comment
Share on other sites

  • 8 months later...

you can do this with ShoppingCartValidateCheckout hook.

 

 

 

<?php
function limitOrders($vars) {
if(mysql_num_rows(mysql_query("SELECT id FROM `tblhosting` WHERE `userid` = '{$_SESSION['uid']}'")) > 0) {
	global $errormessage;
	$errormessage = "<li>limit one product per customer Reply to Thread</li>";
}
}
add_hook("ShoppingCartValidateCheckout", 1, "limitOrders");
?>

Link to comment
Share on other sites

  • 2 weeks later...
you can do this with ShoppingCartValidateCheckout hook.

 

 

 

<?php
function limitOrders($vars) {
   if(mysql_num_rows(mysql_query("SELECT id FROM `tblhosting` WHERE `userid` = '{$_SESSION['uid']}'")) > 0) {
       global $errormessage;
       $errormessage = "<li>limit one product per customer Reply to Thread</li>";
   }
}
add_hook("ShoppingCartValidateCheckout", 1, "limitOrders");
?>

 

So all you would do is replace 0 in ''{$_SESSION['uid']}'")) > 0)' with uid of your product??

Link to comment
Share on other sites

you can do this with ShoppingCartValidateCheckout hook.

 

 

 

<?php
function limitOrders($vars) {
if(mysql_num_rows(mysql_query("SELECT id FROM `tblhosting` WHERE `userid` = '{$_SESSION['uid']}'")) > 0 AND 'xxx' = 'yyy') {
	global $errormessage;
	$errormessage = "<li>limit one product per customer Reply to Thread</li>";
}
}
add_hook("ShoppingCartValidateCheckout", 1, "limitOrders");
?>

 

Check the database in tblhosting for the correct field to check against, replace xxx

Check the product for the correct product id, replace yyy

 

The sql is saying, select clients userid from tblhosting where product is more than zero, if the product exists for you will get their id, in this case they wont be able to continue the order.

 

As for seperate client emails, they can make as many as they want to make on a single domain and each will be different, if you wish you could also drop in a second sql check or just join one to this to check if the email domain is used, the problem is you would need a whitelist of domains for mail services like hotmail etc as they are multi user domains where a private domain for the most part is not.

Edited by disgruntled
Link to comment
Share on other sites

  • 4 months later...

I've developed a Hook to do this!

It works perfectly!

 

function limitOrders($vars) {
$array = array(7, ; // List of free produtcs (ids)

for($i = 0; $i < count($_SESSION['cart']['products']); $i++) {

	$sql = mysql_query("SELECT *, (SELECT `name` FROM tblproducts WHERE id = packageid) AS product FROM tblhosting WHERE userid = ".$_SESSION['uid']." AND packageid = ".$_SESSION['cart']['products'][$i]['pid']) or die(mysql_error());
	if(mysql_num_rows($sql) > 0) {
		$c = mysql_fetch_array($sql);

		if(in_array($c['packageid'], $array)) {
			global $errormessage;
			$errormessage = "<li>Você já possui uma licença FREE do produto <b>".$c['product']."</b>!</li>"; //Portuguese message
			//$errormessage = "<li>You already have a FREE license of <b>".$c['product']."</b>!</li>";  //English message
		}
	}
}
}
add_hook("ShoppingCartValidateCheckout", 1, "limitOrders");

 

Just put the ID of free products in the $array variable...

Edited by _Dudu_1533
Link to comment
Share on other sites

  • 5 months later...
I've developed a Hook to do this!

It works perfectly!

 

function limitOrders($vars) {
$array = array(7, ; // List of free produtcs (ids)

for($i = 0; $i < count($_SESSION['cart']['products']); $i++) {

	$sql = mysql_query("SELECT *, (SELECT `name` FROM tblproducts WHERE id = packageid) AS product FROM tblhosting WHERE userid = ".$_SESSION['uid']." AND packageid = ".$_SESSION['cart']['products'][$i]['pid']) or die(mysql_error());
	if(mysql_num_rows($sql) > 0) {
		$c = mysql_fetch_array($sql);

		if(in_array($c['packageid'], $array)) {
			global $errormessage;
			$errormessage = "<li>Você já possui uma licença FREE do produto <b>".$c['product']."</b>!</li>"; //Portuguese message
			//$errormessage = "<li>You already have a FREE license of <b>".$c['product']."</b>!</li>";  //English message
		}
	}
}
}
add_hook("ShoppingCartValidateCheckout", 1, "limitOrders");

 

Just put the ID of free products in the $array variable...

 

This method doesn't seem to work anymore, you get a MySQL error when signing up with a new account. Any suggestions?

Link to comment
Share on other sites

  • 1 month later...
Check the database in tblhosting for the correct field to check against, replace xxx

Check the product for the correct product id, replace yyy

 

The sql is saying, select clients userid from tblhosting where product is more than zero, if the product exists for you will get their id, in this case they wont be able to continue the order.

 

As for seperate client emails, they can make as many as they want to make on a single domain and each will be different, if you wish you could also drop in a second sql check or just join one to this to check if the email domain is used, the problem is you would need a whitelist of domains for mail services like hotmail etc as they are multi user domains where a private domain for the most part is not.

 

 

That didn't quite work for me in the latest WHMCS (5.24). This does though:

 


<?php

if (!defined("WHMCS"))
       die("This file cannot be accessed directly");


function limitOrders($vars) {
       if(mysql_num_rows(mysql_query("SELECT packageid FROM `tblhosting` WHERE `userid` = '{$_SESSION['uid']}'")) > 0) {

               if($packageid = '2') {
               global $errormessage;
               $errormessage = "<li>Sorry, you already have a Free VPS account and there's a limit of one per user.</li>";
               }
       }
}
add_hook("ShoppingCartValidateCheckout", 1, "limitOrders");
?>

 

Change the '2' to the product id of the product you want to limit orders of.

Link to comment
Share on other sites

  • 1 month later...

Awesome, I was looking for this same thing. I need to allow only 1 product across my entire site regardless of free or paid. Even for paid products I want to limit user to only 1 paid product.

 

I have the following quick questions:

1) This does not affect upgrades, right? If a user upgrades from a Low to Medium, this hook will not conflict and say they can not upgrade, right?

 

2) This goes in /includes/hooks, right? Does it matter what to name the file?

 

cheers~

Link to comment
Share on other sites

This works great for existing customers with a product already in the system, but is there a way to modify the hook to make sure a shopping cart can't have more than 1 product? We have a free hosting/domain package and want to limit it to one per user but currently a user can get to the end of the shopping cart and then back up and add another, and another...

Link to comment
Share on other sites

This works great for existing customers with a product already in the system, but is there a way to modify the hook to make sure a shopping cart can't have more than 1 product? We have a free hosting/domain package and want to limit it to one per user but currently a user can get to the end of the shopping cart and then back up and add another, and another...

 

Hmmmm... you are very correct! I just tested that and you are right that if a user continues shopping, they can purchase multiple products/services. Damn... I had not tested that aspect of it. :oops:

 

Ok, so then there needs to be part of the hook that qualifies only one product on checkout maybe and then tells user to delete all but one product? Or is there a simpler way? I am not a great programmer so I will listen to your ideas...

Link to comment
Share on other sites

  • 2 weeks later...

I wanted to come back and say that we found a way to do this with items currently in the order by using the WHMCS Product Limiter Addon (which is free): http://www.jetserver.net/whmcs-product-limiter.

 

A couple of caveats:

 

* Product Limiter only removes the product that violates the limitation that you've set up. In our case, we had a free domain linked to that hosting product, so I had to add some code to the plugin to remove not only the last product but the last domain, too:

 

In hooks.php, find the line that reads:

 

unset($_SESSION['cart']['products'][$cart_items[count($cart_items) - 1]]);

 

and below it add the following:

 

unset($_SESSION['cart']['domains'][$cart_items[count($cart_items) - 1]]);

 

* If a customer has repeatedly added the same (limited) product, they have to cycle through the checkout a couple of times until the amounts in their cart fall below the limit.

Link to comment
Share on other sites

I wanted to come back and say that we found a way to do this with items currently in the order by using the WHMCS Product Limiter Addon (which is free): http://www.jetserver.net/whmcs-product-limiter.

 

Thank you so much for the tip! I just visited the site and registered an account and added the plugin to my account, but it appears no where -- seems like no place to download it once I am logged in. when I try to "buy" the free addon again, it tells me I can only purchase 1 at a time. So it seems to work, though it doesn;t appear where I can download the plugin from. Very weird....

Link to comment
Share on other sites

  • 4 months later...
  • 3 weeks later...

Great thank you~ thanks for all your hard work~

 

I just tried to download with the same username I used last time and I get this error:

"You can't order "WHMCS Limit Products/Services Purchase" more then one time"

 

Is it possible to re-download this with my same username or should I use a different registration?

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