首页 > 解决方案 > 在 Woocommerce 结帐中动态更新费用

问题描述

基于此代码:根据 Woocommerce 结帐中的单选按钮动态更新费用我试图在 Woocommerce 的结帐页面中根据 3 个条件设置动态的条件百分比费用:

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

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

但它不起作用......我怎样才能让它像链接的答案一样工作?

标签: woocommerce

解决方案


add_action('woocommerce_cart_calculate_fees', 'add_ship_fee');

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

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


推荐阅读