如果任何购物车商品的高度不超过 3 厘米,以下代码将添加特定费用(Woocommerce 中的尺寸单位设置需要位于cm):
add_action( 'woocommerce_cart_calculate_fees', 'shipping_height_fee', 10, 1 );
function shipping_height_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Your settings (here below)
$height = 3; // The defined height in cm (equal or over)
$fee = 50; // The fee amount
$found = false; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_height() >= $height ) {
$found = true;
break; // Stop the loop
}
}
// Add the fee
if( $found ) {
$cart->add_fee( __( 'Height shipping fee' ), $fee, false );
}
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
添加:基于购物车物品总高度的代码:
add_action( 'woocommerce_cart_calculate_fees', 'shipping_height_fee', 10, 1 );
function shipping_height_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Your settings (here below)
$target_height = 3; // The defined height in cm (equal or over)
$total_height = 0; // Initializing
$fee = 50; // The fee amount
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
$total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
}
// Add the fee
if( $total_height >= $target_height ) {
$cart->add_fee( __( 'Height shipping fee' ), $fee, false );
}
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。