wordpress Woocommerce:确认海关费用为“运输”特定费用

zqdjd7g9  于 2023-01-20  发布在  WordPress
关注(0)|答案(1)|浏览(100)

我知道我可以使用下面的代码向购物车添加费用,但它只是添加了一个通用的费用到购物车。Woocommerce认识到航运特定的费用,我想知道是否有一种方法,使费用通过下面的方法添加的东西,Woocommerce视为“航运”特定的费用。某种元数据,得到添加到航运特定的费用。我们的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, '' );

}
8xiog9wr

8xiog9wr1#

你可以这样做:

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/

相关问题