miriam-m
3/2/2020 - 1:07 PM

remove local pickup field for events

//hide the local pickup thingy
add_action( 'woocommerce_after_checkout_form', 'bbloomer_disable_shipping_local_pickup' );
  
function bbloomer_disable_shipping_local_pickup( $available_gateways ) {
    
   // Part 1: Hide time based on the static choice @ Cart
 
   $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
   $chosen_shipping = $chosen_methods[0];
  if ( 0 !== strpos( $chosen_shipping, 'local_pickup' ) ) {
   ?>
      <script type="text/javascript">
         jQuery('#local-pickup-time-select').fadeOut();
      </script>
   <?php  
   } 
	else {
		?>
		<script type="text/javascript">
		if(jQuery('#local_pickup_time_select').val()) {<?php
			wc_add_notice( __( 'Please select a pickup time.', 'woocommerce-local-pickup-time' ), 'error' );
		?>}   </script>
	<?php
   } 
   
   // Part 2: Hide time based on the dynamic choice @ Checkout
 
   ?>
	<script type="text/javascript">
         jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
            var val = jQuery( this ).val();
            if (val.match("^local_pickup")) {
				jQuery('#local-pickup-time-select').fadeIn();	
if(jQuery('#local_pickup_time_select').val()) {<?php
			wc_add_notice( __( 'Please select a pickup time.', 'woocommerce-local-pickup-time' ), 'error' );
		?>}
               } else {
               jQuery('#local-pickup-time-select').fadeOut();
            } 
         });
      </script> -->
   <?php
  
} 
//end of it


add_action( 'woocommerce_after_checkout_validation', 'a4m_validate_pickup', 10, 2);
 
function a4m_validate_pickup( $fields, $errors ){
	$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
	$chosen_shipping = $chosen_methods[0];
 if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
    if ( empty($_POST[ 'local_pickup_time_select' ] ) ){
        $errors->add( 'validation', 'Please Select a pickup time.' );
    }
 }
}