选择付款方式(信用卡)时如何添加总金额的百分比?
\n\n例如:如果客户支付的是货到付款,那么是底价,如果我选择网上支付,那么我收取的百分比是加到总金额中的吗?
\n\n货到付款网关ID设置:cod\n网上支付网关ID设置:tinkoff
\n\nadd_action( \'woocommerce_after_calculate_totals\', \'custom_fee_for_tinkoff\' );\n\nfunction custom_fee_for_tinkoff( $cart ) {\n\n if ( is_checkout() || defined(\'WOOCOMMERCE_CHECKOUT\') ) {\n\n $payment_method = WC()->session->get( \'chosen_payment_method\' );\n\n if ($payment_method == \'tinkoff\') {\n $percentage = 0.025;\n $surcharge = ( $cart->cart_contents_total + $cart->tax_total ) * $percentage;\n\n $cart->add_fee( \'\xd0\x9a\xd0\xbe\xd0\xbc\xd0\xb8\xd1\x81\xd1\x81\xd0\xb8\xd1\x8f \xd0\xb1\xd0\xb0\xd0\xbd\xd0\xba\xd0\xb0\', $surcharge, true, \'\' );\n }\n else { return; }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n 我试图为functions.php 编写一个代码片段,当同时选择角色“订阅者”和付款方式“信用卡”时,该代码片段会对购物车总额应用2% 的折扣。到目前为止我的进展
function discount_when_role_and_payment(/* magic */) {
global $woocommerce;
if ( /* credit-card selected */ && current_user_can('subscriber') ) {
/* get woocommerce cart totals, apply 2% discount and return*/
}
return /*cart totals after discount*/;
}
add_filter( '/* magic */', 'discount_when_role_and_payment' );
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙完成这个吗?或者至少指向正确的方向?
当客户可以下订单免运费,但想选择 COD 付款时,我需要申请额外费用。所以,免费送货 + COD 付款 => 费用。
我尝试了以下代码未成功。我哪里错了?
add_action( 'woocommerce_cart_calculate_fees','cod_fee' );
function cod_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_gateway = WC()->session->chosen_payment_method;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
$fee = 19;
if ( $chosen_shipping == 'free_shipping' && $chosen_gateway == 'cod' ) {
WC()->cart->add_fee( 'Spese per pagamento alla consegna', $fee, false, '' );
}
}
Run Code Online (Sandbox Code Playgroud)