• Resolved wpepro

    (@wpepro)


    I’ve got a pod named “organisation” and in it, I have a relationship (Multi Select) field “org_all_orders” which links to Woocommerce orders (custom post type “shop_order”). I’ve extended the shop_order POD to include the relationship field and everything works fine … except … the title for “shop_order” doesn’t include the order number, which is very important, so I’ve changed the Display Field in Select List to @id. Then to avoid accidentally adding the wrong order numbers I have the following code to make the labels more descriptive (it has the order number, firstname, lastname date and total) in the organisation pod:

    It works fine upon selecting the order number, like so:
    https://imgur.com/j6awa3Y

    And before saving, everything looks good:
    https://imgur.com/UsBE2by

    But after saving it just reverts back to the @id in the UI:
    https://imgur.com/aN1z69c


    This is same on both the front and back ends. Am I missing something really obvious? I’ve tried different filter priorities without much success. Any help is appreciated ??

    add_filter('pods_field_pick_data_ajax_items', 'show_first_and_lastname_in_pick_field', 1, 6);
    add_filter('pods_field_pick_data', 'show_first_and_lastname_in_pick_field', 1, 6);
    add_filter('pods_form_ui_field_pick_data', 'show_first_and_lastname_in_pick_field', 1, 6);
    
    function show_first_and_lastname_in_pick_field($items, $name, $value, $options, $pod, $id) {
        if ("org_all_orders" == $name) {
            foreach ($items as $key => &$data) {
                if ($data['id']) {
                    $order_id = $data['id'];
                    $order_meta = get_post_meta($order_id);
                    $first_name = get_post_meta($order_id, '_billing_first_name', true);
                    $last_name = get_post_meta($order_id, '_billing_last_name', true);
                    $date = get_post_meta($order_id, '_wcpdf_invoice_date_formatted', true);
                    $full_name = $first_name . ' ' . $last_name;
                    $order_total = "$" . get_post_meta($order_id, '_order_total', true);
    				$label = $order_id . ' ' . $full_name . ' ' . $date . ' - ' . $order_total;
                    $data['text'] = $label;
                    $data['name'] = $label;
                }
            }
        }
        return $items;
    }
    
    • This topic was modified 1 year, 4 months ago by wpepro.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter wpepro

    (@wpepro)

    For anyone else stuck/interested, I figured it out. Two completely different methods, but watch out for the difference between “your_fieldname”, “pods_meta_your_fieldname” and “pods_field_your_fieldname”:

    add_filter('pods_field_pick_data_ajax_items', 'labels_in_pick_field_ajax', 1, 6);
    add_filter('pods_field_pick_data', 'labels_in_pick_field_data', 1, 6);
    
    function labels_in_pick_field_ajax($items, $name, $value, $options, $pod, $id) {
        if ("org_all_orders" == $name) {
            foreach ($items as $key => &$data) {
                if ($data['id']) {
                    $data['text'] = get_woo_data_label($data['id']);
                    $data['name'] = $data['text'];
                }
            }
        }
        return $items;
    }
    
    
    function labels_in_pick_field_data($items, $name, $value, $options, $pod, $id) {
    	
    	// pods_meta_ prefix for Pods backend, pods_field_ prefix for front-facing Pods form
        if ("pods_meta_org_all_orders" === $name ||	 "pods_field_org_all_orders" === $name ) {
            if (is_array($items) && !empty($items)) {
                foreach ($items as $key => $item) {
                    if (!is_null($item)) {
                        $items[$key] = get_woo_data_label($items[$key]);
                    }
                }
            }
        }
        
        return $items;
    }
    
    function get_woo_data_label($order_id) {
    	// return whatever you like :) 
    	$meta_values = get_post_custom($order_id);
    
    	$first_name = isset($meta_values['_billing_first_name']) ? $meta_values['_billing_first_name'][0] : '';
    	$last_name = isset($meta_values['_billing_last_name']) ? $meta_values['_billing_last_name'][0] : '';
    	$date = isset($meta_values['_wcpdf_invoice_date_formatted']) ? $meta_values['_wcpdf_invoice_date_formatted'][0] : '';
    	$order_total = isset($meta_values['_order_total']) ? '$' . $meta_values['_order_total'][0] : '';
        $full_name = $first_name . ' ' . $last_name;
    
    	return $order_id . ' ' . $full_name . ' ' . $date . ' - ' . $order_total;
    }
    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @wpepro

    Thank you for sharing this solution! I’ve converted your solution into a doc snippet for future reference: https://docs.pods.io/code-snippets/modify-relationship-select-field-labels/

    Single question on your labels_in_pick_field_data function.

    I notice you checked for ! is_null( $item ). Can you elaborate here?
    From what I know this should return one of two options:

    1: An array of key => label (test) values.
    2: An array of indexes with arrays containing a text and an id key. (In admin ajax context only).

    I’ve modified the snippet to support both but let me know if I’ve mistaken!

    Cheers, Jory

    Thread Starter wpepro

    (@wpepro)

    Ah yes, checking for null is just force of habit from me, but you’re right – you can omit this without any issues ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pick data labels not quite working’ is closed to new replies.