Jump to content

Cart Duplicate


Recommended Posts



We are using the link clicking at which the client immediately adds a certain product to the cart. Ie: /client-area/cart.php/?a=add&pid=1


But if the client several times will click at it, the product several times added to cart.


Can you please tell how to make so that the product can be added to the cart only 1 time?

Ie that the product is simply NOT added to the cart, if it is already there / no more than one.

Link to comment
Share on other sites

You can do this with a hook. The following will allow you to set the pids that you wish to not allow duplicates for, in the shopping cart. Simply create a file under "includes/hooks/" called something like "nodupepids.php" (can be named anything with a .php extension). Within the file, add the following code:



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

function nodupepids($vars) {

 // Set pids that do not allow dupes
 $nodupepids = array('First PID', 'Second PID', 'Third PID');

 // If a no dupe PID is in the cart, do not allow a second one
 foreach ($_SESSION['cart']['products'] as $key => $value) {

   if (in_array($value[pid], $pidsfound)) {
     header('Location: cart.php?a=remove&r=p&i=' . $key);

   if (in_array($value[pid], $nodupepids)) {
     $pidsfound[] = $value[pid];



add_hook("PreCalculateCartTotals", 1, "nodupepids");




Set your PIDs (or single PID), within the array, on the line that reads:


// Set pids that do not allow dupes

$nodupepids = array('First PID', 'Second PID', 'Third PID');



I tested this quickly on my dev server. I did not test it for having quantity enabled on your products. This works for adding an instance of the product, to the cart, one at a time.

Link to comment
Share on other sites

Wow, thank you very much!


But before I try the code I would like to ask you: I am using the module/addon which shows the message about over ordering and stop ordering until the client delete extra products - http://www.jetserver.net/whmcs-product-limiter

Does you code works with this module correct?


In Version 1.0.4 the developers of the module "Changed the way we handle cart limited products - client is noted and cannot checkout until he removes the spare products.".

So in the previous versions the module deleted the excess amount by it self. But now my clients have to do this and for some of them it is a pain - more actions, more links... I prefer to hide the cart at all but have to show it if there is more than 1 of the products to give my clients the possibility to deleted extra products and finish ordering...


So does your code actually do? Or may be you can tell me what can I do to make the module to delete extra products itself freeing my clients from this? I don't know what way is better...


I even found some code that did this in previous versions of the module (http://forum.whmcs.com/showthread.php?41258-limit-one-product-per-customer&p=326751#post326751) but I am not sure if it is still OK now and where exactly I should put it.


Regards, Eugene.

Link to comment
Share on other sites

I have never used the product limiter module you are referring to, so I can't tell you anything about it.


My hook is simple. You give it a list of pids (within the first array) that you only want to allow one instance of, in the cart. If a listed pid exists in the cart, and the same pid is added again, it leaves the original in place, and removes the new one you just added. So, the client will only see one instance of the pid in their cart. It basically won't let you add the additional duplicate product to the cart. The client doesn't have to delete the the duplicates manually, the hook does it for you. I would assume you would use this hook, instead of the module you mentioned, depending on if the hook completely meets your needs.

Link to comment
Share on other sites

Thank you for such a full answer - now I understand everything.


But I just tried your hook and got this: <div class="whmcs_error">WHMCS Integration: Слишком много перенаправлений.</div>


This means "Too much redirections". I am using the WordPress integration plugin (http://premium.wpmudev.org/project/whmcs-wordpress-integration/) and I think it is the pluging who gives this error... could you PLS fix/improve your hook some how?

Link to comment
Share on other sites

I don't use the WordPress integration module, so I wouldn't be able to fix that issue. If you want the duplicate products to be automatically removed from the cart, and not put the task upon the clients to remove the duplicates, you would need the redirect in the hook.


If I were you, I would open a support ticket with the developer of the WordPress integration module, and ask them how to get the hook to work with their module.

Link to comment
Share on other sites

  • 7 years later...

Join the conversation

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

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