• Resolved Ward

    (@yward)


    Is there is a way to hide the field if the selected shipping method isn’t pickup?

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author Tim Nolte

    (@tnolte)

    @yward there is already a request open for this. You can track it here: https://github.com/mattbanks/woocommerce-local-pickup-time/issues/23
    Thanks!

    Thread Starter Ward

    (@yward)

    Perfect, thank you, I’ll keep an eye!

    Thread Starter Ward

    (@yward)

    @tnolte

    LAST EDIT: I have solved this as follows:

    1- I commented out this rule from your original code:

    
    	// Check if set, if its not set add an error.
    		if ( ! $_POST['local_pickup_time_select'] ) {
    			wc_add_notice( __( 'Please select a pickup time.', 'woocommerce-local-pickup-time' ), 'error' );
    		}
    

    Then I created this snippet to dynamically do everything as follows:
    Worth noting that you’re free to use the snippet in your next release:

    
    
    //hide the local pickup thingy//
    add_action( 'woocommerce_after_checkout_form', 'disable_time_local_pickup' );
      
    function disable_time_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  
       } 
    	elseif ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
    		
       if(jQuery('#local_pickup_time_select').val()) {
    			wc_add_notice( __( 'Please select a pickup time.', 'woocommerce-local-pickup-time' ), 'error' );
    		}  
       } 
       
       // 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()) {
    			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 
    • This reply was modified 5 years, 9 months ago by Ward.
    • This reply was modified 5 years, 9 months ago by Ward.
    • This reply was modified 5 years, 9 months ago by Ward.
    • This reply was modified 5 years, 9 months ago by Ward.
    Thread Starter Ward

    (@yward)

    A small correction tot he code:

    //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
    • This reply was modified 5 years, 9 months ago by Ward.

    Thanks for the modification @yward, very appreciated. Unfortunately this modification now ignores the required date/time in the dropmenu. So a customer can make an order without selecting a pickup date/time. Pickup time of the order now after checkout: None

    Can you please help? Thanks in advance.

    Thread Starter Ward

    (@yward)

    @digitalmouse You’re welcome!
    Weird, this part of the code:

    
    if(jQuery('#local_pickup_time_select').val()) {<?php
    			wc_add_notice( __( 'Please select a pickup time.', 'woocommerce-local-pickup-time' ), 'error' );
    		?>} 

    Is supposed to validate that a value was selected before submission, I’ll take another look at it though and get back to you as soon as I can.
    Keep in mind I’m not the plugin’s author and I’m not affiliated with it just another user like you who had this issue.

    @yward I know, thanks for still helping out though ??
    I’m using the flatsome theme, but it also occurs on WordPress theme TwentyNineteen. Wp version: 5.2.3. Woocommerce: 3.7.1

    Just to be sure, step 1 is in woocommerce-local-pickup-time-select/public/class-local-pickup-time.php and step 2 is placed in woocommerce-local-pickup-time-select/woocommerce-local-pickup-time.php, right?

    Thread Starter Ward

    (@yward)

    @digitalmouse I apologize for the incredibly late reply as I have been busy irl

    Just add the following code to your functions.php along with everything in step 2.

    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.' );
        }
     }
    }
    • This reply was modified 5 years, 4 months ago by Ward.
    • This reply was modified 5 years, 4 months ago by Ward.

    @yward thank you for your effort. It seems to work as far as I can see!

    itsmir

    (@itsmir)

    Hello,

    Has this now been incorporated into a more recent version of the plugin oe will I need to use the above code?

    Plugin Author Tim Nolte

    (@tnolte)

    @itsmir this has not been incorporated yet. I will look at getting these changes into a new release within the next couple of weeks.

    Hello,
    sorry to bring back the subject but I need this function too.
    I have tried what @yward kindly wrote but I can’t get it to work.
    Here is the code I have put in function.php in my child theme.
    Even if I choose shipping by post , I can’t get rid of the local pick up time selection. I also found an error about json when I look in the browser console.
    Any help is most welcome ?? Thank you in advance

     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', 'Veuillez choisir un horaire de retrait.' );
        }
     }
    ?>
    	<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( __( 'Veuillez choisir un horaire de retrait.', 'woocommerce-local-pickup-time' ), 'error' );
    		?>}
                   } else {
                   jQuery('#local-pickup-time-select').fadeOut();
                } 
             });
          </script>
       <?php
      
    } 
    //end of it

    Hello,
    a minor improvement to @yward code in the case of having chosen a pickup time and then change the shipping to mailing : we have to empty the pickup time otherwise a date will be displayed in the backoffice.

    <?php
    //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( __( 'Veuillez choisir un horaire de retrait.', '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();	
                   } else {
                   jQuery('#local-pickup-time-select').fadeOut();
    			   jQuery('#local_pickup_time_select').val("none");
                } 
             });
          </script>
       <?php
      
    }
    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', 'Veuillez choisir un horaire de retrait.' );
        }
     }
    } 
    //end of it
    ?>

    Edit to my previous post, the code doesn’t work when I first load the cart page, the local-pickup-time-select field isn’t displayed.

    I’m on WP 5.4 and woocommerce 4.0.1
    Any advice would be great !

    Thread Starter Ward

    (@yward)

    First of all comment the original validation logic of the plugin as follows:
    commenting the following line:

    add_action( 'woocommerce_checkout_process', array( $this, 'field_process' ) );

    which is located in the file: public/class-local-pickup-time.php line 137.

    so it’ll look like:

    //add_action( 'woocommerce_checkout_process', array( $this, 'field_process' ) );

    And don’t worry, it’ll still validate if you have the proper method selected, just it’ll validate through my code, not the original one.
    This is current code I have in my installation, works flawlessly:

    // Conditional Show hide checkout fields based on chosen shipping methods
    add_action( 'wp_footer', 'conditionally_hidding_pickuptime' );
    function conditionally_hidding_pickuptime(){
        // Only on checkout page
        if( ! is_checkout() ) return;
    
        // The shipping methods rate ID "Local Pickup"
        $no_delivery = 'local_pickup:3';
    	$no_delivery2 = 'local_pickup:7';
    	$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    	$chosen_shipping = $chosen_methods[0];
        ?>
        <script>
            jQuery(function($){
                // Choosen shipping method selectors slug
                var shipMethod = 'input[name^="shipping_method"]',
                    shipMethodChecked = shipMethod+':checked';
    
                // Function that shows or hide imput select fields
                function showHide( actionToDo='show', selector='' ){
                    if( actionToDo == 'show' )
                        $(selector).show( 200, function(){
                            $(this).addClass("validate-required");
                        });
                    else
                        $(selector).hide( 200, function(){
                            $(this).removeClass("validate-required");
                        });
                }
    
                // Initialising: Hide if choosen shipping method is "Local Pickup"
                <?php if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) { ?>
    			showHide('show','#local-pickup-time-select' );
    			<?php  
       } 
    			else{
    			?>
    			showHide('hide','#local-pickup-time-select' );	
    			<?php
    			}
    			?>
          
    
                // Live event (When shipping method is changed)
                $( 'form.checkout' ).on( 'change', shipMethod, function() {
                    if( $(shipMethodChecked).val() == '<?php echo $no_delivery; ?>' ||$(shipMethodChecked).val() == '<?php echo $no_delivery2; ?>' ){
    					showHide('show','#local-pickup-time-select' );
    				}
                    else{
    					showHide('hide','#local-pickup-time-select' );
    				}
                });
            });
        </script>
        <?php
    }
    
    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.' );
        }
     }
    }
    //end of it//

    One note about the code, I have 2 pickup option in my shipping methods, their codes are local_pickup:3 and local_pickup:7 if you have more or if their codes are different, just edit that part.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Hide if Shipping method isn’t Pickup’ is closed to new replies.