Я добавил в настраиваемые поля на странице оформления заказа с помощью диспетчера оформления заказа YITH billing_customfield1 и billing_customfield2. Мне нужно показать эти поля в электронном письме, отправленном woocommerce.
Таким образом, поля из кассы отображаются в шаблоне электронной почты:
<?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; ?>
Мне также нужно показать новые 2 настраиваемых поля, но нет метода для
$order->get_customfield1()






Начиная с Woocommerce 3, существует метод, унаследованный от класса WC_Data, который является get_meta () и должен использоваться для объекта экземпляра WC_Order$order.
Most of the time, Order custom fields are registered in database with a meta key starting with an underscore in
wp_postmetatable.
So I will use:_billing_customfield1and_billing_customfield2
Вот ваш обновленный код:
<?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') );
}
?>
Он должен работать, если слагами настраиваемых полей являются _billing_customfield1 и _billing_customfield2.
Вам необходимо проверить в своей базе данных в таблице wp_postmeta, что правильные meta_key - это _billing_customfield1 и _billing_customfield2…
В противном случае замените их на нужные.
Вы также можете использовать функцию WordPress get_post_meta(), которой нужен идентификатор заказа, который вы можете иметь с $order->get_id(), например:
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 ) );
}