为 Woocommerce 中的特定支付网关添加自定义费用

Вик*_*нко 4 php wordpress jquery woocommerce fee

选择付款方式(信用卡)时如何添加总金额的百分比?

\n\n

例如:如果客户支付的是货到付款,那么是底价,如果我选择网上支付,那么我收取的百分比是加到总金额中的吗?

\n\n

货到付款网关ID设置:cod\n网上支付网关ID设置:tinkoff

\n\n
add_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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

有问题的图像

\n

Loi*_*tec 5

尝试以下重新访问的代码应该可以解决问题:

\n\n
// Add a custom fee based o cart subtotal\nadd_action( \'woocommerce_cart_calculate_fees\', \'custom_fee_for_tinkoff\', 20, 1 );\nfunction custom_fee_for_tinkoff ( $cart ) {\n    if ( is_admin() && ! defined( \'DOING_AJAX\' ) )\n        return;\n\n    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )\n        return; // Only checkout page\n\n    $payment_method = WC()->session->get( \'chosen_payment_method\' );\n\n    if ( \'tinkoff\' == $payment_method ) {\n        $surcharge = $cart->subtotal * 0.025;\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}\n\n// jQuery - Update checkout on methode payment change  \nadd_action( \'wp_footer\', \'custom_checkout_jqscript\' );\nfunction custom_checkout_jqscript() {\n    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )\n        return; // Only checkout page\n    ?>\n    <script type="text/javascript">\n    jQuery( function($){\n        $(\'form.checkout\').on(\'change\', \'input[name="payment_method"]\', function(){\n            $(document.body).trigger(\'update_checkout\');\n        });\n    });\n    </script>\n    <?php\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。

\n