1

I've created a custom dropdown "Is this a business address?" on the shipping address step of the Magento 2.1 checkout.

I'd like to hide the company field on the form until the user selects yes, then show it. If the user changed the dropdown to No, it would empty and hide the Company field.

Been going around in circles on this for quite a while now, so any help would be greatly appreciated!

2

We have done a simular thing on our checkout, it's a matter of adding some custom javascript work to your checkout page. Obviously make some kind of custom module to call the javascript you add.

Then simply use jQuery to check for variable information in the field and a result should follow.

// on change of this ID of your input do something
jQuery('#YOURINPUTID').change(function(){

// CHECKBOX EXAMPLE 
// if (jQuery(this).is(':checked')){ 

// of find a value like your question
 if (jQuery(this).val('yes')) {

//..do something here, for example change send the billing adres input to shipping input in our case. Just whatever jQuery you want to do here.                     
// Dunno exactly what you are trying to do but just put everything here

  var changeTarget = jQuery("#checkout-payment-method-load").parent().parent().parent();
  var billingValue = changeTarget.find("input[name='street[0]']").val();
  jQuery("input[name='street[0]']").val(billingValue).trigger("change");

// clear the street field after being copied to shipping
  changeTarget.find("input[name='street[0]']").val('').trigger("change");


} else {

// do other stuff when the value is not yes but no or something else
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.