WooCommerce Payment Link When Not Logged In

All E-commerce platforms can be complicated and often, with security and other updates, functionality can unexpectedly change. Of course, store owners just want things to work as expected and often are not aware of reasons why something might change. Some of the reasons that functionality of an e-commerce platform can change include:

  • Security issues that have arisen and need to be solved
  • Privacy issues (think the recent GPDR – European privacy regulations)
  • Improving functionality elsewhere requiring reduced or changed functionality in some other area.

In some previous versions of WooCommerce, anyone with a payment link after an order has been generated would be taken to a payment page if they clicked on the link. However, in the most recent version, this does not occur and instead, if a non-logged in user clicks on the “Pay now” link in an email from WooCommerce, they will instead see a message, “This order cannot be paid for.”

Discovering this change can of course be annoying, but there were very likely some good reasons why WooCommerce changed the original behaviour. Perhaps those “good reasons” don’t apply specifically to you, but they might apply to many other business owners in other parts of the world due to the increasing nature of government regulations and intrusions on how business gets done.

As a developer, we share the frustrations of our e-commerce shop owners when some functionality changes and things don’t work as expected. There are thousands and thousands of lines of code in any platform that e-commerce websites are now built on and finding a solution other than reverting back to a previous version can be time consuming.

There are a couple of solutions to this newer behaviour of requiring visitors to be logged-in in order to see the payment details page. One could be to add a message that they visitor must be logged in to view it, and adding a link to the “My Account” page where they will need to enter their username credentials. After successfully logging in, account holders may be able to view past orders and pay for any existing orders via the gateways you have set up that are “pending payment.”

This however, creates an extra step and most of us hate “extra steps” when we’re trying to get something done. If we’re in a hurry, and we just want to pay for an order, being required to log in may mean extra time in looking up passwords and username credentials which for some, takes time if they have not had their browser “remember” those details.

Getting Around The Logged In Requirement

With WordPress, you can often change the behaviour of something by adding code to the ‘functions.php’ file. Having this ability means that any changes you make will not get over-written when your WordPress core or WooCommerce plugin(s) get updated. But you do need to be careful when editing your “functions.php” file. It is code that is in there, and must have the correct syntax; forgetting something that is required could mess your entire website up. That is one of the reasons why you should always back up your “functions.php” file before editing it.

When making changes to your functions.php file (or any other file for that matter), it’s always a good idea to ‘comment’ it with an explanation and a date of what the change is about. This helps you to remember later what you did, and can also be a great help to anyone else that might need to look at customization that has been done. There is nothing more frustrating than trying to understand and modify code that was never “commented.”

Getting back to the problem – adding this code to your functions.php file will get around the WooCommerce requirement of being logged-in, in order to pay for an order with a payment link in a WooCommerce order email:

//WooCommerce: Allow payment of anyone with payment link even if not logged in added 20190116

function isg_custom_order_caps( $allcaps, $caps, $args ) {
if ( ! isset( $caps[0], $args[2] ) ) {
return $allcaps;
}

switch ( $caps[0] ) {
case ‘pay_for_order’:
$order = wc_get_order( $args[2] );
if ( ‘admin’ === $order->get_created_via() ) {
$allcaps[‘pay_for_order’] = true;
}
break;
}

return $allcaps;
}
add_filter( ‘user_has_cap’, ‘isg_custom_order_caps’, 10, 3 );

Please note the two forward slashes (//) on the first line – that indicates a “comment” – a line that will not be read when executing the code, but provides an explanation for the code that follows.

Adding the above to your functions.php code will now mean that anyone with the payment link can view the order details and make payment without having to be logged in!

Leave a Comment