wc_add_notice not working in production server - php

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):

Related

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');

WooCommerce customisations break page in PHP 7.2 but not 7.0

I've got the following function that modifies the checkout fields in WooCommerce. The code works fine in PHP 7.0 but after upgrading to 7.2 the checkout page just displays a white screen.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_phone']['label'] = 'Mobile number';
$fields['billing']['billing_address_2']['label'] = ' ';
$fields['billing']['billing_first_name']['class'] = '';
$fields['billing']['billing_last_name']['class'] = '';
$fields['shipping']['shipping_first_name']['class'] = '';
$fields['shipping']['shipping_last_name']['class'] = '';
return $fields;
}
If I comment out some of the $fields[ ... lines the page works again, however it seems to be random which ones I comment out to make it work!
First of all: Why does this upset PHP 7.2? I'd really like to understand why this breaks the page.
Second: How can I make this customisation work in PHP 7.2?
-
FYI: My testing environment only has the WooCommerce plugin activated and is using the Twenty Seventeen theme

Weird issue with PHP on localhost vs test server (apache2)

I am working on a website running WordPress and woocommerce
I have a custom function to add stuff to the cart which is the following:
function custom_add_products_to_cart(){
WC()->cart->empty_cart();
$request_body = file_get_contents('php://input');
$decoded = json_decode($request_body);
$cartElements = $decoded->addToCart;
foreach ( $cartElements as $product_id ) {
WC()->cart->add_to_cart( $product_id );
}
if ( $decoded->Uid ) {
WC()->session->set( 'uid', $decoded->Uid );
}
die();
};
This is quite a straightforward function. All it does is iterating through a list of products id and launching the default Woocommerce add to cart function.
The issue I've got is that this works perfectly fine on my localhost (also debugging it ut behaves just like expected) but when I try it on a test server it doesn't work.
The function is firing (i tried to print some messages) but the cart is not emptying and the new products are not added.
I check the code, commit and revision, and everything is correct.
What else can it be?
My last thought was on the version of PHP:
my localhost runs 7.1.2 while the test server runs 7.0.22 - can it be the PHP version or not? any idea on what else I could try?
Sorry if I cannot provide much more details but unfortunately there's not much more to add...
Also, I am not posting this to WordPress community for now as I think it's not a WordPress related stuff (nor woocommerce) but rather PHP code (maybe my function is somehow wrong?) or PHP version
Thanks in advance to everyone
i think the problem is here
file_get_contents('php://input');
depending on your PHP configuration, maybe you need to change the allow_url_fopen setting in you php.ini.
You have two ways of getting around it without changing php.ini, one of them is to use fsockopen(), and the other is to use cURL.
I recommend using cURL over file_get_contents() anyways, since it was built for this.
At the end i simply solve this by calling two functions:
the first one empties the cart:
function empty_cart(){
WC()->cart->empty_cart();
}
and the second one carries on adding to the cart:
function custom_add_products_to_cart(){
$request_body = file_get_contents('php://input');
$decoded = json_decode($request_body);
$cartElements = $decoded->addToCart;
foreach ( $cartElements as $product_id ) {
WC()->cart->add_to_cart( $product_id );
}
if ( $decoded->Uid ) {
WC()->session->set( 'uid', $decoded->Uid );
}
die();
};
and it now works properly.

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.

woocommerce - checkout fails/doesn't work

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.

Categories