我正在开发一个食品订购woocommerce商店 . 我正在尝试添加输入字段或分组按钮,以便客户添加交付提示以结帐总额 .
后端部分:我写了一个动作,根据给定的百分比添加结账提示(假设总订单的10%)
add_action( 'woocommerce_cart_calculate_fees', 'calculateTipsPercentage', 10, 1 );
function calculateTipsPercentage( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$total_tax = 0;
$carttotal= 0;
// Get the unformated taxes array
$taxes = $cart->get_taxes();
//get Tax amount from taxes array
foreach($taxes as $tax) $total_tax += $tax;
global $woocommerce;
$percentage = 0.10;//percentage of tips (must be entered by customer)
$carttotalamount = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ); //Cart total without tax
$totalorderwithTax = ( $carttotalamount + $total_tax); //cart total with tax
$extrafee= round( $totalorderwithTax * $percentage, 2 ); //extra fee amount
$percetagetoShow = ( $percentage * 100 );
$woocommerce->cart->add_fee( "Tip ({$percetagetoShow}%)", $extrafee, true, '' );
}
我的问题在于前端部分 .
如何添加任何类型的输入字段和一个按钮(添加结帐提示),让客户添加百分比,然后单击此按钮将触发上面的操作 . 或者如果我可以通过ajax / jquery没有按钮(没有刷新页面)这样做会更好 .
任何帮助,将不胜感激 .