• Hi. I’ve got Last name required on my Checkout page https://prntscr.com/nd57lb
    I want to change this and make NOT required if virtual products are in cart.

    
    add_filter( 'woocommerce_checkout_fields', 'woo_simplify_checkout_virtual' );
    function woo_simplify_checkout_virtual( $fields ) {
         
        $only_virtual = true;
         
        foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            // Check if there are non-virtual products
            if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false; 
        }
         
        if( $only_virtual ) {
            // WHAT SHOULD I PLACE HERE?
        return $fields;
    }
    

    I’m trying to unset Lat Name with

    $fields['billing_last_name']['required'] = false;

    but it doesn’t work.

    What code should I use to unset required field with code?
    Thanks.

    • This topic was modified 5 years, 11 months ago by ADvi.
    • This topic was modified 5 years, 11 months ago by ADvi.
  • The topic ‘Make field not required with code’ is closed to new replies.