更改WooCommerce結帳頁面上「立即付款」按鈕的文本,以符合支付網關選項
P粉395056196
P粉395056196 2023-08-16 14:51:27
0
1
606
<p>我已經試了幾個小時了,但是無論如何都無法讓這個「基本」的東西正常工作。 </p> <p>我有一堆可用的支付網關,我需要將其名稱(包括訂單總金額)包含在「立即付款」按鈕文字中。 </p> <p><strong>範例</strong>:「<code>使用Stripe付款訂單$49</code>」</p> <p>我有一段程式碼,據說可以在更改網關時自動更新結帳。請問,有人可以幫忙嗎? </p> <pre class="brush:php;toolbar:false;">add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10, 1 ); function order_button_text_based_on_gateway( $cart ) { // 確保我們取得了支付網關 $payment_method = WC()->session->get( 'chosen_payment_method' ); // 基於不同的網關,顯示不同的按鈕文字(下單按鈕) if ( $payment_method == ' bacs ' ) { return sprintf( '%s %s', __('下單並支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 使用WireTransfer' ); } elseif ( $payment_method == ' cheque ' ) { return sprintf( '%s %s', __('下單並支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 使用個人支票' ); } elseif ( $payment_method == ' cod ' ) { return sprintf( '%s %s', __('下單並支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 貨到付款' ); } elseif ( $payment_method == ' etco ' ) { return sprintf( '%s %s', __('下單並支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 使用EtCo' ); } else ( $payment_method == ' stripe ' ) { return sprintf( '%s %s', __('下單並支付', 'woocommerce'), strip_tags( WC()->cart->get_total() ) . ' 使用Stripe' ); } }</pre> <p><strong>「自動更新」結帳腳本:</strong></p> <pre class="brush:php;toolbar:false;">add_action( 'wp_footer', 'reload_checkout_based_on_gateway_change', 999 ); function reload_checkout_based_on_gateway_change() { if ( is_checkout() && ! is_admin() ) { // 結束PHP,開始SCRIPT ?> <script> ( function( $ ) { $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() { $( 'body' ).trigger( 'update_checkout' ); } ); } ) ( jQuery ); </script> <?php } }</pre> <p><br /></p>
P粉395056196
P粉395056196

全部回覆(1)
P粉594941301

你的程式碼中有很多錯誤:

  • 主要錯誤是關於字串:'cheque''cheque'是兩個不同的字串。
    所以在所有的if語句中,沒有一個支付方式符合。
  • 另一個錯誤是else不支援任何條件參數。

有多種方法可以更改結帳「下單」按鈕的文字:

add_filter( 'woocommerce_order_button_text', 'order_button_text_based_on_gateway', 10 );
function order_button_text_based_on_gateway( $button_text ) {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        $payment_method    = WC()->session->get( 'chosen_payment_method' ); // 获取当前支付网关
        $cart_total_string = strip_tags( WC()->cart->get_total() ); // 获取订单总额字符串
        $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // 下单按钮

        if ( $payment_method == 'bacs' ) {
            $payment_method_text = __('using WireTransfer', 'woocommerce');
        } 
        elseif ( $payment_method == 'cheque' ) {
            $payment_method_text = __('with a personal cheque', 'woocommerce');
        } 
        elseif ( $payment_method == 'cod' ) {
            $payment_method_text = __('on delivery', 'woocommerce');
        }
        elseif ( $payment_method == 'etco' ) {
            $payment_method_text = __('using EtCo', 'woocommerce');
        }
        elseif ( $payment_method == 'stripe' ) {
            $payment_method_text = __('using Stripe', 'woocommerce');
        }

        if ( isset($payment_method_text) ) {
            $button_text = sprintf( '%s %s %s', $pay_order_text, $cart_total_string, $payment_method_text );
        }
    }
    return $button_text;
}

// 在支付方式更改时更新结账(jQuery)
add_action( 'woocommerce_checkout_init', 'trigger_update_checkout_on_payment_method_change' );
function trigger_update_checkout_on_payment_method_change(){
    wc_enqueue_js("$('form.checkout').on( 'change', 'input[name=payment_method]', function(){
        $(document.body).trigger('update_checkout');
    });");
}

或也可以使用WC_Payment_Gatewayorder_button_text屬性,如下所示:

add_filter('woocommerce_available_payment_gateways', 'change_payment_text_button');
function change_payment_text_button( $payment_gateways ) {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        $cart_total_string = strip_tags( WC()->cart->get_total() ); // 获取订单总额字符串
        $pay_order_text    = __('Order &amp; Pay', 'woocommerce'); // 下单按钮文本

        if ( isset($payment_gateways['bacs']) ) {
            $payment_gateways['bacs']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('using WireTransfer', 'woocommerce') );
        } 
        if ( isset($payment_gateways['cheque']) ) {
            $payment_gateways['cheque']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('with a personal cheque', 'woocommerce') );
        } 
        if ( isset($payment_gateways['cod']) ) {
            $payment_gateways['cod']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('on delivery', 'woocommerce') );
        }
        if ( isset($payment_gateways['etco']) ) {
            $payment_gateways['etco']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('using EtCo', 'woocommerce') );
        }
        if ( isset($payment_gateways['stripe']) ) {
            $payment_gateways['stripe']->order_button_text = sprintf( '%s %s %s', 
                $pay_order_text, $cart_total_string, __('using Stripe', 'woocommerce') );
        }
    }
    return $payment_gateways;
}

將程式碼放在你的子主題的functions.php檔案中(或外掛中)。經過測試,可以正常工作。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!