Helpful hints on how to remove Paypal from the cart AND checkout

xcorlettxcorlett Member
in Help edited November 2012
Paypal has a long list of items that are ineligible for processing via their payment service. A store may be selling some items that are okay for Paypal, while some items are not. Some other vendors may wish to disable Paypal payments for items that are more likely to attract chargebacks from Paypal buyers. Here is some code that should help people in this situation build on the basic paypal button hide(), which was originally designed to force buyers to enter their details on the checkout page before going to Paypal.

I don't use categories in my store (everything is "default"), so I assigned the products that I wanted to exclude from Paypal to a different category. Add
<input type="hidden" name="category" value="NOPAYPAL" />
within the <form></form> of any item that you want to exclude from Paypal. Change "NOPAYPAL" to something more appropriate, otherwise you might want to hide the category field on the cart.

On your cart template, add the following right after the ^^cart^^:
<div style="display: none; text-align: center; color: #1A4289; font-family: Verdana, Arial, Helvetica, sans-serif; margin-left: 5%; margin-right: 5%;" id="div_nopaypal">Insert a message regarding the lack of a paypal button.</div>

Change the style to match any customizations you have made to the cart.

Then add the following script. I have it between <head> and </head>.
<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function() {
	for (var i=0; i<fc_json.products.length; i++) {
		if (fc_json.products [ i ] .category == "NOPAYPAL") {
			jQuery("#fc_cart_container a.fc_cart_checkout_paypal").hide();
			jQuery("#div_nopaypal").show();
		};
	};
});
</script>

If you don't want to display a message, remove the div_nopaypal div and the section of the script that shows it.

Then go to your checkout template and insert the following:
<script type="text/javascript" charset="utf-8">
	 jQuery(document).ready(function() {
	 	setTimeout(function() {	
			for(var i=0;i<fc_json.products.length;i++) {
				if (fc_json.products[i].category == 'NOPAYPAL') {
					jQuery("li#fc_payment_method_paypal_container").hide();
				};
			};
		}, 1500);
  });
</script>
For some reason, I can't get the second script to work without the 1500 ms delay, so the paypal button stil appears for a short while then disappears if the script has found a product in the NOPAYPAL category. I'm sure it could be set to display:none and then made visible instead.

Forgive my n00b-ness at coding... I hope this can help other people in similar situations. A better implementation might be to use a hidden product field that would disable paypal, instead of using categories.
Comments
  • fc_adamfc_adam FoxyCart Team
    @xcorlett,

    Thanks for posting your approach!
  • loopedlooped Member
    Please could you advise me as to how to make this approach work with FoxyCart version 2.0
    I would like to remove the Paypal payment options from the cart and checkout for Subscription purchases which will have a 'SUBSCRIPTION' category.
  • marijamarija FoxyCart Team
    Hi @looped -

    Good question! It's actually a bit simpler for 2.0. You'd need a category called 'SUBSCRIPTION', as it looks like you've figured out, then you'll associate your Add to Cart to that category as above (form example):

    <input type="hidden" name="category" value="SUBSCRIPTION" />

    Let us know if you need additional help associating the category with the Add to Cart.

    You can place a script similar to this in your Templates configuration custom footer. Here's a link to that page: https://admin.foxycart.com/admin.php?ThisAction=TemplateConfig . If Add custom header and footer code to your templates isn't checked, you'll need to check that to expose the custom footer: input box.


    {% if context == 'cart' %}
    <script type="text/javascript" charset="utf-8">
    FC.client.on('render.done', function() {
    for (var i=0; i<FC.json.items.length; i++) {
    if (FC.json.items[ i ].category == "SUBSCRIPTIONS") {
    // Changes text on cart
    $('.fc-action--payment--paypal-express-checkout').text("PayPal can't be accepted for this type of order");
    $('.fc-action--payment--or').hide();
    };
    };
    });
    </script>
    {% elseif context == 'checkout' %}
    <script type="text/javascript" charset="utf-8">
    FC.client.on('render.done', function() {
    for (var i=0; i<FC.json.items.length; i++) {
    if (FC.json.items[ i ].category == "SUBSCRIPTIONS") {
    // Changes text on checkout
    $('#fc-payment-method-paypal').text("PayPal can't be accepted for this type of order");
    };
    };
    });
    </script>
    {% endif %}


    The class/id you'll use on the PayPal payment option will change depending upon which PayPal Payment Method you have selected in your gateway settings. The example above is for Accept payments using your PayPal Express Checkout (Legacy) account:. If you're using Accept payments using your PayPal Express Checkout (Reference Transactions) account: the class on the cart will be fc-action--payment--paypal-ec and the ID on the checkout will be fc-payment-method-paypal_ec.

    Hope that helps. Feel free to whisper your FoxyCart domain if you'd like us to take a look at your specific setup.

Sign In or Register to comment.