I created a function to add a custom fee to the cart as insurance based on the amount spent only if the customer is shipping to the US and Canada. It works great. But I cannot figure out how to make it add this fee to the shipping amount instead of creating an additional line item.
Here’s the functioning code:
add_action( 'woocommerce_cart_calculate_fees', 'domestic_insurance_fee', 10, 1 );
function domestic_insurance_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$cou = array( 'US', 'CA' );
// The cart total
$cart_total = $cart->cart_contents_total;
if ($cart_total)
{
if ($cart_total >= 5000)
$prc = 1.25;
elseif ($cart_total >= 4200)
$prc = 1.27;
elseif ($cart_total >= 3200)
$prc = 1.33;
elseif ($cart_total >= 2200)
$prc = 1.37;
elseif ($cart_total >= 1600)
$prc = 1.43;
elseif ($cart_total >= 1100)
$prc = 1.47;
elseif ($cart_total >= 800)
$prc = 1.55;
elseif ($cart_total >= 600)
$prc = 1.65;
elseif ($cart_total >= 400)
$prc = 1.75;
elseif ($cart_total >= 200)
$prc = 1.85;
elseif ($cart_total >= 1)
$prc = 2;
}
else
{
$prc = 2;
}
// The conditional Calculation
$feess = $cart_total * $prc / 100 ;
if ( in_array( WC()->customer->get_shipping_country(), $cou, true ) ) {
$cart->add_fee( __( "Insurance", "woocommerce" ), $feess, false );
} }
So I tried changing the last part that adds the fee to this:
// The conditional Calculation
$feess = $cart_total * $prc / 100 ;
$ship_total = $cart->get_cart_shipping_total();
$feeds = $ship_total + $feess;
if ( in_array( WC()->customer->get_shipping_country(), $cou, true ) ) {
$cart->set_shipping_total($feeds);
} }
But this does not work. Any suggestions?
2
Answers
the extra amount label must be set to a blank value
https://zyrex.eu/jak-dodac-dodatkowa-kwote-w-podsumowaniu-koszyka-w-woocommerce/
Try it…