0

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.

Ste Mappo
  • 19
  • 5

2 Answers2

0

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

MattHamer5
  • 1,431
  • 11
  • 21
  • That works great. Thank you! But now when you change the shipping option on the checkout page it doesn't change. I think it's because the page needs a refresh. what do you think? – Ste Mappo Feb 04 '20 at 15:09
  • @SteMappo Yes; it would need to be refreshed as WooCommerce would be displaying the cached fields (with the hidden ones). Unfortunately, with that snippet of code it isn't possible to do it without refreshing the page each time. – MattHamer5 Feb 04 '20 at 15:15
  • @SteMappo This answer might work for you https://stackoverflow.com/a/46647699/7821865 – MattHamer5 Feb 04 '20 at 15:16
  • 1
    You're a legend mate! I looked for hours for something similar to what I am doing. Thank you for your help! – Ste Mappo Feb 04 '20 at 15:19
  • Hello, I have added the code in the link you sent me and used the code from above. It works great, thank you! My next question is how do you remove the "Deliver to a different address" on the click of a new shipping option on the checkout page? – Ste Mappo Feb 10 '20 at 10:10
  • Hey, @MattHamer5 any ideas? – Ste Mappo Feb 10 '20 at 14:49
  • I'm pretty sure this is an option within the WooCommerce settings in the backend – MattHamer5 Feb 10 '20 at 16:15
  • @SteMappo https://docs.woocommerce.com/document/configuring-woocommerce-settings/#section-13 – MattHamer5 Feb 10 '20 at 16:16
0

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.

MefAldemisov
  • 867
  • 10
  • 21