woocommerce - checkout fails/doesn't work - php

My woocommerce checkout is showing and up till this morning was working fine. But now when I try to submit the form I get this message: "We were unable to process your order, please try again". I haven't changed anything in the settings or plugins. I have updated woocommerce recently to 2.3.5. But still, everything worked perfectly after that. I really don't get what's going on here. I hope someone can help me out.
Thanks!
PS: I get this error message with everything, even when I leave a billing field open...
_____ EDIT ______
I found the function which throws the error:
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' ) );
}
This is located in in woocommerce/includes/class-wc-checkout.php on line 351.

I got the solution. With the WooCommerce 2.3.5 update, two new files got added: payment.php & payment-method.php. In payment.php there's this line:
<?php wp_nonce_field( 'woocommerce-process_checkout' ); ?>
Somehow this line got removed in my child theme.

Someone mentioned that they had this problem when using the GoogleRECAPTCHA plug-in with Woocommerce. They disabled it, and the problem went away. :)

#SPS if you really are on woocommerce 2.3.5, there should be the file payment.php
wp-content/plugins/woocommerce/templates/checkout/
Meanwhile, that's not the actual culprit in my own case; (as there was already
<?php wp_nonce_field( 'woocommerce-process_checkout' ); ?>
there )
It's rather the fact that inside, the process_payment(){} function of my payment plugin, i was returning:
array
(
'result' => 'success',
'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))))
);
instead of
array
(
'result' => 'success',
'redirect' => add_query_arg('order',$order->id, add_query_arg('key', $order->order_key, get_permalink(woocommerce_get_page_id('pay' ))))
);
Check your code to change that too, and the problem will dissapear. :)

As an alternate fix, if you are using a self-signed SSL certificate in staging (or if your SSL is expired) and you have elected to force https in the checkout view, it will cause PayPal to fail with this rather unhelpful message.
You'll know you have this problem because WordPress will be constantly logging you out of the admin and forcing you to re-authenticate any time the secure/insecure hand off happens.

Related

wc_add_notice not working in production server

I have developed a payment gateway for WooCommerce. The thing is, when I developed it, the version of WooCommerce was 4.7.0 (It still is in my localhost) but in the production server the version is 5.1.0. I had to use a webhook like so:
function webhook() {
$data = json_decode($this->df_check_payment(), true);
$code = $data['result']['code'];
$description = $data['result']['description'];
if ($code != WC_Gateway_Datafast::DF_SUCCESS_CODE){
wc_add_notice( __("Payment failed -> Code: $code Description: $description" , 'gateway'), 'error' );
wp_safe_redirect( wc_get_page_permalink( 'cart' ) );
} else{
$orderId = $data['customParameters']['SHOPPER_documentoId'];
$order = wc_get_order( $orderId );
$order->set_transaction_id($data['id']);
$order->payment_complete();
wp_safe_redirect($this->get_return_url($order));
}
}
This works fine in localhost and the wc_add_notice is shown in the cart page but it's not working in production. I've tried with wc_get_page_permalink( 'checkout' ) but it doesn't work either, the notice is not shown. My guess is it has to do with the WC version but if that's the case, what's wrong? Did the API change? I really couldn't say if it was working in production with version 4.7.0 because I developed it before someone decided to update the production's wc plugin.
EDIT: I have updated the WC version in localhost and it works fine. Production still doesn't work. I thought maybe a plugin was putting something on top of the notice so it wasn't visible. I examined the HTML looking for the text in the notice but it's not there, which means it's not being generated at all.
This is how it looks in localhost (notice shows just fine):
And this is production (no notice whatsoever):

Easy Digital Downloads - Fire PHP when transaction was successful goes wrong

