admin管理员组文章数量:1122832
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;
}
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;
}
Share Improve this question asked Oct 23, 2017 at 16:45 Zach TothZach Toth 12 bronze badges4 Answers
Reset to default 0Solved! 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, '' );
}
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, '' );
}
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
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, '' );
}
}
本文标签: PHPWoocommerce 32 Add variable shipping insurance with multiple quotelse if quot conditions
版权声明:本文标题:PHP - Woocommerce 3.2 Add variable shipping insurance with multiple "else if " conditions 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309455a1934030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论