0

I need to provide conditional required fields on 2 of 3 products in my very specific "store" . 1st product has no added fields. Second one has 1 required. 3rd has the same as the second plus one more required. I have tried plugins as I am right brained but they all cause cart failure. I have cobbled together some code from the inter web that has gotten me passed the cart failure but it presents both of the fields to all of the products (I set the second condition to optional in the code so that it would at least work on a basic level with placeholder text in the second field about requirement). In addition I need these conditional fields to be passed to to the completed order so that they show up in the order tables in admin. here is my rudimentary code.

/**
 *  checkout field addition License Plate.
 * 
 * @param  array $fields List of existing billing fields.
 * @return array         List of modified billing fields.
 */
function parking_add_checkout_fields( $fields ) {
    $fields['billing_FIELD_ID'] = array(
        'label'        => __( 'License Plate & State' ),
        'type'        => 'text',
        'class'        => array( 'form-row-wide' ),
        'priority'     => 110,
        'required'     => true,
    );

    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'parking_add_checkout_fields' );

function js_woocommerce_admin_billing_fields( $fields ) {
    $fields['FIELD_ID'] = array(
        'label' => __( 'License Plate & State' ),
        'show' => true,
    );

    return $fields;

}
add_filter( 'woocommerce_admin_billing_fields', 'js_woocommerce_admin_billing_fields' );

 /*  checkout field addition Citation.
 * 
 * @param  array $fields List of existing billing fields.
 * @return array         List of modified billing fields.
 */
function citation_add_checkout_fields( $fields ) {
    $fields['billing_FIELD2_ID'] = array(
        'label'        => __( 'Enter Citation #' ),
        'type'        => 'text',
        'class'        => array( 'form-row-wide' ),
        'priority'     => 112,
        'required'     => false,
        'placeholder'  => 'If you are purchasing a Citation Dismissal Permit, enter Citation number here',
    );

    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'citation_add_checkout_fields' );
        $fields['FIELD2_ID'] = array(
        'label' => __( 'Enter Citation #' ),
        'show' => true,
    );

    return $fields;

add_filter( 'woocommerce_admin_billing_fields', 'js_woocommerce_admin_billing_fields' );

/**
 * Remove optional label
 * https://elextensions.com/knowledge-base/remove-optional-text-woocommerce-checkout-fields/
 */
add_filter( 'woocommerce_form_field' , 'elex_remove_checkout_optional_text', 10, 4 );
function elex_remove_checkout_optional_text( $field, $key, $args, $value ) {
    if( is_checkout() && ! is_wc_endpoint_url() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}
WindWeapon
  • 11
  • 2

0 Answers0