I am trying to change in shipping cost (package rate) depending on user role.
How to manage shipping costs from the total price without taxes?
There is a wholesale user type(wholesale_customer) and then there are the rest of the user roles(Let's call them "The Rest") that have the same rules for everyone.
Shipping Cost Logic:
For wholesale customers:
For regular customers:
You need this calculation to be made based on the subtotal without adding taxes.
Tried bellow code :
// This is the way I try to manage shipping costs for "The rest" user
add_filter('woocommerce_package_rates', 'shipping_costs_for_retailers', 10, 2);
function shipping_costs_for_retailers($rates, $package) {
$user = wp_get_current_user();
$user_roles = $user->roles;
$subtotal = WC()->cart->subtotal;
if (!in_array('wholesale_customer', $user_roles) && $subtotal < 30) {
$new_rate = array();
foreach ($rates as $rate_id => $rate) {
$new_rate[$rate_id] = $rate;
$new_rate[$rate_id]->cost += 5;
}
return $new_rate;
}
return $rates;
}
add_filter('woocommerce_cart_totals_shipping_html', 'message_for_retailers');
function message_for_retailers($html) {
$user = wp_get_current_user();
$user_roles = $user->roles;
$subtotal = WC()->cart->subtotal;
if (!in_array('wholesale_customer', $user_roles) && $subtotal < 30) {
$html .= '<p>Shipping cost: $5.00</p>';
}
return $html;
}
// This is the way I try to manage shipping costs for wholesale users
add_filter('woocommerce_package_rates', 'shipping_ expenses_ for_wholesalers', 10, 2);
function shipping_ expenses_ for_wholesalers($rates, $package) {
$user = wp_get_current_user();
$user_roles = $user->roles;
$subtotal = WC()->cart->subtotal;
if (in_array('wholesale_customer', $user_roles) && $subtotal < 70) {
$new_rate = array();
foreach ($rates as $rate_id => $rate) {
$new_rate[$rate_id] = $rate;
$new_rate[$rate_id]->cost += 10;
}
return $new_rate;
}
return $rates;
}
add_filter('woocommerce_cart_totals_shipping_html', 'message_for_wholesalers');
function message_for_wholesalers($html) {
$user = wp_get_current_user();
$user_roles = $user->roles;
$subtotal = WC()->cart->subtotal;
if (in_array('wholesale_customer', $user_roles) && $subtotal < 70) {
$html .= '<p>Shipping cost: $10.00</p>';
}
return $html;
}
What should I do to correct this?
How should I modify my function to manage the price without the tax?
You can see a screenshot of what happens to me, when a wholesale customer, in the SubTotal does not reach €70, but the shipping fee is deactivated since the total exceeds €70
Before my answer I clarify some points about your code , you are adding +10 to all rates that is added in car for example if already 20 is added in cart so now its 30 and if there is second shipping tax of 15 with it so that will be 25 and so on so 10 is adding in all tax , but you said in question that :: "the shipping costs would be €10" , So for that we add one flat rate shipping method with zero(0) charge for all orders and we add 10 charge where we need by code , I'm giving all steps bellow
Step : 1 , step : 2 , step : 3 , step : 4 , step : 5 , step : 6 , step : 7 , step : 8
<?php
add_filter( 'woocommerce_package_rates', 'custom_shipping_costs_based_on_user_role', 10, 2 );
function custom_shipping_costs_based_on_user_role( $rates, $package ) {
// Get the current user role
$current_user = wp_get_current_user();
$user_role = $current_user->roles[0];
// Get the order subtotal without taxes
$subtotal = WC()->cart->subtotal_ex_tax;
if(isset($rates['flat_rate:3'])) {
// Set the shipping costs based on the user role and subtotal
if ( $user_role == 'wholesale_customer' ) {
if ( $subtotal >= 70 ) {
// Free shipping
unset( $rates['flat_rate:3'] ); // remove standard shipping rate
} else {
// Add €10 shipping cost
$rates['flat_rate:3']->cost = 10;
}
} else {
if ( $subtotal >= 30 ) {
// Free shipping
unset( $rates['flat_rate:3'] ); // remove standard shipping rate
} else {
// Add €10 shipping cost
$rates['flat_rate:3']->cost = 5;
}
}
}
return $rates;
}