我创建了一个函数,仅当客户向美国和加拿大发货时,才根据消费金额向购物车中添加自定义费用作为保险。它工作得很好。但我不知道如何使它将此费用添加到发货金额中,而不是创建一个额外的行项目。以下是函数代码:
add_action( 'woocommerce_cart_calculate_fees', 'domestic_insurance_fee', 10, 1 );
function domestic_insurance_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$cou = array( 'US', 'CA' );
// The cart total
$cart_total = $cart->cart_contents_total;
if ($cart_total)
{
if ($cart_total >= 5000)
$prc = 1.25;
elseif ($cart_total >= 4200)
$prc = 1.27;
elseif ($cart_total >= 3200)
$prc = 1.33;
elseif ($cart_total >= 2200)
$prc = 1.37;
elseif ($cart_total >= 1600)
$prc = 1.43;
elseif ($cart_total >= 1100)
$prc = 1.47;
elseif ($cart_total >= 800)
$prc = 1.55;
elseif ($cart_total >= 600)
$prc = 1.65;
elseif ($cart_total >= 400)
$prc = 1.75;
elseif ($cart_total >= 200)
$prc = 1.85;
elseif ($cart_total >= 1)
$prc = 2;
}
else
{
$prc = 2;
}
// The conditional Calculation
$feess = $cart_total * $prc / 100 ;
if ( in_array( WC()->customer->get_shipping_country(), $cou, true ) ) {
$cart->add_fee( __( "Insurance", "woocommerce" ), $feess, false );
} }
所以我试着修改最后一部分,把费用加进去:
// The conditional Calculation
$feess = $cart_total * $prc / 100 ;
$ship_total = $cart->get_cart_shipping_total();
$feeds = $ship_total + $feess;
if ( in_array( WC()->customer->get_shipping_country(), $cou, true ) ) {
$cart->set_shipping_total($feeds);
} }
但这行不通。有什么建议吗?
2条答案
按热度按时间7qhs6swi1#
额外金额“标签必须设置为空值
https://zyrex.eu/jak-dodac-dodatkowa-kwote-w-podsumowaniu-koszyka-w-woocommerce/
3bygqnnd2#
试试看...