【问题标题】:Change Pay button on checkout based on Woocommerce chosen payment method根据 Woocommerce 选择的付款方式更改结帐时的付款按钮
【发布时间】:2018-08-30 12:43:08
【问题描述】:

您好,有人知道如何根据选择的付款方式更改结帐时的付款按钮吗?我找到了一些东西,但我不知道我是否可以将它变成 function.php 中的 sn-p?谢谢你。

    public function __construct() {
    $this->id = 'ry_ecpay_atm';
    $this->has_fields = false;
    $this->order_button_text = __('Pay via ATM', RY_WT::$textdomain);
    $this->method_title = __('ECPay ATM', RY_WT::$textdomain);
    $this->method_description = '';

【问题讨论】:

    标签: php jquery wordpress woocommerce checkout


    【解决方案1】:

    我认为这是一个更简单的解决方案:

    add_filter('woocommerce_available_payment_gateways', 'change_barion_label');
    function change_barion_label($gateways) {
        if($gateways['ry_ecpay_atm']) {
            $gateways['ry_ecpay_atm']->order_button_text = 'new label';
        }
        return $gateways;
    }
    

    WooCommerce 在加载支付网关时运行此过滤器,因此它应该在站点范围内工作。

    【讨论】:

      【解决方案2】:
      add_filter('woocommerce_order_button_text', 'custom_order_button_text' );
      function custom_order_button_text( $order_button_text ) {
          $default = __( 'Place order', 'woocommerce' ); // If needed
          // Get the chosen payment gateway (dynamically)
          $chosen_payment_method = WC()->session->get('chosen_payment_method');
      
          ## --- For TESTING raw output on the chosen gateway ID --- ##
          // echo '<pre>' . $chosen_payment_method . '</pre>'; // <=== uncomment for testing
      
          // Set your payment gateways IDs in EACH "IF" statement
          if( $chosen_payment_method == 'bacs'){
              // HERE set your custom button text
              $order_button_text = __( 'Bank wire payment', 'woocommerce' ); 
             } elseif( $chosen_payment_method == 'ecpay_shipping_pay'){
              // HERE set your custom button text
              $order_button_text = __( 'Place order via Market', 'woocommerce' ); 
             } elseif( $chosen_payment_method == 'ecpay'){
              // HERE set your custom button text
              $order_button_text = __( 'Place order via ATM/Credit Card', 'woocommerce' ); 
           }
          // jQuery code: Make dynamic text button "on change" event ?>
          <script type="text/javascript">
          (function($){
              $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
                  var t = { updateTimer: !1,  dirtyInput: !1,
                      reset_update_checkout_timer: function() {
                          clearTimeout(t.updateTimer)
                      },  trigger_update_checkout: function() {
                          t.reset_update_checkout_timer(), t.dirtyInput = !1,
                          $(document.body).trigger("update_checkout")
                      }
                  };
                  t.trigger_update_checkout();
              });
          })(jQuery);
          </script><?php
      
          return $order_button_text;
        }
      

      这是该下拉菜单中的付款。

      'ecpay_payment_methods' => array(
                  'title'     => __( 'Payment Method', 'ecpay' ),
                  'type'      => 'multiselect',
                  'description'   => __( 'Press CTRL and the right button on the mouse to select multi payments.', 'ecpay' ),
                  'options'   => array(
                      'Credit'    => $this->get_payment_desc('Credit'),
                      'Credit_3'  => $this->get_payment_desc('Credit_3'),
                      'Credit_6'  => $this->get_payment_desc('Credit_6'),
                      'Credit_12'     => $this->get_payment_desc('Credit_12'),
                      'Credit_18'     => $this->get_payment_desc('Credit_18'),
                      'Credit_24'     => $this->get_payment_desc('Credit_24'),
                      'WebATM'    => $this->get_payment_desc('WebATM'),
                      'ATM'       => $this->get_payment_desc('ATM'),
                      'CVS'       => $this->get_payment_desc('CVS'),
                      'BARCODE'   => $this->get_payment_desc('BARCODE'),
                      'ApplePay'  => $this->get_payment_desc('ApplePay')
                  ),
      

      【讨论】:

      • 这个下拉菜单非常不寻常......所以我不知道如何处理它,因为代码与支付网关 ID 一起工作,在这种情况下是“ecpay”......你应该需要添加一个 @987654324 @查看选择不同值时显示的内容(仅用于临时测试)
      • 我应该在插件文件中添加代码并从下拉菜单中选择付款方式,然后查看屏幕上打印的内容,是吗?谢谢。
      • 好的,在你的代码中看到它(我已经更新了它)......你必须取消注释并保存,只是为了测试并查看当你在网关上进行更改时的输出+选择字段。这样您将获得输出……但我不确定您的选择字段是否会有所作为,因为它们非常自定义且不寻常……
      • 这里是打印结果。 ibb.co/hhjOaH 我通过选择 ATM 或信用卡购物车获得了“ecpay”。这是否意味着该功能不适用于下拉菜单?
      • 是的,因为这是非常自定义的东西,woocommerce 无法处理...... Woocommerce 只处理支付网关 ID(就像它们显示在 Woocommerce 设置 > 结帐上一样,所以在您的情况下,它是一种子 ID由相关的支付网关插件专门处理……
      【解决方案3】:

      这可以使用以下代码完成(您将在其中设置支付网关 ID 和相应的所需按钮文本)

      add_filter('woocommerce_order_button_text', 'custom_order_button_text' );
      function custom_order_button_text( $order_button_text ) {
          $default = __( 'Place order', 'woocommerce' ); // If needed
          // Get the chosen payment gateway (dynamically)
          $chosen_payment_method = WC()->session->get('chosen_payment_method');
      
          // Set your payment gateways IDs in EACH "IF" statement
          if( $chosen_payment_method == 'bacs'){
              // HERE set your custom button text
              $order_button_text = __( 'Bank wire payment', 'woocommerce' ); 
          } elseif( $chosen_payment_method == 'ry_ecpay_atm'){
              // HERE set your custom button text
              $order_button_text = __( 'Place order via ECPay', 'woocommerce' ); 
          }
          // jQuery code: Make dynamic text button "on change" event ?>
          <script type="text/javascript">
          (function($){
              $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
                  var t = { updateTimer: !1,  dirtyInput: !1,
                      reset_update_checkout_timer: function() {
                          clearTimeout(t.updateTimer)
                      },  trigger_update_checkout: function() {
                          t.reset_update_checkout_timer(), t.dirtyInput = !1,
                          $(document.body).trigger("update_checkout")
                      }
                  };
                  t.trigger_update_checkout();
              });
          })(jQuery);
          </script><?php
      
          return $order_button_text;
      }
      

      代码进入您的活动子主题(或主题)的 function.php 文件中。经过测试并且可以工作。

      【讨论】:

      • 嗨,非常感谢。它有效,但我的问题没有解决。我有更多的付款方式。如何配置代码?该代码是否适用于这样的下拉支付菜单?ibb.co/eE1GCx
      • 感谢您的提醒。我只使用社区情侣时间。我只是投票并解决。这是代码。它真的很好用,只是这里不能定义 dorpdown 付款,对吧?而且代码太长了。
      猜你喜欢
      • 2020-05-28
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2018-01-26
      • 1970-01-01
      • 2019-08-29
      相关资源
      最近更新 更多