When customer click cancel subscription, they still can logged in the my account section, but I want to let user cannot access anything on my account section. From this point, I would like to make the function on "if the user cancels payment, it also delete the account too" to make them cannot access, so I try to investigate and write the code below.
add_action( 'woocommerce_order_status_cancelled',
'custom_woocommerce_auto_delete_user' );
function custom_woocommerce_auto_delete_user( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ( !$order_id )
return false;
if ('cancelled' == $order_status) {
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID,true );
return true;
}
return false;
}
However, I'm just the beginner of woocommerce and I don't know which solution is the best for this issue. I'm so glad for anyone that come to response to me :)
I want to delete the users who are created when trying to buy a subscription but the payment gets failed although the user anyhow creates. I am just to confirm if this code works for Me or not?
add_action('woocommerce_subscription_status_failed', 'custom_woocommerce_subscription_status_failed');
function custom_woocommerce_subscription_status_failed($order_id)
{
global $woocommerce;
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ($order_status`enter code here` == 'failed') {
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID,true );
return true;
}
return false;
}
Related
How to change in woocommerce order status from processing to completed
?
I found snippet, but it only changes status if you go to thank you page, but if my customer decides just to close paypal page and don't go to thank you page ?
Then it is still processing, tested it already. I need automatically to detect processing status and change it to processing.
This will check for when the order changes status. If the status changes to processing, it will set the status to completed. Note that it does go through processing, so the user will probably get two emails back to back. You can disable one of the emails in the WC settings dashboard.
function wc_status_change($order_id,$old_status,$new_status) {
$order = new WC_Order( $order_id );
if($order->status == 'processing'){
$order->update_status('completed');
}
}
In your case you have two options the first may not work as it is related to previous versions of woocommerce but the second one should work
add the code to your functions.php
1º
add_filter( 'woocommerce_payment_complete_order_status', 'update_order_status_woo', 10, 2 );
function update_order_status_woo( $order_status, $order_id ) {
$order = new WC_Order( $order_id );
if ( $order_status == 'processing' && ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {
return 'completed';
}
return $order_status;
}
2º
add_action('woocommerce_order_status_changed', 'auto_update_processing_to_complete');
function auto_update_processing_to_complete($order_id)
{
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
if ($order->data['status'] == 'processing') {
$order->update_status( 'completed' );
}
}
I want to get the "mycred" balance of a customer through the order while using WP ALL Export to export the customer balance based on orders to a spreadsheet. It's actually probably quite simple. I'm able to get the Order ID, but not the Customer ID
Here is what I'm doing to test if I can get the customer ID:
function get_customeruserid($value)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_order_number();
$customer = new WC_Customer($post->ID);
$user_id = $customer->get_ID();
$value = $user_id;
return $value;
}
This returns a 0.
However, I can get the order number easily enough by doing this:
function get_customerorderid($value)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_order_number();
$value = $order_id;
return $value;
}
This returns the customer's order number which is great, but only half the battle. I now want the Customer ID so I call call the mycred balance function to show their balance.
Any ideas? I'm a newbie at php and probably very bad.
To get the User ID from the Order ID, you can use many ways, Here are 2 of them:
In WooCommerce 3.0+ you can use WC_Order Class methods this way:
function get_customerorderid(){
global $order, $post;
if( ! is_a($order, 'WC_Order') ) {
$order_id = $post->ID;
// Get an instance of the WC_Order object
$order = wc_get_order($order_id);
} else {
$order_id = $order->id;
}
// Get the user ID from WC_Order methods
$user_id = $order->get_user_id(); // or $order->get_customer_id();
return $user_id;
}
Before WooCommerce 3.0 version, you can use get_post_meta() function this way:
function get_customerorderid(){
global $order, $post;
if( ! is_a($order, 'WC_Order') ) {
$order_id = $post->ID;
} else {
$order_id = $order->id;
}
// Get the user ID
$user_id = get_post_meta($order_id, '_customer_user', true);
return $user_id;
}
For those who want to specifically add the customer mycred balance from an ORDER into the CSV sheet within WP All Export here is the bit of code I used.
Thank you for your help getting it solved.
While editing an ORDER export in WP ALL EXPORT, add a new data object and click on it and "Export the value returned by a PHP function" then add the following function in the code editor:
function all_export_mycred($balance)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$user_id = $order->get_user_id( );
$balance = mycred_get_users_balance( $user_id );
return $balance;
}
Then make sure to add the "all_export_mycred" to the php return field.
I have a user role for wholesale customers (wholesale_customer). When I mark an order as completed a notification is send to customers. This is ok for my regular customers but I would like to disable/remove the notification for wholesale customers.
What I've got so far:
function do_not_send_some_email_notifications(WC_Emails $wc_emails) {
$order = new WC_Order( $order_id );
if ( $order->user_id > 0 ) {
$user_id = $order->user_id;
$get_user_data = get_userdata($user_id);
$user_roles = $get_user_data->roles;
if (in_array('wholesale_customer', $user_roles)) {
remove_action('woocommerce_order_status_completed_notification', array($wc_emails->emails['WC_Email_Customer_Completed_Order'], 'trigger'));
}
}
}
add_action('woocommerce_email', 'do_not_send_some_email_notifications');
I have tested this but it's not working.
It would be very nice if someone can point me in the right direction.
Thanks.
Updated 2: I finally find the right hook to make it work. I have revisited your code a little bit using a very similar custom function hooked in the woocommerce_order_status_completed action hook.
Here is the code:
function custom_conditional_email_notifications( $order_id ) {
// Set HERE the targetted user role
$targeted_user_role = 'wholesale_customer';
// Get the order object, the user ID, and the user role.
$order = wc_get_order($order_id);
$user_id = $order->get_user_id();
$user_info = get_userdata($user_id);
if ( in_array( $targeted_user_role, $user_info->roles ) && $user_id > 0 )
remove_action( 'woocommerce_order_status_completed_notification', array(
$wc_emails->emails['WC_Email_Customer_Completed_Order'],
'trigger'
) );
}
add_action( 'woocommerce_order_status_completed', 'custom_conditional_email_notifications' );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Alright, I've been pulling my hair out the last couple of days trying to figure this out.
I have a wholesale plugin in wordpress install with woocommerce. It gives the user "wholesale_customer" special rates over everyone else. I want to be able to offer local delivery to only the "wholesale_customer" user role but can't seem to figure out how to do it.
I've gotten this code from #mcorkum but it's still not working.
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
I know this achievable with a plugin, but I'd rather not use plugins or pay for it for that matter.
Does anyone see anything that I'm not seeing?
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
if ( isset( $available_methods['local_delivery'] ) ) {
if ( !current_user_can( 'wholesale_customer' ) ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
Try the above code by pasting it in your theme's functions.php file. And let me know if this worked for you.
I'm not a wordpress dev, but that code doesn't look like it is providing a user with role "wholesale_customer" the "local_delivery" option. On the contrary in fact, it looks to be removing the local delivery option if the user role IS "wholesale_customer":
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
If I was to take this code simply at face value (As I am not a wordpress dev) I would re-write this function to be easier to understand and read:
function wholesale_local_delivery($available_methods)
{
global $woocommerce;
global $current_user;
// Return early if no local delivery option is available
if (!isset($available_methods['local_delivery'])) {
return $available_methods;
}
// Determine if the user has a user role of wholesale customer
$hasRoleWholeSaleCustomer = false;
foreach ($current_user->roles as $role) {
if ($role === 'wholesale_customer') {
$hasRoleWholeSaleCustomer = true;
break;
}
}
// If the user does not have the role wholesale customer
// And for the code here to be being processed the local delivery
// option must be available
if (!$hasRoleWholeSaleCustomer) {
unset($available_methods['local_delivery']);
}
// Return the available methods applicable to the users roles
return $available_methods;
}
Hope someone else with experience in woocomerce, can give a better answer. But in the meantime, you can try this re-write and see if it works for you.
Goodluck.
Hey guys I have a cash on delivery payment method on my wordpress/woocomerce website that I want to hide from the customer user role and non-logged in users.
I've been searching up and down and the only thing I found close was this bit of code.
function paypal_disable_manager( $available_gateways )
{global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && current_user_can('customer') ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways','paypal_disable_manager' );
Would someone be able to help me modify this code to make it work for my use. Thank you in advance!
Have mention the code which is tried and tested for you. It works well. Lemme know if the same works for you too.
function wdm_disable_cod( $available_gateways ) {
//check whether the avaiable payment gateways have Cash on delivery and user is not logged in or he is a user with role customer
if ( isset($available_gateways['cod']) && (current_user_can('customer') || ! is_user_logged_in()) ) {
//remove the cash on delivery payment gateway from the available gateways.
unset($available_gateways['cod']);
}
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'wdm_disable_cod', 99, 1);
<?php
//--- Filter for remove any payment gateway as per the user role selected --
add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
global $woocommerce, $current_user;
if ( is_user_logged_in() ) {
$userRole = implode(',',$current_user->roles);
if($userRole == 'my_user_role'){
//-- Remove casho on delivery if following user have logged in
unset($gateways['cod']);
}
}else{
//-- Hide COD if user not logged in
unset($gateways['cod']);
}
return $gateways;
}
?>
//-- Try this one, I have already make use of this code against minimum order limit