Woo Order Custom Fields

Home » Snippets » Woo Order Custom Fields
0

Created with:

Visibility: 

public

Creator: Cristi

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/*
Plugin Name: Custom Order Fields
Description: Adds custom fields to WooCommerce order details and user dashboard
Version: 1.0
Author: Your Name
*/

// Add custom fields to the order details page in the admin
function wpturbo_add_custom_order_fields( $order ) {
    echo '<div class="order_custom_fields">';
    
    // Add custom field 1
    $field1_label = 'Field 1 Label';
    $field1_value = get_post_meta( $order->get_id(), '_custom_field1', true );
    if ( ! empty( $field1_value ) ) {
        echo '<p><strong>' . esc_html( $field1_label ) . ':</strong> ';
        echo '<a href="' . esc_url( $field1_value ) . '" target="_blank">' . esc_html( $field1_value ) . '</a></p>';
    }

    // Add custom field 2
    $field2_label = 'Field 2 Label';
    $field2_value = get_post_meta( $order->get_id(), '_custom_field2', true );
    if ( ! empty( $field2_value ) ) {
        echo '<p><strong>' . esc_html( $field2_label ) . ':</strong> ';
        echo '<a href="' . esc_url( $field2_value ) . '" target="_blank">' . esc_html( $field2_value ) . '</a></p>';
    }

    // Add more custom fields if needed

    echo '</div>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wpturbo_add_custom_order_fields', 10, 1 );

// Add custom fields to the order table on the user dashboard
function wpturbo_add_custom_order_columns( $columns ) {
    $columns['custom_field1'] = 'Field 1 Label';
    $columns['custom_field2'] = 'Field 2 Label';
    // Add more custom fields if needed
    return $columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'wpturbo_add_custom_order_columns' );

// Populate custom fields in the order table on the user dashboard
function wpturbo_populate_custom_order_columns( $order ) {
    $field1_value = get_post_meta( $order->get_id(), '_custom_field1', true );
    if ( ! empty( $field1_value ) ) {
        echo '<p><strong>' . esc_html( $field1_value ) . '</strong></p>';
    }

    $field2_value = get_post_meta( $order->get_id(), '_custom_field2', true );
    if ( ! empty( $field2_value ) ) {
        echo '<p><strong>' . esc_html( $field2_value ) . '</strong></p>';
    }

    // Add more custom fields if needed
}
add_action( 'woocommerce_my_account_my_orders_column_custom_field1', 'wpturbo_populate_custom_order_columns' );
add_action( 'woocommerce_my_account_my_orders_column_custom_field2', 'wpturbo_populate_custom_order_columns' );
				

Explanation:

This code creates a custom plugin that adds two custom fields to the WooCommerce order details in the admin area and the user dashboard.

In the wpturbo_add_custom_order_fields() function, we retrieve the values of the custom fields (_custom_field1 and _custom_field2) for a given order using the get_post_meta() function. We then check if the field has a value and display it along with the label in the order details section of the admin area. The value is displayed as a link that opens in a new tab.

In the wpturbo_add_custom_order_columns() function, we add the custom fields as columns to the order table on the user dashboard. We define the column labels (Field 1 Label and Field 2 Label) and return the updated columns array.

In the wpturbo_populate_custom_order_columns() function, we retrieve the values of the custom fields for each order and display them as the content of the respective columns in the order table on the user dashboard. The value is displayed as a label.

You can place this code in a new file named custom-order-fields.php inside a folder named custom-order-fields within the wp-content/plugins directory of your WordPress installation. Activate the plugin from the WordPress dashboard, and the custom fields will be added to the order details in the admin area and the order table on the user dashboard.

Please note that you should replace the field labels (Field 1 Label and Field 2 Label) with your preferred labels, and adjust the meta keys (_custom_field1 and _custom_field2) accordingly for your custom fields.

Register an account to save your snippets or go Pro to get more features.