Error on checkout - Wordpress - php

I have this extremely strange error that only happens when people try to order. Guests come onto our site and add stuff to their cart. Then they proceed to go to the checkout. On checkout when they click proceed to paypal they get the following error: "We were unable to process your order, please try again.". However on reloading the page the error is gone when they try to recheckout. No plugins or themes are conflicting as I checked all of them.
I did some debugging and I noticed that the reason the error is thrown is because of the function: wp_verify_nonce() I did some further debugging and found out that during the first time the user tries to checkout the above function is called. And returns false because the nonce is different when compared to what is currently hashed. However on the second checkout the above function returns true because the nonce is correct. This means that for some reason my nonce is wrong the first time. Does anyone know why?
I'm getting this problem on the following function:
process_checkout() When the following command is run:
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-process_checkout' )) {
WC()->session->set( 'refresh_totals', true );
throw new Exception( __( 'We were unable to process your order, please try again.', 'woocommerce' ) );
}
I tried my best to explain my issue but does anyone know why my nonce is wrong during checkout on the first time?

The issue isn't really with wp_verify_nonce() or the code you posted.
The way wp_nonce work is like a queue number, just like your processing number at the DMV.
My best guess on what's happening is an extra wp_nonce being created when people checkout and wp_verify_nonce() is receiving a different one from what's expected. There's no way for me to really identify where it's happening without looking at the code base.
If no custom code has been done for these parts then it really means there's a subtle plugin conflict with WooCommerce. It could be even a conflict with the theme.

Related

WooCommerce webhooks sending an empty body

When the webhook fired, I could see in the other end, that the body of the request was empty.
I then enabled WP_DEBUG, shot another API-call and checked the log-files ( WooCommerce >> Status >> Logs >> Log-file for the given day ), and could see that it indeed was empty. So it wasn't data 'falling off the truck' on the way to the destination.
I found where the shot was done in the code, and it looked like this:
do_action( 'my_custom_webhook', $order->id, [], $order );
So I changed that to:
error_log( $order );
do_action( 'my_custom_webhook', $order->id, [], $order );
And could see that the $order wasn't empty.
WIERD!
Debugging steps taken
1. Webhooks defined properly
I spent quite a bit of time, diving down into the webhooks, seeing if it was something there, that was setup incorrectly. Or some legacy-code that had been deprecated.
2. Error logs
I dug through the error-logs several time. Assuming that it was some bad code, that encountered an error, before the API-call was sent. But it didn't make any sense, since that most likely would stop the API-call from firing entirely. Hmm!
3. Webhooks deactivating automatically
The API I was hitting, also returned and error 500, when the body was empty. So the webhooks deactivated themselves. Whenever I tried to reactivate them, I still got an error (either of these two):
Error: Delivery URL returned response code: 500
Error: Delivery URL cannot be reached: cURL error 28: Operation tiemd out after 5001 milliseconds with 0 bytes received.
I thought for a long time, that that was centralt to the problem.
But I couldn't get the API changed. So there was no good way around this.
I eventually realized, that even though that I got that error, the the webhook was still activated... And worked!
So I simply ignored that error entirely and moved on.
4. Permissions
I thought that maybe it was something about permissions, since I would see that I got a woocommerce_rest_cannot_view-error.
System details
WordPress-version 5.8.1
WooCommerce-version: 4.9.3
The site is a custom site, that has been built on Trellis, Bedrock and Sage.
The solution was found here: https://github.com/woocommerce/woocommerce/issues/24533#issuecomment-551148570
Simply delete the webhook(s) and create it/them again with the exact same contents.
¯\_(ツ)_/¯

Wordpress functions.php won't save using unset() PHP function

I have the unset function in my functions.php theme file and WordPress no longer allows me to save my code in the editor.
I'm using it to unset product tabs on WooCommerce as per this code (found on the WooCoomerce site - https://docs.woocommerce.com/document/editing-product-data-tabs/):
add_filter( 'woocommerce_product_tabs', 'custom_remove_product_tabs', 98 );
function custom_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
return $tabs;
}
With this code in I get the generic error of: "Something went wrong. Your change may not have been saved. Please try again. There is also a chance that you may need to manually fix and upload the file over FTP." when saving my functions.php file. If I comment out the line with the unset function is saves just fine.
It does work on the site if I save this code using FTP etc so I appreciate some may say just do that, but I like to quickly edit things inside WordPress sometimes which is no longer possible so want to understand why it's happening (and WordPress have docs on using this function themselves so it's driving me mad!). I've tried adding a standard php unset function with a generic variable to check it's not WooCommerce related but this doesn't work either.
Am I missing something? If not, is it a server setting? We're using PHP 7.4 on a litespeed server so should be up to date. I'm not sure if there is a way to see a more detailed error log from this issue so if there is any help finding it would be great.
Thanks for any help!

How to fix 504 error caused by a plugin in WordPress?

I get a 504 error when I want to access the admin dashboard in my client website. I've contacted technical support in siteground and he told me that the error caused by a plugin, and he was right. When I desactivate the plugin, I can enter the dashboard again.
How can I fix this problem since there are no errors or warnings, I checked my PHP logs, and it just shows me a PHP Notice:
Trying to get property of non-object in /home/mysite/public_html/wp-includes/class-wp-query.php on line 4056
public function is_singular( $post_types = '' ) {
if ( empty( $post_types ) || ! $this->is_singular ) {
return (bool) $this->is_singular;
}
$post_obj = $this->get_queried_object();
//here is the error
return in_array( $post_obj->post_type, (array) $post_types );
}
So, does this PHP notice make a timeout error? or there is another reason?
Any advice?
You haven't provided enough information for people to help you. 504 error, is a timeout, meaning that the plugin you are using doing something on the background for too long, longer than your server timeout is set, usually 30 seconds.
We don't know plugin you are using, but if it do something with large amounts of posts, pages, images or products.. like preparing them in a specific order, format or similar, this is likely the cause.
Just to give you an example, a plugin that do Product filters often run multiple loops to format everything in a certain way before it could be used, but if you have 10.000+ products and a slow/low resources hosting it could easily go over 30 seconds.

