Customize Tax Amount in "Woocommerce_Package_Rates" Hook

Customize Tax amount in woocommerce_package_rates hook

Update: related to tax cost calculation for the shipping methods

There is some little errors on your code and you have missed the tax calculation discount. I have revisited your code a bit, you should try this:

add_filter( 'woocommerce_package_rates', 'conditional_shipping_discount', 10, 2 );
function conditional_shipping_discount( $rates, $packages ) {

$user_id = get_current_user_id();
if ( ! wc_memberships_is_user_active_member( $user_id, 'silver' ) ) return $rates;

$percent = 30; // 30%
$discount = 1 - ($percent / 100);

foreach($rates as $rate_key => $rate_values ) {
// Get original cost
$original_cost = $rates[$rate_id]->cost;
// Calculate the discounted rate cost
$new_cost = $original_cost * $discount;
// Set the discounted rate cost
$rates[$rate_key]->cost = number_format(new_cost, 2);
// calculate the conversion rate (for taxes)
$conversion_rate = $new_cost / $original_cost;

// Taxes rate cost (if enabled)
$taxes = array();
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){ // set the new tax cost
// set the new line tax cost in the taxes array
$taxes[$key] = number_format( $tax * $conversion_rate, 2 );
}
}
// Set the new taxes costs
$rates[$rate_key]->taxes = $taxes
}
return $rates;
}

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.

You should need to refresh the shipping caches:

  1. First this code is already saved on your function.php file.
  2. In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". Then re-enable that Shipping Method and "save". You are done.

Include taxes when editing programmatically WooCommerce taxable shipping rates

Just add a line to set the taxes to zero. You can do it like this:

add_filter('woocommerce_package_rates', 'wc_ninja_change_flat_rates_cost', 10, 2);
function wc_ninja_change_flat_rates_cost( $rates, $package ) {
if ( isset( $rates['flat_rate:1'] ) ) {
$cart_subtotal = WC()->cart->cart_contents_total;
if ( $cart_subtotal >= 60 ) {
$rates['flat_rate:1']->cost = 0;
$rates['flat_rate:1']->label = __('Next Day - Free Delivery', 'woocommerce');
// set the tax amount to zero
$rates['flat_rate:1']->taxes = 0;
}
}
return $rates;
}

You can find more information in this answer: Set specific taxable shipping rate cost to 0 based on cart subtotal in WooCommerce

The code has been tested and works. Add it to your active theme's functions.php.

WooCommerce custom shipping costs for min required subtotal with tax calculations

To enable shipping taxes calculations in your code, use the following revisited code instead:

add_filter( 'woocommerce_package_rates', 'adjust_shipping_rates_cost', 10, 2 );
function adjust_shipping_rates_cost( $rates, $package ) {
$min_subtotal = 30; // Set min subtotal
$cart_subtotal = 0; // Initializing

// Loop through cart items to get items total for the current shipping package
foreach( $package['contents'] as $item ) {
$cart_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
// $cart_subtotal += $item['line_subtotal']; // Or without taxes
}

// Check if the subtotal is greater than specified value
if ( $cart_subtotal < $min_subtotal ) {
return $rates; // Exit
}

// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
$has_taxes = false; // Initializing
$taxes = array(); // Initializing
$new_cost = $initial_cost = $rate->cost; // grab initial cost

if ( $initial_cost == 7.38 ) {
$new_cost -= 2.54;
} elseif ( $initial_cost == 4.10 ) {
$new_cost -= 5;
}
$rates[$rate_key]->cost = $new_cost; // Set new rate cost

// Loop through taxes array (change taxes rate cost if enabled)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
// Get the tax rate conversion
$tax_rate = $tax / $initial_cost;

// Set the new tax cost in the array
$taxes[$key] = $new_cost * $tax_rate;
$has_taxes = true; // Enabling tax changes
}
}
// set array of shipping tax cost
if( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}

Code goes in functions.php file of the active child theme (or active theme). It should work.

Don't forget to empty your cart to refresh shipping cached data.

Change shipping cost for specific countries based on total orders by customer in WooCommerce

Some notes on your code attempt/question:

  • The answer you refer to is about adding a (negative) fee, the woocommerce_package_rates hook is indeed better suited than the woocommerce_cart_calculate_fees hook for your question
  • You can use $package['destination']['country'] to determine the country
  • unset( $rates['flat_rate:15'] ) will not adjust the cost but remove the method
  • To get the total orders by a customer you can use the wc_get_customer_order_count() function

So you get:

function filter_woocommerce_package_rates( $rates, $package ) {
// ONLY for specific countries
$specific_countries = array( 'UK', 'BE' );

// Checks if a value (country) exists in an array, if not return
if ( ! in_array( $package['destination']['country'], $specific_countries ) ) return $rates;

// Only for logged in users
if ( is_user_logged_in() ) {
// Get user ID
$user_id = get_current_user_id();

// Get the total orders by a customer.
$count = wc_get_customer_order_count( $user_id );

// Loop through
foreach ( $rates as $rate_key => $rate ) {
// Initialize
$has_taxes = false;

// Targeting "Flat Rate" shipping method
if ( $rate->method_id == 'flat_rate' ) {
// Get the initial cost
$initial_cost = $new_cost = $rates[$rate_key]->cost;

// Based on order count
if ( $count == 0 ) {
// Set the new rate cost
$new_cost = 6;
} else {
// Set the new rate cost
$new_cost = 25;
}

// Set the new cost
$rates[$rate_key]->cost = $new_cost;

// Taxes rate cost (if enabled)
$taxes = [];

// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax ) {
if ( $rates[$rate_key]->taxes[$key] > 0 ) {
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];

// Get the tax rate conversion
$tax_rate = $initial_tax_cost / $initial_cost;

// Set the new tax cost
$taxes[$key] = $new_cost * $tax_rate;

// Enabling tax
$has_taxes = true;
}
}

// When true
if ( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
}
}

return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates', 10, 2 );

Don't forget to empty your cart to refresh shipping cached data!

Woocommerce Tax not working properlytax

Found A solution. The checkout.js does not work since I forgot to include this

  get_footer()

in my custom template.



Related Topics



Leave a reply



Submit