这是我在这里的第一篇帖子,我对PHP非常陌生,对我来说非常简单。
我需要根据购物车总数为优先邮件添加USPS的可变运费。我尝试过插件,但没有一个能与USPS shipping插件客户端坚持使用的插件一起正常工作,而且我已经将购买任何其他插件的预算增加到了最大。
我创建了一个子主题并添加了一个新函数。php。这是添加防更新php的正确方法正确吗?
我在Git上找到了一个php函数,它根据购物车的超量/不足量添加一个费率,但如果条件是7个可变购物车总数,我还需要添加更多。下面是我用有限的php知识添加所需条件的最佳尝试。有人能帮我把这个代码写出来吗?我遗漏了什么以允许多个其他if条件?
add_action( \'woocommerce_cart_calculate_fees\',\'woocommerce_custom_surcharge\' );
函数woocommerce\\u custom\\u association(){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;
}
SO网友:Rogers Sampaio
因此,如果在每个if语句中都返回空,则函数将永远无法访问:
$woocommerce->cart->add_fee( \'Insurance\', $insurance_fee, true, \'\' );
看看我在下面所做的更改,我还认为如果发货方式不是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, \'\' );
}