Tips on how to cover firm subject primarily based on a Woocommerce customized checkout radio choice?

This one here’s a nifty one. I’ll create a customized radio choice on Woocommerce checkout web page. This choice has two choices: “Private” and “Enterprise”. Then I’ll cover firm subject provided that consumer has chosen choice “Private”.

The place so as to add the code?

Add a snippet proven right here beneath to your little one theme’s features.php file or higher but, use a snippet supervisor like Code Snippets or WpCodeBox (my favourite).

WpCodeBox is my favourite code snippets supervisor for WordPress. This can be a premium plugin and if you happen to’re , then seize WPCodeBox with a pleasant 20% low cost right here (SAVE 20% Coupon WPSH20).

Add customized radio choice subject on Woocommerce checkout web page and conceal it when “Private” is chosen

<span position="button" tabindex="0" data-code="add_action('woocommerce_before_checkout_billing_form', 'add_radio_selection_checkout');

perform add_radio_selection_checkout($checkout) {
echo '<div id="my_custom_checkout_field"><h2>'.__('Account Sort').'</h2>';

woocommerce_form_field('account_type', array(
'kind' => 'radio',
'class' => array('my-field-class form-row-wide'),
'label' => __('Account Sort'),
'required' => true,
'choices' => array(
'private' => __('Private'),
'enterprise' => __('Enterprise')
)
), $checkout->get_value('account_type'));

echo '</div>';
}

// Present/cover firm identify subject primarily based on radio choice
add_action('wp_footer', 'my_custom_checkout_field_script');

perform my_custom_checkout_field_script() {
if (is_checkout()) {
?>
<script kind="textual content/javascript">
jQuery(doc).prepared(perform($) {
$('kind.checkout').on('change', 'enter[name="account_type"]', perform() {
if ($(this).val() == 'private') {
$('#billing_company_field').cover();
} else {
$('#billing_company_field').present();
}
});
});
</script>

add_action('woocommerce_before_checkout_billing_form', 'add_radio_selection_checkout');

perform add_radio_selection_checkout($checkout) {
    echo '<div id="my_custom_checkout_field"><h2>'.__('Account Sort').'</h2>';

    woocommerce_form_field('account_type', array(
        'kind' => 'radio',
        'class' => array('my-field-class form-row-wide'),
        'label' => __('Account Sort'),
        'required' => true,
        'choices' => array(
            'private' => __('Private'),
            'enterprise' => __('Enterprise')
        )
    ), $checkout->get_value('account_type'));

    echo '</div>';
}

// Present/cover firm identify subject primarily based on radio choice
add_action('wp_footer', 'my_custom_checkout_field_script');

perform my_custom_checkout_field_script() {
    if (is_checkout()) {
        ?>
        <script kind="textual content/javascript">
            jQuery(doc).prepared(perform($) {
                $('kind.checkout').on('change', 'enter[name="account_type"]', perform() {
                    if ($(this).val() == 'private') {
                        $('#billing_company_field').cover();
                    } else {
                        $('#billing_company_field').present();
                    }
                });
            });
        </script>
        <?php
    }
}

Customized CSS

You might have to tweak it with a customized CSS. So, add this one to the Look >>> Customise >> Extra CSS and modify it accordingly.

.my-field-class .woocommerce-input-wrapper {
	show: flex;
}
.my-field-class label.radio {
	margin-left: 10px
}
.my-field-class enter {
	margin-left: 20px;
}

That is the top consequence

How to hide company field based on a Woocommerce custom checkout radio selection?

Associated Woocommerce hacks

  • Tips on how to Customise Woocommerce Orders web page? (20 hacks)

  • Tips on how to Disguise Woocommerce Delivery Strategies (Conditionally) – A Full Information

  • Tips on how to show Woocommerce my account tabs horizontally?

  • Tips on how to Customise Woocommerce Admin Dashboard? 16 hacks

  • Tips on how to Show Woocommerce Cost Strategies Conditionally? (14 hacks)

  • Tips on how to Customise Woocommerce Inventory Standing? (17 hacks)

  • Tips on how to Add Customized Endpoints in WooCommerce?

  • Tips on how to Create Customized Product Badges for Woocommerce?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Scroll to Top