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