wordpress 我怎样才能添加一个自定义费用直接到运费在woocommerce

3j86kqsm  于 2023-03-07  发布在  WordPress
关注(0)|答案(2)|浏览(215)

我创建了一个函数,仅当客户向美国和加拿大发货时,才根据消费金额向购物车中添加自定义费用作为保险。它工作得很好。但我不知道如何使它将此费用添加到发货金额中,而不是创建一个额外的行项目。以下是函数代码:

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);
        
} }

但这行不通。有什么建议吗?

3bygqnnd

3bygqnnd2#

试试看...

$shipping= new WC_Order_Item_Shipping();
$shipping->set_method_title( "Shipping with fees" );
$shipping->set_total( $feeds);
$order->add_item( $shipping);

相关问题