首页 文章

填写textarea时更改支付网关

提问于
浏览
0

在Woocommerce中,我在结帐页面中创建了一个自定义文本区域字段

因此,如果我在textarea上写任何东西,支付网关将变为“检查”,如果没有,那么它将只是“paypal”

我不知道怎么做,因为我对WooCommerce知之甚少

woocommerce_form_field( 'custom_product', array(

   'type' => 'textarea',
   'label'      => __('Custom products', 'woocommerce'),
   'required'   => false,
   'class'      => array('form-row-wide'),
   'clear'     => true,
       ), $checkout->get_value( 'custom_product' ));

任何帮助将非常感谢 .

谢谢

2 回答

  • 1

    是的,您可以使用一些简单的jQuery脚本来执行此操作,具体取决于您要启用的支付网关 .

    您可能需要先为您的texarea添加一个额外的CSS类,以便使用查询脚本更好地定位它(我已经完成了一些代码,仅用于测试目的) .

    所以你的完整代码功能将是:

    add_action( 'woocommerce_after_order_notes', 'custom_checkout_textarea_field', 10, 1 );
    function custom_checkout_textarea_field( $checkout ){
    
        echo '<div id="custom-texarea-field">
            <h2>' . __('My Field Title', 'woocommerce') . '</h2>';
    
        woocommerce_form_field( 'custom_product', array(
    
           'type' => 'textarea',
           'label'      => __('Custom products', 'woocommerce'),
           'required'   => false,
           'class'      => array('custom-product-ta form-row-wide'), ## @ <== HERE
           'clear'     => true,
        ), $checkout->get_value( 'custom_product' ));
    
        echo '</div>';
    
    }
    

    然后,现在您还可以添加以下挂钩在 woocommerce_checkout_before_customer_details checkout操作钩子中的代码,这将在您的结帐页面中嵌入一些查询脚本 .

    在此示例中,jQuery脚本将启用Check支付方法的单选按钮,此时文本区域将填充某些内容 .

    所以这是用于此目的的附加代码:

    add_action('woocommerce_checkout_before_customer_details','custom_jquery_for_texarea');
    function custom_jquery_for_texarea(){
       ?>
       <script>
       jQuery(document).ready(function($) {
           $('.custom-product-ta textarea').on('input', function(){
                $('.wc_payment_method payment_method_cheque > input[name=payment_method]').prop('checked', true); 
           });
       });
       </script>
       <?php 
    }
    

    代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .

    This code is tested and works…


    jQuery代码的相关答案:JQuery: detect change in input field

    WooCommerce文档:Customizing checkout fields using actions and filters

  • 0

    如果您可以在结帐页面上看到选中付款方式的复选框,那么您可以使用jQuery在文本区域填充后模拟点击:

    jQuery('textarea').on('input', function(){
      jQuery('#payment_method_cheque').click(); // check if the radio input has this id #payment_method_cheque and change if not
    });
    

相关问题