Woocommerce:将自定义费用识别为 "shipping" 特定费用

Woocommerce: recognize custom fee as "shipping" specific charge

我知道我可以使用以下代码向购物车添加费用,但它只是向购物车添加一般费用。 Woocommerce 识别运输特定费用,我想知道是否有一种方法可以通过以下方法增加费用,woocommerce 将其视为 "shipping" 特定费用。添加到运输特定费用中的某种元数据。我们的 API 没有将这些费用识别为运费,因此没有记录它们,这就是我要解决的问题。

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 0.01;
    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
    $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}

你可以这样做:

add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates', 10, 2 );
function woocommerce_package_rates( $rates, $package ) {

    $percentage = 0.01;

    foreach($rates as $key => $rate ) {
        $surcharge = ( wc()->cart->cart_contents_total + $rates[$key]->cost ) * $percentage;
        $rates[$key]->label .= ": {$rates[$key]->cost} + Fee: {$surcharge}";
        $rates[$key]->cost += $surcharge;
    }
    return $rates;
}

将附加费添加到运费中。上面的代码将产生如下内容:

更多关于运费的信息:http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/