pRieStaKos Posted November 19, 2020 Share Posted November 19, 2020 Hello, I would like to add a free product every time a client adds a specific product from a product group. I'm trying to find a hook and an API call to use for this, but I cannot find anything related. Note: Due to complex product configurations and other provisioning actions I cannot use bundles for this. Any suggestion ? Thank you in advance 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted November 19, 2020 Share Posted November 19, 2020 1 minute ago, pRieStaKos said: Any suggestion ? use a bundle.... oh i'm joking! 😃 if you had said it was a product addon, i'd have suggested enabling it in the link... but I guess you're going to tell me that it's a product. the cart uses the session array, so have you tried adding your free product to the $session ? 1 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted November 19, 2020 Author Share Posted November 19, 2020 (edited) No. I didn’t know it’s using the session. I will check it. If only WHMCS had updated their dev. documentation.... 😆 Thank you @brian! Edited November 19, 2020 by pRieStaKos 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted November 20, 2020 Author Share Posted November 20, 2020 @brian! Thank you again (typical) for your help. 😅 I managed to add a free product by using `PreCalculateCartTotals` and push a product array in `$_SESSION['cart']['products']` $addProduct = array( "pid" => $customProduct, "qty" => "1", "domain" => "", "billingcycle" => NULL, "configoptions" => NULL, "customfields" => NULL, "addons" => NULL, "server" => ""); array_push($_SESSION['cart']['products'], $addProduct);  0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted November 23, 2020 Author Share Posted November 23, 2020 @brian! Do you know how to trigger the addition of the free product, after main product is fully added to cart (after product configoptions) ? On Review & Checkout page. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted November 24, 2020 Share Posted November 24, 2020 array pushing the free product into the session should add the product - if you're going to want it to occur after c/o, then you might need to set a conditional statement to check whether c/o X has a value, and only if that's the case to push the array. you will also need to ensure that you only add it the free product once, e.g if the user refreshes viewcart, ensure that the hook doesn't keep on adding multiple free products. 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted November 25, 2020 Author Share Posted November 25, 2020 21 hours ago, brian! said: array pushing the free product into the session should add the product - if you're going to want it to occur after c/o, then you might need to set a conditional statement to check whether c/o X has a value, and only if that's the case to push the array. you will also need to ensure that you only add it the free product once, e.g if the user refreshes viewcart, ensure that the hook doesn't keep on adding multiple free products. I'm already doing it . The problem is that in action hook "ClientArePageCart" or "PreCalculateCartTotals" the free product is added when you are on configoption page too. I want to add the free product only on Review & Checkout page. Â 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted November 26, 2020 Author Share Posted November 26, 2020 (edited) UPDATE: I finally used "ShoppingCartViewCartOutput" and checking for "noconfig" variable. It worked... Edited November 26, 2020 by pRieStaKos 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted November 26, 2020 Share Posted November 26, 2020 23 hours ago, pRieStaKos said: The problem is that in action hook "ClientArePageCart" or "PreCalculateCartTotals" the free product is added when you are on configoption page too. I want to add the free product only on Review & Checkout page. even if you wrap the hook function in an if ($vars['templatefile'] == 'viewcart') { condition to ensure that it only runs on viewcart/checkout ? 3 hours ago, pRieStaKos said: UPDATE: I finally used "ShoppingCartViewCartOutput" and checking for "noconfig" variable. It worked... there are plenty of ways to skin a cat! 😼 2 Quote Link to comment Share on other sites More sharing options...
ZARTURA Posted April 21 Share Posted April 21 There is a better, less non-invasive way to achieve this. 1. GoTo [WHMCS] -> [Products & Services] -> [Product Addons] -> [{Product to Add The Products To}] -> [Applicable Products] -> [{Add Preferred Addon To Selected Products}] 2. The on the Hosting Server, Edit the file [{Your Template Folder}] -> [Store} -> [Order.tpl} 3. Find the line [ <form method="post" action="{routePath('cart-order-addtocart')}" id="frmAddToCart"> ] 4. Replace it with [ <form method="post" action=" {$WEB_ROOT}/cart.php?a=confproduct&i=0" id="frmAddToCart"> ] 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.