在Woocommerce结帐中动态更新费用

时间:2018-06-30 14:01:18

标签: woocommerce

基于此代码:Update fee dynamically based on radio buttons in Woocommerce checkout,我试图基于以下三种条件在Woocommerce的结帐页面上收取动态的有条件百分比费用:

这是我的实际自定义代码:

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

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

$percentage3 = 0.01;
$fee3 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage3;
$woocommerce->cart->add_fee( 'Fee 3', $fee3, true, '' );

$percentage2 = 0.02;
$fee2 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage2;
$woocommerce->cart->add_fee( 'Fee 2', $fee2, true, '' );

$percentage1 = 0.03;
$fee1 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage1;
$woocommerce->cart->add_fee( 'Fee 1', $fee1, true, '' );
}

但是它不起作用……如何使其像链接的答案中那样起作用?

1 个答案:

答案 0 :(得分:0)

add_action('woocommerce_cart_calculate_fees', 'add_ship_fee');

function add_ship_fee() {
      $custom_shippingcost = 10;
      WC()->cart->add_fee('VAT: ', $custom_shippingcost);
}

您可以在购物车和结帐时动态添加此费用。您还可以通过获取购物车的费用并添加购物车总费用的一定百分比来更改变量的费用。