I’m using Easy Digital Downloads for my Wordpress webshop. After someone buys a item it needs to add credits to the MySQL database. I got this working by adding PHP code to shortcode-receipt.php.
This is working correct but when I reload the receipt via browser or mail the PHP code will fire again:
php
<?php
if( edd_is_payment_complete( $payment->ID ) && edd_receipt_show_download_files( $item['id'], $edd_receipt_args, $item ) ) :
?>
Could someone help me out here?
What is the best method to fire PHP code when a payment is successful?
Thanks in advance!
There is action edd_complete_purchase which fires when order is completed.
So, in your case, I would remove the code from the shortcode and created an plugin. Inside the plugin should be something like this
function my_edd_receipt( $payment_id ){
if( edd_is_payment_complete( $payment_id ) && edd_receipt_show_download_files( $item['id'], $edd_receipt_args, $item ) ) :
}
add_action( 'edd_complete_purchase', 'my_edd_receipt');

Wordpress permissions issue when trying to upload and atach file to post

I'm having real headache with attaching photo to post in WordPress. Everything works perfectly when I'm logged in as an administrator. For other users (with edit_posts permission set), I'm getting
Sorry, you are not allowed to attach files to this post
error. Checked if statement in the wp_ajax_upload_attachment() function and it reads the following:
if ( ! current_user_can( 'edit_post', $post_id ) )
What I did to narrow down the issue was to concatenate piece of code to AJAX response:
'data' => array(
'message' => __( 'Sorry, you are not allowed to attach files to this post.' ).' '.print_r(current_user_can( 'edit_post', $post_id ))
To my surprise value concatenated to response is 1. Now I'm thinking that maybe using print_r is messing up. Otherwise it means that current_user_can returns different value in second invocation. Checked POST data, also checked that $post_id has correct value.
Can anyone help me to figure out what is going on here?
Thanks,
Marcin

Show plugin incompatibility error on plugin page in wordpress

I am developing a custom plugin for woocommerce. For now i support it for few version of woocommerce. So i want to check and show incompatibility error if some is using lower version of woocommerce than the version i minimal support.
I want to show the error message on plugin page in admin panel under the my plugin listed.
I have function to get woocommerce version and checking incompatibility using if else condition. But i have no idea how to display the error message as i want.
So please help.
Thanks in Advance.
This is how I do it in my own plugin:
add_action( 'plugins_loaded', 'so_31217783_version_test' );
function so_31217783_version_test(){
$required_woo = '2.1.0';
if ( ! defined( 'WC_VERSION' ) || version_compare( WC_VERSION, $required_woo, '<' ) ) {
add_action( 'admin_notices', 'so_31217783_admin_notice' );
return false;
}
// add the rest of your actions here
// they will only be triggered if the
// version test has been passed
}
function so_31217783_admin_notice() {
echo '<div class="error"><p>' . sprintf( __( 'My custom plugins requires at least WooCommerce version %s in order to function. Please upgrade WooCommerce.', 'your-custom-function' ), $required_woo ) . '</p></div>';
}
The base explanation is that you check the version of WooCommerce very early on and then shut down your plugin if the minimum version is not met. You also add a function to the admin_notices hook so that you can tell the user what has happened.

Wordpress - What capability is required to view posts, scheduled in the future, by someone else?

So, I'm trying to create a number of custom roles within my theme to meet with the custom requirements of the site.
The first, and most simple, being 'external viewer' who I want to simply be able to view posts (both built in and custom post-types) that someone else on the content team has created and scheduled for some point in the future.
Initially I tried this
$viewer_capabilities = array(
'read_private_pages' => true,
'read_private_posts' => true,
'read' => true
);
add_role('external_viewer', 'External Viewer', $viewer_capabilities );
But when logging into /wp-admin I just got the 'no permission' message and I couldn't do anything. If I went to one of the scheduled posts, I just got 'not found'.
So then I tried
$role_object = get_role( 'external_viewer' );
$role_object->add_cap( 'read_private_pages' );
$role_object->add_cap( 'read_private_posts' );
$role_object->add_cap( 'read' );
Which got me a little further. I can login and see the admin dashboard, but if I visit a scheduled post, then I still get 'page not found'.
Can anyone see what I'm missing?
I believe the capabilities you want are some combination of edit_private_pages, edit_private_posts, edit_published_posts, edit_published_pages, delete_published_pages, and delete_published_posts. The names are self explanatory.

Categories