• Resolved suyog3p

    (@suyog3p)


    Hello Team, I have added extra options for the products and can see submitted data from those options in cart, order details etc.

    But I have one more requirement, Can we show that data in User’s profile as well? I mean here: Dashboard > All users > “X” user > edit profile > show data somewhere there.

    Please let me know,
    Thank you

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Dina S.

    (@themehighsupport)

    You can achieve your requirement by adding the below code snippet in your child theme’s functions.php file.

    // Update custom user meta during checkout
    function update_custom_user_meta_on_checkout($order_id) {
        $user_id = get_current_user_id(); // Get the current user ID
        $fields_array = array('test_field_1', 'test_field_2');
    
        if ($user_id) {
            // Get the value from the checkout field
            $cart_items = WC()->cart->get_cart();
    
            // Extract details from each cart item
            $cart_item_details = array();
            foreach ($cart_items as $cart_item_key => $cart_item) {
                $options = isset($cart_item['thwepof_options']) ? $cart_item['thwepof_options'] : array();
                foreach ($options as $option => $value) {
                    if (in_array($option, $fields_array) && isset($value['value'])) {
                        update_user_meta( $user_id, $option, $value['value'] );
                    }
                }
            }
        }
    }
    add_action('woocommerce_checkout_update_user_meta', 'update_custom_user_meta_on_checkout');
    
    
    // Add custom field to user profile
    function add_custom_user_profile_field($user) {
        ?>
        <h3><?php esc_html_e('Custom Information', 'woo-extra-product-options'); ?></h3>
        <table class="form-table">
            <tr>
                <th><label for="custom_info"><?php esc_html_e('Test field 1', 'woo-extra-product-options'); ?></label></th>
                <td>
                    <p class="description"><?php echo esc_attr(get_user_meta($user->ID, 'test_field_1', true)); ?></p>
                </td>
            </tr>
    
            <tr>
                <th><label for="custom_info"><?php esc_html_e('Test field 2', 'woo-extra-product-options'); ?></label></th>
                <td>
                    <p class="description"><?php echo esc_attr(get_user_meta($user->ID, 'test_field_2', true)); ?></p>
                </td>
            </tr>
        </table>
        <?php
    }
    add_action('show_user_profile', 'add_custom_user_profile_field');

    In the above code, please replace test_field_1 and test_field_2 with your field names.

    Thank you!

Viewing 1 replies (of 1 total)
  • The topic ‘Need help showing submitted data on User’s profile’ is closed to new replies.