Giovanni Broegg
Giovanni Broegg

Reputation: 103

Add custom field variable in WooCommerce email

I have some custom variable for my WooCommerce user as:

billing_pin
billing_piva

etc...

When a new customer is created it receive an email of New Account and I have modified it.

For now my email is:

<?php
/**
 * Customer new account email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-new-account.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates/Emails
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

?>

<?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

    <p><?php printf( __( 'Il tuo account di Magazzino Perfetto è stato creato con successo. Da questo momento potrai iniziare ad usare tutti i nostri servizi per aumentare la produttività della tua azienda. <b>Il tuo nome utente è la tua email</b>')); ?></p>

<?php if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && $password_generated ) : ?>

    <p><?php printf( __( 'La tua password è stata generata automaticamente. La trovi di seguito: %s', 'woocommerce' ), '<strong>' . esc_html( $user_pass ) . '</strong>' ); ?></p>

<?php endif; ?>

    <p><?php printf( __( 'Per accedere al riepilogo ordine, fare upgrade, modificare la password, visualizzare la licenza e scaricare le fatture, puoi cliccare sul seguente link: %s.', 'woocommerce' ), make_clickable( esc_url( wc_get_page_permalink( 'myaccount' ) ) ) ); ?></p>

    <p><?php printf( __( 'Per iniziare subito a lavorare puoi accedere al <b>pannello di controllo</b> online all\'indirizzo <a href="https://www.magazzinoperfetto.cloud">https://www.magazzinoperfetto.cloud</a>.' )); ?></p>

    <p><?php printf( __( 'Se non l\'hai già fatto, <b>scarica subito la nostra APP</b> dal Play Store <a href="#">cliccando qui</a> ed accedi inserendo i tuoi dati di accesso ed il <b>seguente PIN provvisorio:</b> ' )); ?></p>

<?php do_action( 'woocommerce_email_footer', $email );

My question is: How do I do to insert the variable billing_pin in email?

Sorry for my bad English... I'm Italian.

Upvotes: 0

Views: 2054

Answers (2)

zawhtut
zawhtut

Reputation: 8541

You do not need to edit the template file. Just add the new varibale in functions.php file. The following will do.

add_filter( 'woocommerce_email_format_string' , 'add_pin_format_string', 10, 4 );
function add_pin_format_string( $string, $email ) {
    $placeholder = '{pin}'; // The corresponding placeholder to be used
    $order = $email->object; // Get the instance of the WC_Order Object
    //$value = $order->get_customer_note();// Get the value
    $value = "PN000001";
    // Return the clean replacement value string for "{delivery_date}" placeholder
    return str_replace( $placeholder, $value, $string );
}

Upvotes: 1

Neil K
Neil K

Reputation: 1328

You could use the get_user_meta() function to find the value and echo this into the email template.

<p>Billing Pin: <?php echo get_user_meta($order->user_id, 'billing_piva', true); ?></p>

Replace the billing_piva field with whatever the key actually is. I'm not sure what your key is exactly.

Upvotes: 0

Related Questions