0

I found a code online that has worked (https://gist.github.com/corsonr/599261be409c98b01083b9aa5f81c628?permalink_comment_id=3735869), but I can only add one checkout field on each product. However, I want two different checkouts, therefore I need to be able to add more fields. How can I do that?

I tried this, but only works to add a single field on each product:

add_action( 'woocommerce_checkout_fields', 'woo_add_conditional_checkout_fields' );

function woo_add_conditional_checkout_fields( $fields ) {

    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_id = $cart_item['product_id'];
            if( $product_id == 7005 ) {
                $fields['billing']['billing_field_product1'] = array(
                    'label'     => __('Product 1' . $product_id, 'woocommerce'),
                    'placeholder'   => _x('Field for Product ' . $product_id, 'placeholder', 'woocommerce'),
                    'required'  => false,
                    'class'     => array('form-row-wide'),
                    'clear'     => true,
                    'default'   => '1'
                );
                
            }

            if( $product_id == 6405 ) {
                $fields['billing']['billing_field_product2' . $product_id] = array(
                    'label'     => __('Product 1 ' . $product_id, 'woocommerce'),
                    'placeholder'   => _x('Field for Product ' . $product_id, 'placeholder', 'woocommerce'),
                    'required'  => false,
                    'class'     => array('form-row-wide'),
                    'clear'     => true,
                    'default'   => '2'
                );
                
            }
    }

    // Return checkout fields.
    return $fields;

}
Nerea
  • 1

0 Answers0