Jump to content

Hide a payment gateway for addon orders


Fabio R

Recommended Posts

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!

Link to comment
Share on other sites

  • 2 weeks later...
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.

Link to comment
Share on other sites

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.

image.png.240b3e00e8f260b70740425e8c786478.png

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...

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");
Link to comment
Share on other sites

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

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