Fabio R Posted December 4, 2020 Share Posted December 4, 2020 Hi, I would like to hide a payment gateway only when a customer tries to purchase an addon for a product, let me explain the scenario and the reason: Scenario: I have two payment gateways, they are not native or known as Paypal or Stripe, but they work well, so in summary they work like this: Payment gateway 1: creates a recurring subscription, so the customer is automatically charged every month directly to their credit card. Payment gateway 2: does not create a subscription, so the customer needs to access the invoice manually and pay. Problem: When a customer places the product and addon on the same order, everything works well, as a "plan is created with these items" and the gateway can always charge it. When a customer places a product order first and only then chooses to place an order for an addon for that product, that's exactly my problem. That is, if at the time of ordering the addon he chooses the gateway that creates a subscription, he will receive an error and abandon the cart. Possible solution: I'm not sure if there are better ways to do it, but as a layman I think an alternative would be to create a hook to hide the recurring subscription gateway just at the checkout for an addon order. So in the end it should behave like this: For product orders, the two gateways appear normally (which is already the case today) For orders for addons purchased later, hide one gateway. I tried WHMCS support before, they just told me that they understand how useful this is, but that they don't have this option available (WOW - it seems so important and that could help so many people not to look at it). So I found excellent articles here in the community written by Brian, but unfortunately I don't have the technical knowledge to create a hook with the condition "If it's a" normal "order, show both gateways. If the order is just for an addon later, hide one gateway and show another gateway ". Note: My WHMCS version is 7.10.2 and I use a Lagom theme for the client area. Unfortunately my English is not good, so I'm sorry for the language barrier! Any help or tip would be welcome! Tks! 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted December 12, 2020 Share Posted December 12, 2020 On 04/12/2020 at 14:13, Fabio R said: So I found excellent articles here in the community written by Brian, but unfortunately I don't have the technical knowledge to create a hook with the condition "If it's a" normal "order, show both gateways. If the order is just for an addon later, hide one gateway and show another gateway ". try the following hook... <?php # Remove Gateways for Product Addons Hook # Written by brian! function cart_gateway_removal_for_product_addons($vars) { if ($vars['templatefile']=='viewcart'){ $gateways = $vars['gateways']; $allowed = ['mailin','paypal']; if (count($vars['addons']) > 0 && count($vars['products']) == 0) { foreach ($gateways as $k => $item) { if (!in_array($item['sysname'],$allowed)) { unset($gateways[$k]); } } if (count($gateways) > 0) { return array("gateways" => $gateways); } } } } add_hook("ClientAreaPageCart", 1, "cart_gateway_removal_for_product_addons"); the only line you should need to change is the $allowed array that contains the list of valid gateways to be used with product addons. the hook will check whether there are product addons in the cart, but not products (i'm ignoring whether domains are in there for now) - if there are addons, but no products, then all gateways (other than those listed in $allowed) will be removed... if the hook tries to remove all available gateways and would leave the user with no gateway options, then the hook does not make any changes. 1 Quote Link to comment Share on other sites More sharing options...
Fabio R Posted December 15, 2020 Author Share Posted December 15, 2020 Hi Brian, WOW! Thank you so much for looking at this and trying to help us. I believe that like me, this could be useful for many WHMCS users looking for this 🙂 Your code worked perfectly well! Thank you again!! But, it could also hide the "Payment Details" too? It may seem strange, but my payment gateway opens a new secure tab for the customer to pay manually with any card, so having these card options for that gateway on this screen can be very confusing as well. If this information is not useful, please excuse me, I'm not a programmer and I'm just trying to make an effort to contribute to it. I hope you have a great day! Fábio 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted December 17, 2020 Share Posted December 17, 2020 possibly you could wrap it in an IF statement to detect when these gateways have been removed.... or use CSS to hide the creditCardInputFields div if the hook is triggered.... almost certainly, it can be hidden one way or another. 0 Quote Link to comment Share on other sites More sharing options...
Fabio R Posted December 17, 2020 Author Share Posted December 17, 2020 Hi Brian, Thanks again for your reply! To be honest, I have trouble understanding all of these codes. I'm not a developer, but I'm trying my best to try to contribute here. If I understand your valuable tip well, I should add it within the condition to hide the <div class = "section" id = "creditCardInputFields"> too. Since I have no experience and knowledge about this, I started researching how he could do, everything seems to me to sum up in something like "display: none" but I'm not sure where I put it and I am not able to make it work. Here's what I'm trying to do: <?php # Remove Gateways for Product Addons Hook # Written by brian! function cart_gateway_removal_for_product_addons($vars) { if ($vars['templatefile']=='viewcart'){ $gateways = $vars['gateways']; $allowed = ['my-gateway-name']; if (count($vars['addons']) > 0 && count($vars['products']) == 0) { foreach ($gateways as $k => $item) { if (!in_array($item['sysname'],$allowed)) { unset($gateways[$k]); #creditCardInputFields { display: none } } } if (count($gateways) > 0) { return array("gateways" => $gateways); } } } } add_hook("ClientAreaPageCart", 1, "cart_gateway_removal_for_product_addons"); I'm certainly doing it wrong, because I broke everything. Please, could you point me to where I'm going wrong? If we do that, it’s perfect and all checkout problems go to space. Fábio 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted December 27, 2020 Share Posted December 27, 2020 the issue will be that you can't add CSS changes how you're trying to do it - assuming the CSS ID of yours is correct, it should be... <?php # Remove Gateways for Product Addons Hook # Written by brian! function cart_gateway_removal_for_product_addons($vars) { if ($vars['templatefile']=='viewcart'){ $gateways = $vars['gateways']; $allowed = ['mailin','paypal']; if (count($vars['addons']) > 0 && count($vars['products']) == 0) { foreach ($gateways as $k => $item) { if (!in_array($item['sysname'],$allowed)) { unset($gateways[$k]); } } function addon_gateway_custom_headeroutput_hook($vars) { $header_return = '<style>#creditCardInputFields {display: none !important;}</style>'; return $header_return; } add_hook("ClientAreaHeaderOutput",1,"addon_gateway_custom_headeroutput_hook"); if (count($gateways) > 0) { return array("gateways" => $gateways); } } } } add_hook("ClientAreaPageCart", 1, "cart_gateway_removal_for_product_addons"); 1 Quote Link to comment Share on other sites More sharing options...
Fabio R Posted December 28, 2020 Author Share Posted December 28, 2020 Hi Brian! Thank you very much for your new help. This worked perfectly well :) Yesterday was my birthday and that was a great gift. I hope this is useful for other users in the community, just as it is for me. I wish you and your family a great holiday! Regards, Fábio 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted December 29, 2020 Share Posted December 29, 2020 23 hours ago, Fabio R said: Yesterday was my birthday and that was a great gift. Happy Birthday Fábio! 🎂 1 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.