亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將百分比和固定成本的自定義費用添加到WooCommerce中的特定支付網關

將百分比和固定成本的自定義費用添加到WooCommerce中的特定支付網關

PHP
LEATH 2022-09-25 20:16:25
在Woo商務中,我需要對特定的支付網關應用自定義處理費。百分比成本的自定義處理費和每個固定成本的自定義處理費。我有這2段代碼:A) 成本百分比 - 函數/************************************************************//*  PERCENTACE COST **/// Add a custom fee based o cart subtotal// Add a custom fee based o cart subtotaladd_action( 'woocommerce_cart_calculate_fees', 'custom_percentage_fee', 20, 1 );function custom_percentage_fee ( $cart ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )        return; // Only checkout page    $payment_method = WC()->session->get( 'chosen_payment_method' );    if ( 'cod' == $payment_method ) {        $surcharge = $cart->subtotal * 0.025;        $cart->add_fee( 'Percentage Cost', $surcharge, true );    }}// jQuery - Update checkout on methode payment change  add_action( 'wp_footer', 'custom_checkout_jqscript' );function custom_checkout_jqscript() {    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )        return; // Only checkout page    ?>    <script type="text/javascript">    jQuery( function($){        $('form.checkout').on('change', 'input[name="payment_method"]', function(){            $(document.body).trigger('update_checkout');        });    });    </script>    <?php}結果前端B) 固定成本 - 功能/************************************************************//*  FIXED COST **/// Add a custom fee based o cart subtotaladd_action( 'woocommerce_cart_calculate_fees', 'custom_fixed_fee', 10, 1 );function custom_fixed_fee ( $cart ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    if ( 'cod' === WC()->session->get('chosen_payment_method') ) {        $fee = 0.31;        $cart->add_fee( 'Fixed Cost', $fee, true );    }}
查看完整描述

1 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

您發布的2個示例之間的差異非常小。您只需使用:


if ( 'cod' == $payment_method ) {

    $surcharge = $cart->subtotal * 0.025;

    $fee = 0.31;


    $together = $surcharge + $fee;

    $cart->add_fee( 'Plus Cost', $together, true );

}


查看完整回答
反對 回復 2022-09-25
  • 1 回答
  • 0 關注
  • 86 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號