Skip to content
Advertisement

Disable specific payment method when WooCommerce billing company is set

I am trying to disable the paypal payment gateway when the company name is added on a new order but for some reason my code isn’t working. Can somebody help me out?

//  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' );

Advertisement

Answer

Use the following to disable specific payment method when specific checkout field is filled by user on checkout page:

// 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;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement