Stop user role changing only when canceling a subscription - php

I'm using WooCommerce Subscriptions plugin and I would like to stop user role changing only when canceling a subscription.
I found a code snippet here:
add_filter( 'woocommerce_subscriptions_update_users_role', '__return_false', 100 );
… which Stop WooCommerce Subscriptions Changing a User's Role. This will stop role changing when purchasing a subscription too.
What I would like is to stop user role changing only when canceling a subscription.
Is it possible?
Thanks

You could try to use woocommerce_subscription_status_cancelled action hook (without any guaranty as it's untested), this way:
add_action( 'woocommerce_subscription_status_cancelled', 'keep_user_role_unchanged_on_cancelled_status' );
function keep_user_role_unchanged_on_cancelled_status(){
add_filter( 'woocommerce_subscriptions_update_users_role', '__return_false', 100 );
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Reference: Subscription Status Change Actions

remove_action( 'woocommerce_subscription_status_cancelled', 'wcs_maybe_make_user_inactive_for', 10, 1 );
Use this in your active theme functions.php

Related

How to separate order review and payment methods sections in WooCommerce checkout page

Problem:
on the checkout page (form-checkout.php) the following action code calls the review order AND the payment method section at the same position (after each other)
do_action('woocommerce_checkout_order_review');
However, I want the order review and payment method sections to be separate, i.e. to call them individually in different places on the checkout page.
Solution attempt:
I came across guidance here and found the file includes/wc-template-hooks.php where the above action is created with he following code:
add_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
add_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
So to test and call ONLY the payment section separately, I went back to form-checkout.php and tried this:
do_action( 'woocommerce_checkout_payment' );
or this
do_action( 'woocommerce_checkout_payment',99 );
But neither calls the payment section!
Could anyone please help me understand why this doesn't work?
Note: I know I can register hook the woocommerce_checkout_payment to a custom function and call it that way, but I'm confused as to why the direct method doesn't work and I'd prefer to keep things simple were possible.
Add this lines to functions.php in your child-theme:
// Detaching `payment` from `woocommerce_checkout_order_review` hook
remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
// Attaching `payment` to my `woocommerce_checkout_payment_hook`
add_action('woocommerce_checkout_payment_hook', 'woocommerce_checkout_payment', 10 );
now you can use the custom hook to show the payment section:
do_action( 'woocommerce_checkout_payment_hook' );
P.S. Thank you #LoicTheAztec for the solution.

Remove structured data from Woocommerce single product pages

I am trying to remove the structured data that Woocommerce adds to the product pages.
I did some research and found that WC_Structured_Data::generate_product_data() generates the structured data markup. It's hooked in the woocommerce_single_product_summary action hook in the woocommerce/templates/content-single-product.php template file.
I tried by adding the following code to the functions.php
remove_action( 'woocommerce_single_product_summary', 'WC_Structured_Data::generate_product_data()', 60 );
So structured data wouldn't be added by Woocommerce, but it doesn't work…
Am I doing something wrong? Is there another way to do what I am trying to achieve?
Instead, you can use dedicated filter hook 'woocommerce_structured_data_product' that is located in WC_Structured_Data for generate_product_data() method nulling the structured data output in single product pages:
add_filter( 'woocommerce_structured_data_product', 'structured_data_product_nulled', 10, 2 );
function structured_data_product_nulled( $markup, $product ){
if( is_product() ) {
$markup = '';
}
return $markup;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I suspect people want to remove the default tabs and they come here after they see the Woocommerce content-single-product.php template. In this template you see that generate_product_data() is hooked with priority 60.
After inspecting the hooks that run on woocommerce_single_product_summary.
You can easily remove the tabs with:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_output_product_data_tabs', 60 );
I think Woocommerce forgot to mention this add_action.
This is how you can remove hooks associated with instantiated object method. You have to find the variable that holds the new Object instance.
In this case the main WooCommerce object is accessible as $GLOBALS['woocommerce'] and it has public property $structured_data which holds an instance of the WC_Structured_Data object.
Hence, to remove the hook in the question you can write this code:
remove_action( 'woocommerce_before_main_content', array( $GLOBALS['woocommerce']->structured_data, 'generate_website_data' ), 30 );
Add to functions.php:
add_action('wp_loaded', function() {
remove_action('woocommerce_single_product_summary', [$GLOBALS['woocommerce']->structured_data, 'generate_product_data'], 60);
});
Unhooks WC_Structured_Data::generate_product_data(). Will not waste resources on generating product data first for no reason and then "nulling" that same generated data a moment later using a filter.

Wordpress Admin delete update-nag for user

I want to hide/delete the update notification of WordPress v4.9.1 from administration panel.
You can add this to your functions.php.
function hide_update_nag_admin()
{
if (current_user_can('update_core')) {
remove_action( 'admin_notices', 'update_nag', 3 );
}
}
add_action( 'admin_notices', 'hide_update_nag_admin', 1 );
The function checks to see if the current user has permission to update the core, if they do, then remove the update nag from the admin. We then add the hook into the admin_notices function.
Here's some reading about admin_notices:
https://digwp.com/2016/05/wordpress-admin-notices/
And a similar question:
https://wordpress.stackexchange.com/questions/231010/remove-update-nags-for-non-admins
And a plugin to do it:
https://wordpress.org/plugins/no-update-nag/

WooCommerce - Skip cart page redirecting to checkout page

Our website is kohsamuitour.net. I have added custom code to skip the cart page on checkout, which works for all sales. This code:
function wc_empty_cart_redirect_url() {
return 'https://www.kohsamuitour.net/all-tours/';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
Now that does the job, but we also have a possibility to check booking availability. That can be found on the pages of private charters, i.e. this one: https://www.kohsamuitour.net/tours/kia-ora-catamaran/ .
Here the customer is being redirected to the cart, where I don't want that to happen as this is not a sale.
How can I make sure the 'Check booking availability' is also redirected to the checkout straight away?
You can skip cart definitively, redirecting customers to checkout page when cart url is called.
To achieve this use this code snippet, that should do the trick:
// Function that skip cart redirecting to checkout
function skip_cart_page_redirection_to_checkout() {
// If is cart page, redirect checkout.
if( is_cart() )
wp_redirect( WC()->cart->get_checkout_url() );
}
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and fully functional.
Edit: Since WooCommerce 3 replace wp_redirect( WC()->cart->get_checkout_url() ); by:
wp_redirect( wc_get_checkout_url() );

Is there any way to hide all admin notification for non admin users in wordpress?

Is there any way to all hide admin notification for non-admin users.
I have used below code but it will hide only update notification but I need to hide all notification
function hide_update_notice_to_all_but_admin_users()
{
if (!current_user_can('update_core')) {
remove_action( 'admin_notices', 'update_nag', 3 );
}
}
add_action( 'admin_head', 'hide_update_notice_to_all_but_admin_users', 1 );
I'm also want to hide these type of notifications.
Try to insert the following code on your functions.php file of your active theme. This code may disable all the updates notifications regarding plugins, themes & WordPress completely.
function remove_core_updates(){
global $wp_version;
return(object) array('last_checked'=> time(),'version_checked'=> $wp_version,);
}
add_filter('pre_site_transient_update_core','remove_core_updates');
add_filter('pre_site_transient_update_plugins','remove_core_updates');
add_filter('pre_site_transient_update_themes','remove_core_updates');
I hope that this code works fine.

Categories