skip to Main Content

How do you change the woo-commerce checkout form for different shipping options? If they choose free shipping the checkout page displays shipping form. If they choose e-voucher the check out page displays a simpler form.

2

Answers


  1. You can use the below code snippet to hide whichever fields you chose when the “e-voucher” shipping method is chosen. Simply put the following inside your functions file.

    <?php
      add_filter('woocommerce_checkout_fields', 'evoucher_remove_fields');
    
      function evoucher_remove_fields($fields) {
    
          $shipping_method ='evoucher:1'; // Change this to the value name of your shipping method
          global $woocommerce;
          $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
          $chosen_shipping = $chosen_methods[0];
    
          if ($chosen_shipping == $shipping_method) {
              unset($fields['billing']['billing_address_1']); // Hides billing address line 1
              unset($fields['billing']['billing_address_2']); // Hides billing address line 2
          }
          return $fields;
    
      }
    ?>
    

    Source

    Login or Signup to reply.
  2. There is a good plugin for such purposes. Its free version includes conditional logic, e.g. change the form with the base on shipping option.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search