Hi @tomasp98,
Could you please share the code snippet you are using to display the custom field on your PDF invoice?
Thread Starter
ft981
(@tomasp98)
Of course, here is the code snipet
Presupuesto: order_number(); ?>
Cliente: custom_field('billing_first_name'); ?> custom_field('billing_last_name'); ?> billing_email(); ?>
Fecha: invoice_date(); ?>
Delegacion: custom_field('billing_wcj_checkout_field_2'); ?>
The only field that is not showing me is the billing_wcj_checkout_field_2. I can see it in the page preview in the plugin, but it is not sent correctly when the invoice is sent to the mail
This is the function I use to assign the value
add_action( 'woocommerce_thankyou', 'bbloomer_alter_checkout_fields_after_order', 10, 1 );
function bbloomer_alter_checkout_fields_after_order( $order_id ) {
$order = wc_get_order( $order_id );
$delegacion = '';
// TARGET-ZIP-CODE
$zonaTest= array( '12564', '23453');
// User ZIP-CODE
$user_zip_zone = $order->get_shipping_postcode();
if(empty($user_shipping_postcode))
$user_zip_zone = $order->get_billing_postcode();
if ( in_array( $user_zip_zone, $zonaTest)) {
$delegacion = 'test test / test@test.com / test';
update_post_meta( $order_id, '_billing_wcj_checkout_field_2', $delegacion );
}
}
Thanks for providing those code snippets, @tomasp98.
However, I’m afraid the first one would return an error, unless you have edited it before paste it here.
Anyway, I realized that you’re using the meta key there without the underscore, that is, billing_wcj_checkout_field_2 instead of _billing_wcj_checkout_field_2
Please add the underscore before the meta key to check if the value is displaying in all contexts.
If this does not work, please send the complete code, so I can understand better what it’s happening.
Thread Starter
ft981
(@tomasp98)
I have tried but still, I can’t get the field to be displayed in the attachment. However, I do see the value in the woocommerce backoffice. I can also see it on the invoice preview. The problem comes when the automatic mail is sent with the attached file, it disappears there. The field contains value because I can see it in the order details. Could it be that it has something to do with the function? The mail is being executed before the function and that is why when the attachment is sent the value will be null. But I dont know if thats the problem
Here is the code of the invoice.php
table class="tabla100">
<tr>
<td>
<h3>Presupuesto previo (no vinculante)</h3>
</td>
<td>
[logo]
</td>
</tr>
<tr>
<td colspan="2" class="dataHeader">
<b>Presupuesto:</b> <?php $this->order_number(); ?><br>
<b>Cliente:</b> <?php $this->custom_field('billing_first_name'); ?> <?php $this->custom_field('billing_last_name'); ?>&nbps;<?php $this->billing_email(); ?><br>
<b>Fecha:</b> <?php $this->invoice_date(); ?><br>
<b>Delegacion:</b> <?php $this->custom_field('_billing_wcj_checkout_field_2'); ?>
</td>
</tr>
</table>
Thanks for providing more details, @tomasp98.
I can see now the reason why this is happening: You’re adding the custom value AFTER the email is sent. To solve this, you have to save the value for $delegacion, BEFORE the email is sent.
Try using the woocommerce_checkout_create_order action hook instead, like this:
add_action( 'woocommerce_checkout_create_order', 'bbloomer_alter_checkout_fields_after_order', 10, 2 );
function bbloomer_alter_checkout_fields_after_order( $order, $data ) {
$delegacion = '';
// TARGET-ZIP-CODES
$zonaTest = array( '12564', '23453');
// Customer ZIP-CODE
$customer_zip_zone = ! empty( $order->get_shipping_postcode() ) ? $order->get_shipping_postcode() : $order->get_billing_postcode();
if ( in_array( $customer_zip_zone, $zonaTest ) ) {
$delegacion = 'test test / test@test.com / test';
$order->update_meta_data( '_billing_wcj_checkout_field_2', $delegacion );
}
}