• Resolved Jadav Sanjay

    (@jadavsanjay)


    Hi,

    I want to add few Fields on Checkout Page.
    I have added view custom code.
    ———————————————————————-

    add_filter(‘woocommerce_checkout_fields’, ‘custom_checkout_fields’, 999);

    function custom_checkout_fields($fields) {
    // Event Type
    $fields[‘billing’][‘event_type’] = array(
    ‘type’ => ‘text’,
    ‘label’ => (‘Event Type’, ‘woocommerce’), ‘placeholder’ => (‘e.g. Birthday, Wedding, Corporate’, ‘woocommerce’),
    ‘required’ => true,
    ‘class’ => array(‘form-row-wide’),
    ‘priority’ => 20,
    );

    return $fields;

    }

    // Save Custom Fields and File Upload
    add_action(‘woocommerce_checkout_update_order_meta’, ‘save_custom_checkout_fields’);
    function save_custom_checkout_fields($order_id) {
    $custom_fields = [
    ‘event_type’
    ];

    foreach ($custom_fields as $field) {
        if (!empty($_POST[$field])) {
            update_post_meta($order_id, '_' . $field, sanitize_text_field($_POST[$field]));
        }
    }

    }

    // Display Fields in Admin Panel
    add_action(‘woocommerce_admin_order_data_after_billing_address’, ‘display_custom_fields_in_admin’, 10, 1);
    function display_custom_fields_in_admin($order) {
    $custom_fields = [
    ‘event_type’ => ‘Event Type’
    ];

    foreach ($custom_fields as $key => $label) {
        $value = get_post_meta($order->get_id(), '_' . $key, true);
        if ($value) {
            echo '<p><strong>' . __($label, 'woocommerce') . ':</strong> ' . esc_html($value) . '</p>';
        }
    }

    }

    // Include Fields & File in Order Emails
    add_filter(‘woocommerce_email_order_meta_keys’, ‘custom_fields_in_emails’);
    function custom_fields_in_emails($keys) {
    $keys[] = ‘_event_type’;
    return $keys;
    }

    ———————————————————————————–

    When i Print my option in footer via hook

    add_action(‘wp_footer’, function() {

        if (is_checkout()) {

            echo ‘<script>console.log(“WooCommerce Checkout Fields:”, ‘ . json_encode(WC()->checkout->get_checkout_fields()) . ‘);</script>’;

        }

    });

    it is display on Object – please check attachment – https://prnt.sc/RBd-zVUsMqgc

    but when I show in checkout page this custom fields is not showing – https://prnt.sc/tDQ1j3yxsxYD

    please help me if you have idea how to display it

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Want to Display Custom Fields in checkout’ is closed to new replies.