I am trying to add shipping 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.
The rules are as follows for Wholesale users: Shipping is free if they reach the amount of €70, if they do not reach that amount, the shipping costs would be €10, which are added to the total
The rules for "The Rest" are that shipping is free if they exceed the amount of €30, if this amount is not exceeded, the shipping costs would be €10
I get this by checking the user role with current_user_can()
For users who are not a Wholesaler, ("The Rest"), everything works fine with the following function, since the price of the products for this type of user does not carry taxes. Can functions:
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;
}
The problem comes with users with the role of wholesalers, since a tax is added to the price of the products. So, there is a small margin that gives me error. For example, if the amount is €68.40, the shipping charges are disabled, since when adding taxes, the total sum exceeds €70, which is the limit amount for free shipping for these users. In other words, I need this calculation to be made based on the subtotal without adding taxes.
For this type of users, I have used the same system as for "The Rest" of users, I just tried to modify the variable WC( )->cart->subtotal
, but I don't know how to do it, I have really looked for ideas and solutions, but I can't find the way.
This is the way I try to manage shipping costs in 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
Thank you