• Resolved 00lessio

    (@00lessio)


    hello, I have created custom checkout fields that are repeated based on the quantity. the changes are seen on the page but the value is not seen in the email and on the order page. I didn’t find them in the database so I think it’s not saving them, I tried disabling the plugins but no difference, with active debugging there are no errors. I don’t understand what I’m doing wrong, this is the code:

    /* nuovo test custom field*/
    add_action( 'woocommerce_before_order_notes', 'persons_details' );
    function persons_details( $checkout ) {
        $count = WC()->cart->get_cart_contents_count();
        $i = 0;
    
        for( $k=1; $k<= $count; $k++ ) {
            $i++;
            echo '<div><strong>'. __('Dati iscritto n. ') . $i . '</strong></div>';
            
            woocommerce_form_field( 'cstm_full_name' . $i, array(
                'type'          => 'text',
                'class'         => array('my-field-class form-row-first'),
                'label'         => __("Nome e cognome"),
                'placeholder'   => __(""),
                'required'      => true,
            ), $checkout->get_value( 'cstm_full_name' . $i ));
            
            woocommerce_form_field( 'cstm_email' . $i, array(
                'type'          => 'email',
                'class'         => array( 'my-field-class form-row-last' ),
                'label'         => __( "Email" ),
                'placeholder'   => __(""),
                'required'      => true,
            ), $checkout->get_value( 'cstm_email' . $i ));
            
            woocommerce_form_field( 'cstm_phone' . $i, array(
                'type'          => 'text',
                'class'         => array('my-field-class form-row-first'),
                'label'         => __("Numero di telefono"),
                'placeholder'   => __(""),
                'required'      => true,
            ), $checkout->get_value( 'cstm_phone' . $i ));
            
            woocommerce_form_field( 'cstm_address' . $i, array(
                'type'          => 'textarea',
                'class'         => array('my-field-class form-row-last'),
                'label'         => __("Indirizzo di residenza"),
                'placeholder'   => __(""),
                'required'      => true,
            ), $checkout->get_value( 'cstm_address' . $i ));
            
            echo '<div class="clear"></div>
            <div class="clearbox"></div>';
        }
    }
    
    
    add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_field_order_meta' );
    function save_custom_checkout_field_order_meta( $order )
    {
        $count = WC()->cart->get_cart_contents_count();
        $order->update_meta_data( 'cstm_items_count', intval($count) ); // Save the cart contents count as meta data
        
        $i = 0;
        for($k=1; $k<= $count; $k++) {
            $i++;
            if ( isset($_POST['cstm_full_name'.$i]) && ! empty($_POST['cstm_full_name'.$i]) ) {
                $order->update_meta_data( 'cstm_full_name'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]) );
            }
            if ( isset($_POST['cstm_email'.$i]) && ! empty($_POST['cstm_email'.$i]) ) {
                $order->update_meta_data( 'cstm_email'.$i, sanitize_text_field($_POST['cstm_email'.$i]) );
            }
            if ( isset($_POST['cstm_phone'.$i]) && ! empty($_POST['cstm_phone'.$i])) {
                $order->update_meta_data( 'cstm_phone'.$i, sanitize_text_field($_POST['cstm_phone'.$i]) );
            }
            if ( isset($_POST['cstm_address'.$i]) && ! empty($_POST['cstm_address'.$i])) {
                $order->update_meta_data( 'cstm_address'.$i, sanitize_text_field($_POST['cstm_address'.$i]) );
            }
        }
    }
    
    add_action( 'woocommerce_email_order_meta', 'add_email_custom_order_meta', 10, 3 );
    function add_email_custom_order_meta( $order, $sent_to_admin, $plain_text ){
        $quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data
        
        echo '<ul>';
        $i = 0;
        for( $k=1; $k <= $quantity; $k++ ) {
            $i++;
            echo '<li><strong>'. __("Dati iscritto n. ") . $i . '<strong></li>
            <li>' . __("Nome e cognome: ") . $order->get_meta('cstm_full_name'.$i) . '</li>
            <li>' . __("Email: ") . $order->get_meta('cstm_email'.$i) . '</li>
            <li>' . __("Numero di telefono: ") . $order->get_meta('cstm_phone'.$i) . '</li>
            <li>' . __("Indirizzo di residenza: ") . $order->get_meta('cstm_address'.$i) . '</li>';
        }
        echo '</ul>';
    }
    
    add_action( 'woocommerce_admin_order_data_after_order_details', 'display_custom_fields_in_admin_order_pages' );
    function display_custom_fields_in_admin_order_pages( $order ){ 
        $quantity = $order->get_meta('cstm_items_count'); // Get items quantity count from meta data  
        
        echo '<div class="order_data_column" style="width: 100% !important;">
            <h4>' . __( 'Your label' ) . '</h4>
            <ul>';
            $i = 0;
            for( $k=1; $k <= $quantity; $k++ ) {
                $i++;
                echo '<li><strong>'. __("Dati iscritto n. ") . $i . '<strong></li>
                <li>' . __("Nome e cognome: ") . $order->get_meta('cstm_full_name'.$i) . '</li>
                <li>' . __("Email: ") . $order->get_meta('cstm_email'.$i) . '</li>
                <li>' . __("Numero di telefono: ") . $order->get_meta('cstm_phone'.$i) . '</li>
                <li>' . __("Indirizzo di residenza: ") .$order->get_meta('cstm_address'.$i) . '</li>';
            }
            echo '</ul>
        </div>';
    }
    phpwordpresswoocommerceguardareCampi personalizzati
    Condividere
    Modificare
    Eliminare
    Bandiera
    modificato il 16 giugno alle 8:41
    ha chiesto l'8 giugno alle 5:51
    L'avatar dell'utente di LesCa
    Les Ca
    2144 distintivi in ??bronzo
    @LoicTheAztec hai chiuso la domanda ma ho provato anche le vecchie domande ma non riesco a capire dove sbaglio, continuo a non vedere il valore dei campi nelle email e nella pagina di amministrazione dell'ordine. Vorrei che tu potessi anche modificare i campi dalla pagina di amministrazione dell'ordine in caso di errori 
    – 
    Les Ca
     8 giugno alle 14:39   Eliminare
    
Viewing 1 replies (of 1 total)
  • Hi @00lessio

    Thanks for reaching out!

    I understand that you have created custom checkout fields on your site using the code snippet above, however, changes are seen on the page but the value is not seen in the email and on the order page, right?

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Checkout custom fields are not saved’ is closed to new replies.