I need to add two different shipping rates, depending on the role of the user making the purchase. I have a role that is 'wholesale_customer' and should not pay shipping costs. However, the purchase must be prevented and a message added at Checkout if the subtotal of the cart is equal to or less than €90. (remember that you must reach the amount of €90)
The other option is all other WordPress roles, who are required to pay €5 shipping if their cart subtotal is €40 or less.
I have configured shipping in WooCommerce, I have created two free shipping methods with a minimum order amount, one of €40 and another of €90.
The problem is that the user with the 'wholesale_customer' role, when he reaches the amount of €40, is also enabled for that free shipping method, which should not happen, since for this user role ('wholesale_customer ' ) shipping is free, if the purchase exceeds the minimum of €90.
I have configured the shipments in WooCommerce in the following way, creating 2 Shipping Methods: - one has "Minimum Quantity Required" at €90
another has "Minimum Quantity Required" at €40
and one of shipping costs at €0.
I have tried to add the following function to add to get what I need, but all the shipping methods are always enabled, so the retail shipping method of "Minimum €40 for free shipping", is also activated for the user with the role wholesale_customer' This should not happen, as users with this role would benefit from benefits that are not theirs.
I'm showing some of the code I've used to try to do what I'm looking for, since I did a lot of testing. Only I have not had the way to add the text that I mention in the presentation for the role 'wholesale_customer' Show images of WooCommerce setup
function custom_shipping_methods_visibility( $available_methods ) {
$subtotal = WC()->cart->subtotal;
$user = wp_get_current_user();
$user_roles = $user->roles;
// Check user role and cart subtotal to show/hide shipping methods
if ( in_array( 'wholesale_customer', $user_roles ) && $subtotal <= 70 ) {
unset( $available_methods['flat_rate:13'] );
} elseif ( $subtotal <= 30 ) {
unset( $available_methods['flat_rate:9'] );
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'custom_shipping_methods_visibility', 10, 1 );
////////////////////
// Adds a custom function to filter shipping methods based on cart role and subtotal
function custom_shipping_methods( $available_methods ) {
// Get cart subtotal
$subtotal = WC()->cart->subtotal;
// Gets the role of the current user
$user = wp_get_current_user();
$user_roles = $user->roles;
//Check user role and cart subtotal to adjust shipping methods
if ( in_array( 'wholesale_customer', $user_roles ) && $subtotal < 70 ) {
foreach ( $available_methods as $method_id => $method ) {
// Hide the shipping methods if the user is a wholesaler and the subtotal is less than €70
unset( $available_methods[ $method_id ] );
}
// Show a warning message
wc_add_notice( 'Debes alcanzar un mínimo de 70€ en tu carrito para realizar el pedido.', 'error' );
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 10, 1 );
config-1
Config-2 :
Config-3 :