woocommerce - 在 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, '' );
}
但它不起作用......我怎样才能让它像链接的答案一样工作?
解决方案
add_action('woocommerce_cart_calculate_fees', 'add_ship_fee');
function add_ship_fee() {
$custom_shippingcost = 10;
WC()->cart->add_fee('VAT: ', $custom_shippingcost);
}
您可以在购物车和结帐时动态添加此费用。您还可以通过获取购物车成本并添加购物车总成本的一些百分比来更改变量成本。
推荐阅读
- python - 如何从两个制表符分隔的文件中获取枢轴线?
- ms-access - 该文件将不会打开与它关联的超链接字段
- python - 从 np.array() 1d 中删除项目
- android - 颤振深度链接(uni_links)在真实设备上不起作用
- sapui5 - 使用 SAPUI5 时在 XML 视图中的何处添加 Shell Bar 控件?
- c# - 将元数据/注释传递给查看模型 .Net Core Razor
- javascript - 反应从谷歌地图获取地址?#反应
- algorithm - 反向 0/1 背包:表中的这一行怎么可能?
- reactjs - React - 转换字符串以使用导入的组件
- javascript - 关于 JavaScript 对象的方法不可枚举的困惑