0

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

enter image description here

gemita
  • 2,686
  • 2
  • 10
  • 17

1 Answers1

2

Your question is long and complicated so first I point out things that I understand and use it to answer your question :

  1. You are trying to add shipping depending on user role.
  2. 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.
  3. 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.
  4. 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 €5
  5. You need this calculation to be made based on the subtotal without adding taxes.

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;
}

If this is not the way you think then let me know , don't mix it with your question code , first remove them and then use it

Note : cart doesn't reflect changes on page reload, it reflect changes on fragment change , so after any change in code or setting , change in product quantity or add remove any product so you can see changes if that happens

Yash
  • 1,020
  • 1
  • 15
  • Hi, friend @#Yash A configuration change for shipping costs and user roles has come up and I'm stuck. I finally asked a question to try to get someone to help me, can you review the question and tell me if I have a way to do what I'm looking for? Of course if you can help me, when the time comes I will create a nice bounty to reward your help. Thank you [The question](https://stackoverflow.com/questions/76502094/how-to-adapt-the-shipping-costs-according-to-the-role-of-the-user-in-woocommerce) – gemita Jun 18 '23 at 18:57
  • 1
    my bad, I was on a break :) – Yash Jun 27 '23 at 18:58
  • 1
    Hi @Yash. Don't worry, I asked you for help because you helped me with something similar. The answer I accepted was working on a test website I have. I moved that code to the real store and did not test. After several days, I have received messages from Wholesale customers saying that their orders did not arrive, and they did it correctly, without error messages. But the orders are not coming to WooCommerce. I already left a comment to the author of the answer to see if he has any ideas. You can review the answer code if you want, I can still create a new bounty for another answer. – gemita Jun 28 '23 at 11:19