How can i call a plugin shortcode from within a function with theme functions.php file?

I use a plugin (Popup Builder) on my WordPress site. Whenever I create a new popup with the plugin, it creates a shortcode for that popup. I want to call on that shortcode from within the theme functions.php file. But I can't seem to get it to work properly.
The shortcode runs only if conditions are met. And in this example it's when a visitor access the site for the first time. I then check for a certain cookie and if that cookie does not exist, the popup will fire up and force the visitor to choose one option from a list of options, and then the cookie will be set with the correct value, once they do.
However I cant seem to find a solution that fires the popup at all. An I also get this notice: Notice: do_shortcode_tag was called incorrectly. Attempting to parse a shortcode without a valid callback:
function check_for_cookies() {
// Check if cookie is already set
if(isset($_COOKIE['tln_valgt_fylke'])) {
// Do this if cookie is set
$string .= 'Hi, and welcome back!' ;
return $string;
} else {
// Do this if the cookie doesn't exist
$shortcode = do_shortcode("[sg_popup id=163]");
return $shortcode;
}
}
add_action('init', 'check_for_cookies');
What am I doing wrong, and what if this is not a good way of accomplishing what I want, then what is?
This is just a guess
But, I think its a timing issue. You are using the init action to hook into another plugins shortcodes. It's possible that plugin has not yet registered it's shortcode via add_shortcode or if it has registered it, it may not have "included" the file that defines the callback for it (for whatever reason).
In fact it seems likely because:
do_shortcode_tag was called incorrectly. Attempting to parse a shortcode without a valid callback
This indicates the shortcode was called and no callback existed for it. This is not a problem with your code per say. But it says that the plugin has not yet loaded the file that contains the callback.
You could test this by hooking into an action that happens later in the execution chain, after you know all plugins have been loaded and initialized. Like even wp_head
Perhaps you could even get away with changing the priority of the hook:
add_action('init', 'check_for_cookies', 20); //default is 10
This way it's triggered at the end of init, but even then it may be too soon. The only real way to know is to look at the code for the plugin and find out when it's registering it's "stuff". An easy way to do that is add this code to the plugins shortcode callback:
try{
throw new \Exception();
}catch(\Exception $e){
die("<pre>{$e->getTraceAsString()}</pre>");
}
This will throw and then catch an exception, and then output the stacktrace. Which may show you exactly where the shortcode is being setup. You'll have to trigger the callback (obviously) for it to work. You can also use print_r(debug_backtrace()) but it's much harder to read IMO.
PS I been doing a lot of WP work lately and I just had an issue with action timing ... lol. That was why I thought of it, I spent the last 2 days refactoring code. In my case I an replacing the add/edit/profile parts of the user system on both the front and back end. It's a sort of subuser plugin. And there is a lot of competing actions related to that if you know what I mean...

Wordpress CiviRCM Content Formatting

I'm trying to figure out how to properly format the following site's pages:
http://marchofremembrancehouston.org/march/?page=CiviCRM&q=civicrm/event/register&reset=1&id=25
See how the CiviCRM data is pushed to the top right of the site? I would like the form to fit inside the content area of the theme.
I've already done a lot of research and no one seemed to give a clear answer.
This is a bug in CiviCRM. You're using a WordPress plugin that processes each page's content to create tags for Facebook Open Graph (to populate the blurb and image that goes on your Facebook post when you share the page). The problem is that processing the content triggers CiviCRM to run, and consequently, it prints all the content in the head of the page.
The issue describing this in a little more depth is here:
https://issues.civicrm.org/jira/browse/CRM-14244
The next release of CiviCRM (4.4.5) will contain the fix, but if you're in a hurry to publish the page, here's what you can do:
Go in the files of your site to wp-content/plugins/civicrm/civicrm.php
Scroll down to line 412 or so (depending upon your version) and look for the line saying
public function invoke() {
Add the following lines below it:
if ( !in_the_loop() && !is_admin() && empty($_REQUEST['snippet']) ) {
return;
}
What that says is, if you aren't displaying the main content of the page (running "The Loop"), showing an admin page, or displaying a "snippet" (CiviCRM content that belongs within another page), go back and do nothing. CiviCRM will be invoked again when it's the time to run the actual page content.
For reference, the pull request I made in GitHub to handle this is at https://github.com/civicrm/civicrm-wordpress/pull/36/files, where you can see the end result.
If you're in CiviCRM 4.3 or earlier, you'll want to add those lines to the function civicrm_wp_invoke, inserting them after the following (at or near line 292):
function civicrm_wp_invoke() {
This fix is not complete and breaks chain loading AJAX queries within CiviCRM, e.g. for retrieving state dropdown lists if a country is picked in CiviProfiles. The actual fix should check for this:
if ( !in_the_loop() && !is_admin() && empty($_REQUEST['snippet']) && ($_REQUEST['q'] != "civicrm/ajax/jqState") ) {
return;
}
NOTE: this is not yet fixed in 4.4.5 but adding the above line to wp-content/plugins/civicrm/civicrm.php should allow for the state selection to start working again, otherwise you'll also notice a nasty JavaScript error since the returned value is HTML, but the ajax call expects JSON. Why this sort of bug is not caught and fixed prior to release in a simple automated test, to this day I do not understand.

Categories