Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.8 kB
4
Indexable
Never

add_action('wp_head', function(){
    if (!is_user_logged_in()){
        ?>
        <script>
            jQuery(document).ready(function(){
                jQuery('.woocommerce-checkout .b2bking_registration_roles_dropdown_section').detach().insertBefore('#billing_company_field');
                function showHideCompany(){
                    let val = jQuery('#b2bking_registration_roles_dropdown').val();
                    if (val === 'role_53'){
                        jQuery('#billing_company_field').css('display','none');
                        jQuery('#billing_company').attr('required', false);

                    } else {
                        jQuery('#billing_company_field').css('display','block');
                        jQuery('#billing_company').attr('required', true);
                    }
                }
                jQuery('#b2bking_registration_roles_dropdown').on('change', showHideCompany);
                showHideCompany();
                jQuery('<abbr class="required" title="required">*</abbr>').insertBefore('#billing_company_field .optional');
                jQuery('#billing_company_field .optional').css('display','none');
            });
        </script>
        <?php    
    }
});

add_action('woocommerce_checkout_process', 'custom_checkout_company_validation');

function custom_checkout_company_validation() {
    // Check if the specific role is selected
    if (isset($_POST['b2bking_registration_roles_dropdown']) && $_POST['b2bking_registration_roles_dropdown'] === 'role_54') {
        // Check if the company name field is empty
        if ( !isset($_POST['billing_company']) || empty($_POST['billing_company']) ) {
            // Display an error message
            wc_add_notice( __( 'Please enter your company name.', 'woocommerce' ), 'error' );
        }
    }
}
Leave a Comment