0

This is my first post here, I am very new to PHP so bare with me.

I need to add USPS's variable shipping rates for priority mail based on cart total. I have tried plugins but none of them work right with the USPS shipping plugin client insists on using and I have maxed budget for buying any other plugins.

I have created a child theme and added a new functions.php. That is the correct way of adding update-proof php correct?

I found a php function on Git that adds a rate based on an over/under cart amount but I need to add more else if conditions for 7 variable cart totals. Below is my best attempt at adding the conditions I need with my very limited knowledge of php. Can someone please help me get this code working? What am I missing to allow for multiple else if conditions?

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );

function woocommerce_custom_surcharge() { global $woocommerce;

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];

if ( strpos($chosen_shipping, 'USPS_Simple_Shipping_Method' ) !== false ) {
    // this compare needed since if the string is found at beg of target, it returns '0', which is a false value

    $insurance_fee = 0;
    if ( $woocommerce->cart->cart_contents_total  <= 50 ) {
        $insurance_fee = 0;
        return;
    } else {
        if ( $woocommerce->cart->cart_contents_total  > 50  ) { 
            $insurance_fee = 2.05; 
            return; 
    } else {
         if ( $woocommerce->cart->cart_contents_total  > 100  ) { 
            $insurance_fee = 2.45; 
            return; 
    } else {
         if ( $woocommerce->cart->cart_contents_total  > 200  ) { 
            $insurance_fee = 4.60; 
            return;
    } else {
         if ( $woocommerce->cart->cart_contents_total  > 300  ) { 
            $insurance_fee = 5.50; 
            return;
    } else {
         if ( $woocommerce->cart->cart_contents_total  > 400  ) { 
            $insurance_fee = 6.40; 
            return;
    } else {
         if ( $woocommerce->cart->cart_contents_total  > 500  ) { 
            $insurance_fee = 7.30; 
            return;
    }

    $woocommerce->cart->add_fee( 'Insurance', $insurance_fee, true, '' );
}
return;

}

4 Answers 4

0

Solved! I had the statements in the wrong order, the first if statement was satisfied so none of the others were checked. Reversed them and voila! I also could have added a <= to each statement

//General Fall back for shipping and cart totals <= 50
$insurance_fee = 0;
$chosen_methods = WC()->session->get( 'USPS_Simple' );
$chosen_shipping = $chosen_methods[0]; {

    if ( $woocommerce->cart->cart_contents_total  > 500  ) { 
            $insurance_fee = 7.30; 
    } else if ( $woocommerce->cart->cart_contents_total  > 400  ) { 
            $insurance_fee = 6.40; 
    } else if ( $woocommerce->cart->cart_contents_total  > 300  ) { 
            $insurance_fee = 5.50; 
    } else if ( $woocommerce->cart->cart_contents_total  > 200  ) { 
            $insurance_fee = 4.60; 
    } else if ( $woocommerce->cart->cart_contents_total  > 100  ) { 
            $insurance_fee = 2.45; 
    } else if ( $woocommerce->cart->cart_contents_total  >= 50  ) { 
            $insurance_fee = 2.05; 
    }
}

//The fallback $insurance_fee value of 0 will be used if none of the conditions are met
$woocommerce->cart->add_fee( 'Shipping Insurance', $insurance_fee, true, '' );

}

0

So, you are returning empty in every if statement, the function will never get to the:

$woocommerce->cart->add_fee( 'Insurance', $insurance_fee, true, '' );

Take a look at the changes I've made below, I also think you should add a fallback if the shipping method is not USPS.

function woocommerce_custom_surcharge() { 
      global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    //General Fall back for shipping and cart totals <= 50
    $insurance_fee = 0;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];

    if ( strpos($chosen_shipping, 'USPS_Simple_Shipping_Method' ) !== false ) {

        if ( $woocommerce->cart->cart_contents_total  > 500  ) { 
                $insurance_fee = 7.30; 
        } else if ( $woocommerce->cart->cart_contents_total  > 400  ) { 
                $insurance_fee = 6.40; 
        } else if ( $woocommerce->cart->cart_contents_total  > 300  ) { 
                $insurance_fee = 5.50; 
        } else if ( $woocommerce->cart->cart_contents_total  > 200  ) { 
                $insurance_fee = 4.60; 
        } else if ( $woocommerce->cart->cart_contents_total  > 100  ) { 
                $insurance_fee = 2.45; 
        } else if ( $woocommerce->cart->cart_contents_total  > 50  ) { 
                $insurance_fee = 2.05; 
        }
    }

    //The fallback $insurance_fee value of 0 will be used if none of the conditions are met
    $woocommerce->cart->add_fee( 'Insurance', $insurance_fee, true, '' );
}
5
  • THANKS! I had to remove the strpos compare to get it to work. Hopefully that doesnt cause any issues. But now I am only returning the first if condition even when the else if conditions are met. Hmmm
    – Zach Toth
    Oct 23, 2017 at 23:34
  • I will give it a try. Oct 23, 2017 at 23:35
  • It works just fine, on my side. Can you print $chosen_shipping with var_dump($chosen_shipping)? Oct 24, 2017 at 0:20
  • I just needed to reverse the order of the statements. First if statement was met so others werent checked. Could have also added <= to every statement
    – Zach Toth
    Oct 24, 2017 at 0:22
  • Understood! I change the answer to match your needs, please accept it as the final answer. Oct 24, 2017 at 0:42
0

For anyone that stumbles on this and wants to implement it I have posted the code as a gist where I keep it updated. I recently added an if statement to turn shipping insurance off if local pick up is selected

https://gist.github.com/Zach-Toth/95041d32b072109fa8fd99c60eff1c5a

0

Thank you for that, it helped me get started. I've made a small modification so that it works with percentages instead, which is much better, especially if your site deals with large amounts:

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() { 
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // General Fall back for shipping and cart totals <= 50
    $insurance_percentage = 0;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0]; {
      
       if ( $chosen_shipping == 'local_pickup:3' ) { 
            $insurance_percentage = 0; 
       } else {
            // Adjust the percentage value as needed, e.g., 2% would be 0.02
            $insurance_percentage = 0.025; // 2.5% insurance fee
       }
    }

    // Calculate the fee based on the cart total and the percentage
    $cart_total = $woocommerce->cart->cart_contents_total;
    $insurance_fee = $cart_total * $insurance_percentage;

    // The fee will be added if the cart total is above or equal to 100
    if ( $cart_total >= 100 ) {
        $woocommerce->cart->add_fee( 'Shipping Insurance', $insurance_fee, true, '' );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.