Display Choice Labels in Card & Order

Code

Filename: wcgfpa-use-choice-labels.php

<?php
/**
 * Gravity Wiz // WC GF Product Add-ons // Display Choice Labels in Card & Order
 * https://gravitywiz.com/
 */
add_filter( 'woocommerce_gforms_get_item_data', function( $data, $field ) {
	$data['display'] = gw_wcgfpa_get_choice_label( $data['display'], $field );
	return $data;
}, 10, 2);

add_filter( 'woocommerce_gforms_order_meta_value', function( $display_value, $field ) {
	return gw_wcgfpa_get_choice_label( $display_value, $field );
}, 10, 2 );

function gw_wcgfpa_get_choice_label( $value, $field ) {
	if ( ! empty( $field->choices ) ) {
		foreach ( $field->choices as $choice ) {
			if ( $choice['value'] == $value ) {
				$value = $choice['text'];
				break;
			}
		}
	}

	return $value;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.