Reputation: 295
I have added to custom fields on the checkout page using YITH checkout Manager billing_customfield1 and billing_customfield2. I need to show this fields on the email sent by woocommerce.
this is the way , the fields from checkout are being displayed on the email template:
<?php if ( $order->get_billing_first_name() ) : ?>
<br/><?php echo '<b>Nombre: </b>'.esc_html( $order->get_billing_first_name().' '.$order->get_billing_last_name() ); ?>
<?php endif; ?>
<?php if ( $order->get_billing_phone() ) : ?>
<br/><?php echo '<b>Teléfono: </b>'.esc_html( $order->get_billing_phone() ); ?>
<?php endif; ?>
<?php if ( $order->get_billing_company() ) : ?>
<br/><?php echo '<b>Nombre de la empresa: </b>'.esc_html( $order->get_billing_company() ); ?>
<?php endif; ?>
<?php if ( $order->get_billing_address_1() ) : ?>
<br/><?php echo '<b>Dirección: </b>'.esc_html( $order->get_billing_address_1() ); ?>
<?php endif; ?>
<?php if ( $order->get_billing_address_2() ) : ?>
<br/><?php echo '<b>Dirección 2: </b>'.esc_html( $order->get_billing_address_2() ); ?>
<?php endif; ?>
<?php if ( $order->get_billing_city() ) : ?>
<br/><?php echo '<b>Ciudad: </b>'.esc_html( $order->get_billing_city() ); ?>
<?php endif; ?>
I need to show the new 2 custom fields as well but there's no method for
$order->get_customfield1()
Upvotes: 1
Views: 362
Reputation: 254373
Since Woocommerce 3, there is a method inherited from WC_Data
Class which is get_meta() and that has to be used on the WC_Order
$order
instance object.
Most of the time, Order custom fields are registered in database with a meta key starting with an underscore in
wp_postmeta
table.
So I will use:_billing_customfield1
and_billing_customfield2
Here is your revisited code:
<?php
if ( $order->get_formatted_billing_full_name() ) {
echo '<br/><b>'.__("Nombre", "woocommerce") . ': </b>' . esc_html( $order->get_formatted_billing_full_name() );
}
if ( $order->get_billing_phone() ) {
echo '<br/><b>'.__("Teléfono", "woocommerce") . ': </b>' . esc_html( $order->get_billing_phone() );
}
if ( $order->get_billing_company() ) {
echo '<br/><b>'.__("Nombre de la empresa", "woocommerce") . ': </b>' . esc_html( $order->get_billing_company() );
}
if ( $order->get_billing_address_1() ) {
echo '<br/><b>'.__("Dirección", "woocommerce") . ': </b>' . esc_html( $order->get_billing_address_1() );
}
if ( $order->get_billing_address_2() ) {
echo '<br/><b>'.__("Dirección 2", "woocommerce") . ': </b>' . esc_html( $order->get_billing_address_2() );
}
if ( $order->get_billing_city() ) {
echo '<br/><b>'.__("Ciudad", "woocommerce") . ': </b>' . esc_html( $order->get_billing_city() );
}
if ( $order->get_meta('_billing_customfield1') ) {
echo '<br/><b>'.__("Campo personalizado 1", "woocommerce") . ': </b>' . esc_html( $order->get_meta('_billing_customfield1') );
}
if ( $order->get_meta('_billing_customfield2') ) {
echo '<br/><b>'.__("Campo personalizado 2", "woocommerce") . ': </b>' . esc_html( $order->get_meta('_billing_customfield2') );
}
?>
It should works if those custom fields slugs are _billing_customfield1
and _billing_customfield2
You should need to verify in your database under wp_postmeta
table that the correct meta_key
are _billing_customfield1
and _billing_customfield2
…
If not you will replace them with the right ones.
You can also use the WordPress get_post_meta()
function that needs the Order ID that you can have with $order->get_id()
like:
if ( get_post_meta( $order->get_id(), '_billing_customfield1', true ) ) {
echo '<br/><b>'.__("Campo personalizado 1", "woocommerce") . ': </b>' . esc_html( get_post_meta( $order->get_id(), '_billing_customfield1', true ) );
}
if ( get_post_meta( $order->get_id(), '_billing_customfield2', true ) ) {
echo '<br/><b>'.__("Campo personalizado 2", "woocommerce") . ': </b>' . esc_html( get_post_meta( $order->get_id(), '_billing_customfield2', true ) );
}
Upvotes: 1