Home > Blockchain >  Disable place order button if billing_address_2 contains value at WooCommerce checkout
Disable place order button if billing_address_2 contains value at WooCommerce checkout

Time:11-03

I need to disable place order button if billing_address_2 contains value 10 right before order is placed.

I was thinking about how we could get this directly from the field not from the account, but live from the field when user choose the option. The field is now set as a dropdown select so people could choose from the options.

I have this code so far, but unfortunately without the desired result. I think I'm not far from it to make it work. Any adivce?

Code so far:

 add_filter('woocommerce_order_button_html', 'disable_place_order_button_html', 10, 2);
 function disable_place_order_button_html( $button ) {
        // Get the field data
        $address_fields = $_POST['billing_address_2'] );
        
        // If the billing_address 2 contains 10 we disable button
        if( $address_fields == '10 ' ) {
            $style  = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important; text-align:center;"';
            $text   = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
            $button = '<a  '.$style.'>' . $text . '</a>';
        }
      return $button;
 }

The billing_address_2 dropdown code

add_filter( 'woocommerce_default_address_fields' , 'address_field_dropdown' );
 
function address_field_dropdown( $address_fields ) {
 
    $location_array = array(
      'Location 1' => '1',
      'Location 2' => '2',
      'Location 3' => '3',
      'Location 4' => '4',
      'Location 5' => '5',
      'Location 6' => '6',
      'Location 7' => '7',
      'Location 8' => '8',
      'Location 9' => '9',
      'Location 10' => '10 ',
    );
 
     $address_fields['address_2']['label'] = 'Počet osôb';
     $address_fields['address_2']['type'] = 'select';
     $address_fields['address_2']['options'] = $location_array;
 
     return $address_fields;
 
}

CodePudding user response:

To apply this live when the desired value is selected from the dropdown menu you can use jQuery

So you get:

function action_wp_footer() {
    // Only on checkout
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
        ?>
        <script>
        jQuery( function($) {
            // Function 
            function place_order_button() {             
                // Compare
                if ( $( '#billing_address_2' ).val() == 'Location 10' ) {
                    $( '#place_order' ).prop( 'disabled', true );
                    $( '#place_order' ).css({ 'background-color': 'silver', 'color': 'white', 'cursor': 'not-allowed' });
                } else {
                    $( '#place_order' ).prop( 'disabled', false );
                    $( '#place_order' ).removeAttr( 'style' );
                }
            }
            
            // On change
            $( document ).on( 'change', function() {                
                place_order_button();
            });
            
            // Ajax
            $( document ).ajaxComplete( function () {               
                place_order_button()
            });
        
        });
        </script>
        <?php
    endif;
}
add_action( 'wp_footer', 'action_wp_footer', 10, 0 );
  • Related