Reputation: 37
I'm trying to display multiple custom product fields on the checkout page. I've found the below code which works for one custom field but how can I add multiple custom fields to it?
add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
function display_custom_product_field_data( $cart_data, $cart_item ) {
// Define HERE your product custom field meta key <== <== <== <== <==
$meta_key = 'custom_time';
$product_id = $cart_item['product_id'];
$meta_value = get_post_meta( $product_id, $meta_key, true );
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( !empty($meta_value) ) {
$custom_items[] = array(
'key' => __('Time', 'woocommerce'),
'value' => $meta_value,
'display' => $meta_value,
);
}
return $custom_items;
}
Upvotes: 0
Views: 177
Reputation: 56
you can define $meta_keys as array
$meta_keys = array('custom_time','custom_time2'); // or more than tow
and other Field
$dictionary = array('custom_time'=>'Time' , 'custom_time2'=>'Date')
$product_id = $cart_item['product_id'];
foreach($meta_keys as $key=>$meta_key){
$meta_value = get_post_meta( $product_id, $meta_key, true );
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( !empty($meta_value) ) {
$custom_items[] = array(
'key' => __( $dictionary[$meta_key] , 'woocommerce'), //or user $meta_key
'value' => $meta_value,
'display' => $meta_value,
);
}
}
return $custom_items;
Upvotes: 1