你的代码有点过时
woocommerce_cart_calculate_fees
动作钩有
WC_Cart
对象作为可用参数。
global $woocommerce
具有
$woocommerce->cart
$woocommerce->customer
现在简单地替换为:
-
WC()->cart
对象)
-
WC()->customer
(以
WC_Customer
对象)
要使费用仅适用于您的特定条件,请尝试以下重新访问的代码:
add_action( 'woocommerce_cart_calculate_fees', 'conditional_shipping_surcharge', 10, 1 );
function conditional_shipping_surcharge( $cart ) {
$targeted_weight = 100; // Define the starting targeted weight
$cart_weight = $cart->get_cart_contents_weight();
$shipping_state = WC()->customer->get_shipping_state();
$percent = 0; // Initializing
if ( $cart_weight >= $targeted_weight ) {
// 5 ⬠of surcharge per 100kg for Sardinia
if ( in_array( $shipping_state, array('CA', 'NU') ) ) {
$percent = 5;
}
// 3 ⬠of surcharge per 100kg for Sicily
elseif ( in_array( $shipping_state, array('AG', 'CL') ) ) {
$percent = 3;
}
// 2 ⬠of surcharge per 100kg for Calabria
elseif ( in_array( $shipping_state, array('CZ', 'CS') ) ) {
$percent = 2;
}
}
if( $percent > 0 ){
// Fee calculation
$fee = $cart_weight * $percent / 100;
// Add calculated fee (Taxable for "standart" default tax class)
$cart->add_fee( __("Maggiorazione Isole & Calabria", "woocommerce"), $fee, true );
}
}
代码放在活动子主题(或活动主题)的function.php文件中。测试和工作。