Woocommerce:将定制费用视为“运费”特定费用

时间:2018-04-04 02:40:48

标签: woocommerce shipping fee

我知道我可以使用以下代码向购物车添加费用,但它只是为购物车添加了一般费用。 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, '' );

}

1 个答案:

答案 0 :(得分:1)

您可以这样做:

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

将附加费加到运费上。上面的代码会产生如下代码:

reigelgallarde.me

有关运费的更多信息,请访问:http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/