![](/img/trans.png)
[英]Add fee based on payment methods and cart items total in WooCommerce
[英]Add a fee based on cart items height in Woocommerce
我正在尝试找到一个功能,如果其中产品的高度超过2.9厘米,该功能会自动向购物车添加费用。
我正在将Woocommerce用于我们简单的非营利漫画书店。 在瑞典,我们将重量运输作为标准,如果3厘米或3厘米以上的物品,则收取巨额费用。
我已经尝试过修改LoicTheAztec的答案,该答案是基于购物车总重量的费用,但是我真的不知道我在做什么,因为保存代码后我得到了空白页。
我要修改的代码是以下代码:
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 30, 1 );
function shipping_weight_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Convert cart weight in grams
$cart_weight = $cart->get_cart_contents_weight() * 1000;
$fee = 50; // Starting Fee below 500g
// Above 500g we add $10 to the initial fee by steps of 1000g
if( $cart_weight > 1500 ){
for( $i = 1500; $i < $cart_weight; $i += 1000 ){
$fee += 10;
}
}
// Setting the calculated fee based on weight
$cart->add_fee( __( 'Weight shipping fee' ), $fee, false );
}
我对php的经验不只是能够将动作粘贴到我的子主题的functions.php中。
感谢您能提供的任何帮助。
如果任何购物车的高度不超过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文件中。 经过测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.