GrayHunter Posted September 30, 2014 Share Posted September 30, 2014 Hello! 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. 0 Quote Link to comment Share on other sites More sharing options...
SeanP Posted October 1, 2014 Share Posted October 1, 2014 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: <?php 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. 0 Quote Link to comment Share on other sites More sharing options...
GrayHunter Posted October 1, 2014 Author Share Posted October 1, 2014 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. 0 Quote Link to comment Share on other sites More sharing options...
SeanP Posted October 1, 2014 Share Posted October 1, 2014 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. 0 Quote Link to comment Share on other sites More sharing options...
GrayHunter Posted October 2, 2014 Author Share Posted October 2, 2014 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? 0 Quote Link to comment Share on other sites More sharing options...
SeanP Posted October 3, 2014 Share Posted October 3, 2014 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. 0 Quote Link to comment Share on other sites More sharing options...
GrayHunter Posted October 3, 2014 Author Share Posted October 3, 2014 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.Thanks, I'll do it! 0 Quote Link to comment Share on other sites More sharing options...
Md Rasel Khan Posted October 1, 2022 Share Posted October 1, 2022 Hi @SeanP Is there any way to show a warning during order If the same product already purchased before and active? Thanks! 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.