Display custom order meta in order received page and notification emails
Display custom order meta in order received page and notification emails
I've found this question on how to display custom order meta into the order received page and into the notifications email of woocommerce. I've tried to do the same by refactoring the code but I'm unable to accomplish it correctly. In my case I have only three custom informations stored in product meta to display, how I need to fix the code?
// Display order custom meta data in Order received (thankyou) page
public function display_order_custom_meta( $order_id ) {
$fields_labels = array(
'selected_pdv',
'selected_date',
'selected_hour'
);
$order = wc_get_order( $order_id );
$count = 1;
// Loop through order items
foreach ( $order->get_items() as $item ){
// Loop through item quantity
for($i = 1; $i <= $item->get_quantity(); $i++ ) {
echo '<h6> '.$i . '</h6>';
echo '<table><tbody>';
// Loop through participants keys / labels pairs
foreach( $fields_labels as $label ){
$meta_key = $label;
echo '<tr><th>'.$label.':</th><td>'.$order->get_meta( $meta_key ).'</td></tr>';
}
//
echo '</tbody></table>';
}
$count++;
}
}
add_action('woocommerce_thankyou', array($this, 'display_order_custom_meta') )
According to provided code, to implement required functionality of displaying woocommerce order custom meta fields on "order received" page, woocomerce order emails, the following code can be checked:
add_action('woocommerce_thankyou', 'custom_woocommerce_thankyou');
function custom_woocommerce_thankyou( $order_id ) {
$fields_labels = array(
'selected_pdv',
'selected_date',
'selected_hour'
);
$order = wc_get_order( $order_id );
echo '<table><tbody>';
foreach( $fields_labels as $label ){
echo '<tr><th>'.$label.':</th><td>'.$order->get_meta( $label ).'</td></tr>';
}
echo '</tbody></table>';
}
add_action('woocommerce_email_order_details', 'custom_woocommerce_email_order_details', 10, 4 );
function custom_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
$fields_labels = array(
'selected_pdv',
'selected_date',
'selected_hour'
);
$html_output = '<div>
<table><tbody>';
foreach( $fields_labels as $label ){
$html_output .= '<tr><th>'.$label.':</th><td>'.$order->get_meta( $label ).'</td></tr>';
}
$html_output .= '</tbody></table></div>';
echo $html_output;
}
January 7, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
January 7, 2024