Show plugin incompatibility error on plugin page in wordpress - php

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.

Related

remove_action wc_empty_cart_message not working in WooCommerce

I would like to REMOVE completely the wc_empty_cart_message. My site has no cart. and when the item is removed from checkout the user is redirected to the home page. But then upon browsing to a shop page, the message "your cart is not available whilst your checkout is empty" appears and is completely unnecessary.
I have seen many of questions/answers about how to change the message to something else (I tried some of them which also did not seem to work).
I tried this which seems to be the right thing to do, but for some reason it does not make any visible change on my website.
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
Is there any way to check which hook is creating the message from the front-end? Or any way how I can see what is overruling this command from executing?
To remove the "Your cart is currently empty" message, use:
function action_woocommerce_cart_is_empty() {
// Remove
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
}
add_action( 'woocommerce_cart_is_empty', 'action_woocommerce_cart_is_empty', 9, 0 );
Where the remove_action in an add_action with a lower priority number (9)
To remove the "Checkout is not available whilst your cart is empty" message, use:
function filter_woocommerce_add_notice ( $message ) {
// Equal to (Must be exactly the same).
// If the message is displayed in another language, adjust where necessary!
if ( $message == 'Checkout is not available whilst your cart is empty.' ) {
return false;
}
return $message;
}
add_filter( 'woocommerce_add_notice', 'filter_woocommerce_add_notice', 10, 1 );
Based on the information in the answer from #7uc1f3r and some other tutorials I was checking recently, I discovered that this also does the trick:
add_filter( 'woocommerce_add_notice', '__return_null' );

Wordpress run function before plugin is updating

I am trying to accomplish the following:
Whenever my plugin is updated via the wordpress plugin update function, I want it to execute a function which backs up certain plugin files first before the upgrade is running.
I was checking through available hooks on wordpress, however only found the upgrader_process_complete hook, which according to the wordpress codex website:
The upgrader_process_complete action hook is run when the download process for a plugin install or update finishes.
While "the download process" is a little bit unclear, I have checked in the source code and it appears that the hook is called AFTER the plugin has been installed, meaning the plugin files are already overwritten and cannot be backed up anymore.
Is there a way to accomplish this hook or is wordpress missing this functionality to call a function before the plugin update progress is initiated ?
You can use upgrade_pre_install filter of WordPress which is executed before upgrade start to deactivate the plugins. check the snippet below, hope this will help to play around plugin backup before updates.
add_filter( 'upgrader_pre_install', 'deactivate_plugin_before_upgrade_callback', 10, 2 );
function deactivate_plugin_before_upgrade_callback( $return, $plugin ) {
if ( is_wp_error( $return ) ) { //Bypass.
return $return;
}
// When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
if ( wp_doing_cron() ) {
return $return;
}
$plugin = isset( $plugin['plugin'] ) ? $plugin['plugin'] : '';
if ( empty( $plugin ) ) {
return new WP_Error( 'bad_request', $this->strings['bad_request'] );
}
if ( is_plugin_active( $plugin ) ) {
//You can play with plugin zip download over here
//Deactivate the plugin silently, Prevent deactivation hooks from running.
deactivate_plugins( $plugin, true );
}
return $return;
}

How to make plugin Worpress self-deactivated?

Good day, I need a plugin to be self-deactivated once the mail is sent to the site owner. However, when I run on local machine the plugins still active in my admin panel.
My code :
if(count($result) == 0){
// Send the mail
send_to_mail();
// self deactivation of this plugin
add_action( 'init', 'deactivate_cronjob_plugin' );
}
// deactivate the plugin
function deactivate_cronjob_plugin(){
if ( is_plugin_active('myPlugin/cron_job.php') ) {
deactivate_plugins('myPlugin/cron_job.php', true);
}
}
I'm using Wordpress 4.9.6, I'm glad if there's any help. Thank you and have a good day.
You need the hole path to the plugin file, like
deactivate_plugins( plugin_basename( __FILE__ ) );
Also the small note that the function is_plugin_active is not necessary. The deactivation works only, if the plugin is active.

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

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