【问题标题】:Disable specific payment method when WooCommerce billing company is set设置 WooCommerce 计费公司时禁用特定付款方式
【发布时间】:2021-05-22 09:30:12
【问题描述】:

在新订单中添加公司名称时,我试图禁用贝宝支付网关,但由于某种原因我的代码无法正常工作。有人可以帮帮我吗?

//  Disable gateway if company name is filled in
function payment_gateway_disable_paypal( $available_gateways ) {
    global $woocommerce;
    if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_billing_company() == "" ) {
        unset(  $available_gateways['paypal'] );
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_paypal' );

【问题讨论】:

    标签: php jquery ajax wordpress woocommerce


    【解决方案1】:

    当用户在结帐页面填写特定结帐字段时,使用以下方法禁用特定付款方式:

    // Jquery script that send the Ajax request
    add_action( 'woocommerce_after_checkout_form', 'custom_checkout_js_script' );
    function custom_checkout_js_script() {
        $field_key = 'billing_company'; // Here set the targeted field
    
        WC()->session->__unset('field_'.$field_key);
        ?>
        <script type="text/javascript">
        jQuery(function($){
            if (typeof wc_checkout_params === 'undefined')
                return false;
    
            var field = '[name="<?php echo $field_key; ?>"]';
    
            $( 'form.checkout' ).on('input change', field, function() {
                $.ajax({
                    type: 'POST',
                    url: wc_checkout_params.ajax_url,
                    data: {
                        'action': 'targeted_checkout_field_change',
                        'field_key': '<?php echo $field_key; ?>',
                        'field_value': $(this).val(),
                    },
                    success: function (result) {
                        $(document.body).trigger('update_checkout');
                        console.log(result); // For testing only
                    },
                });
            });
        });
        </script>
        <?php
    }
    
    // The Wordpress Ajax PHP receiver
    add_action( 'wp_ajax_targeted_checkout_field_change', 'get_ajax_targeted_checkout_field_change' );
    add_action( 'wp_ajax_nopriv_targeted_checkout_field_change', 'get_ajax_targeted_checkout_field_change' );
    function get_ajax_targeted_checkout_field_change() {
        // Checking that the posted email is valid
        if ( isset($_POST['field_key']) && isset($_POST['field_value']) ) {
    
            // Set the value in a custom Woocommerce session
            WC()->session->set('field_'. esc_attr($_POST['field_key']), esc_attr($_POST['field_value']) );
    
            // Return the session value to jQuery
            echo json_encode(WC()->session->get('field_'. esc_attr($_POST['field_key']))); // For testing only
        }
        wp_die(); // always use die at the end
    }
    
    // Disable specific payment method if specif checkout field is set
    add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_paypal' );
    function payment_gateway_disable_paypal( $available_gateways ) {
        if( is_admin() )
            return $available_gateways;
    
        $payment_id  = 'paypal'; // Here define the payment Id to disable
        $field_key   = 'billing_company'; // Here set the targeted field
    
        $field_value =  WC()->session->get('field_'.$field_key);
    
        if ( isset($available_gateways[$payment_id]) && ! empty($field_value) ) {
            unset(  $available_gateways[$payment_id] );
        }
        return $available_gateways;
    }
    

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

    【讨论】:

      猜你喜欢
      • 2013-05-03
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 2021-03-07
      • 2018-06-10
      • 2021-06-07
      相关资源
      最近更新 更多