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.

  • Sorry for the very loing delay in getting back to you. Thank you very much for this help. It worked perfectly on my test site once I had changed it to suit the paypal set up.

    Unfortunately now that I am using it in a live site I am struggling to hide paypal in the side cart. The 'fca-action--payment--or' hide is working but nothing else is.
    Paypal is being hidden successfully in the checkout. This is the last script I attempted using:

    {% 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 == "GIFT" || FC.json.items[ i ].category == "SUBSCRIPTION") {
    // Changes text on cart
    $('.fc-action--payment--paypal-express-checkout').text("PayPal can't be accepted for this type of order");
    $('.fc-actions--payment').hide();
    $('.fca-action--payment--or').hide();
    $('.fc-address-entry').hide();
    $('.fc-action--payment--paypal-express-checkout').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 == "GIFT" || FC.json.items[ i ].category == "SUBSCRIPTION") {
    // Changes text on checkout
    $('#fc-payment-method-paypal').text("PayPal can't be accepted for this type of order");
    $('#fc-payment-method-paypal').hide();
    };
    };
    });
    </script>
    {% endif %}


    Ideally it would be even prefer it if I could bypass the side cart and go straight to checkout when products with the category SUBSCRIPTION or GIFT are in the basket.

    Thank you in advance for any help you can give me.
  • marijamarija FoxyCart Team
    @looped

    Thanks for passing that along to us. Off the top, I think $('.fca-action--payment--or').hide(); should be $('.fc-action--payment--or').hide();

    I know you said that was the bit that was working, but I'm wondering if that is causing any issues with something down the line. I don't think so, but it's worth trying.

    There's full-page cart and straight-to-checkout–it sounds like you want to go straight to checkout for those? You can use the cart parameter (with a value of "checkout") on any products you want to go straight to the checkout. More here: https://wiki.foxycart.com/v/2.0/cart#non-product_cart_parameters

    You'll either add that as a hidden input in your form or parameter in the link, depending upon which you're using.

    If you are indeed wanting to just bypass the sidecart and show the cart in a full window, you can check out our article here: https://wiki.foxycart.com/v/2.0/cart#bypassing_your_sidecart

    Let us know if you need us to further look at the script.
  • Apologies, there was a typo in my message ( too tired!!). I do have it set to $('.fc-action--payment--or').hide();

    I am already using a straight to checkout on my add to cart links for those products.
    The problem I have is that a customer might chose to click the 'continue shopping' button on the checkout page. They are then taken back to the site where the basket link is available to them. This link opens the side-cart.

    Which event would I have to target to make the basket link open the checkout page if a SUBSCRIPTION or GIFT product is already in the basket; How can I use an event to check the basket and add the required class to the basket link.

    Thank you for your help.
  • marijamarija FoxyCart Team
    Ah, gotcha. The link to the basket should contain a ?cart=checkout parameter as well if you want it to go straight to checkout.

    Not sure if you're using our mini-cart, which hides the entire mini-cart when it's empty. It's documented here: https://wiki.foxycart.com/v/2.0/javascript#mini-cart_display_and_html_helper_functionality

    If you're showing the cart when it's empty and you use ?cart=checkout, it will still take you to a checkout when the cart is empty. If you want something else to happen when the cart is empty (as you mention, add a class) you can use a JSON request to the cart on your store's page to see if there are any items in the cart and then add the required class.

    Something like this (except I'm just changing the css to display:none). Assuming my id on the basket link is 'cart-link'.

    <script>

    var FC = FC || {};
    FC.onLoad = function () {
    FC.client.on('ready.done', function () {
    if (FC.json.items.length == 0) {
    $('#cart-link').css('display','none');
    }
    });
    }

    </script>


    Hope that helps. Feel free to send a link to your site (you can whisper it) if you need additional help.
  • marijamarija FoxyCart Team
    More about requests to the cart here: https://wiki.foxycart.com/v/2.0/json
  • fc_adamfc_adam FoxyCart Team
    @looped,
    Which event would I have to target to make the basket link open the checkout page if a SUBSCRIPTION or GIFT product is already in the basket; How can I use an event to check the basket and add the required class to the basket link.
    To dynamically skip the cart and go straight to checkout if a specific product already exists in the cart - you would hook into the cart-submit event, and then dynamically append the cart=checkout directive to force that to happen. It would look like this - and you'd add this to your own website's javascript:
    <script>
    var FC = FC || {};
    FC.onLoad = function () {
    FC.client.on("cart-submit", function(params) {
    var directToCheckout = false;
    for (var i = 0; i < FC.json.items.length; i++) {
    if (FC.json.items[i].category == "GIFT" || FC.json.items[i].category == "SUBSCRIPTION") {
    directToCheckout = true;
    }
    }
    if (directToCheckout) {
    params.url += "&cart=checkout";
    params.data.cart = "checkout";
    }
    });
    }
    </script>
  • Thank you. That helped to point me in the right direction.
    In the end I did the following to force the cart link to go straight to checkout:

    FC.client.on('ready.done', function () {
    if(!jQuery.isEmptyObject(FC.json.items)) {
    jQuery.each(FC.json.items, function(i, item){
    if(item.category == "SUBSCRIPTION" || item.category == "GIFT") {
    //$("body").addClass("fc-sidecart--bypass");
    //$("#basket a").addClass("fc-sidecart--bypass");
    $("#basket a").attr("href", "https://shop.foxycart.com/cart?cart=checkout");
    }
    });
    }
    });
Sign In or Register to comment.