• Resolved ft981

    (@tomasp98)


    Hello,

    I have added a custom field to my invoice. I calculate this field in funcion.php and send it to the order. The problem comes with the mail that is sent to the client. This field is not displayed. However, it does show if I download the invoice directly from the order in woocommerce backoffice or from the plugin settings

    Thanks in advance

    Regards

    • This topic was modified 3 years, 1 month ago by ft981.
    • This topic was modified 3 years, 1 month ago by ft981.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Yordan Soares

    (@yordansoares)

    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 );   
       }   
    }
    Plugin Contributor Yordan Soares

    (@yordansoares)

    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'); ?>&nbsp;<?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>

    Plugin Contributor Yordan Soares

    (@yordansoares)

    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 );   
        }   
    }

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Issue with plugin’ is closed to new replies.