Skip Navigation

[Resolved] Woocommerce email placeholders from custom fields

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 8.71 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 16 replies, has 2 voices.

Last updated by Minesh 10 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2676991

Tell us what you are trying to do?
I've created some custom fields which is used to add information to Woocommerce products. Now I want the info from some of these fields to be showned in the order confirmation email. I belive the correct way for this is to create custom placeholders for these fields.

I came across following guide,which describe how to create custom placeholders:
hidden link

But, this guide only show how to create placeholders from woocommerce default product information. Can someone point me in the correct direction how to create placeholder from product information made out of Toolset custom fields?

BR
Tobias

#2677003

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

With the reference link you shared - there is a link " WooCommerce order object here" when you click on that link you will be landed on the following page that shows how you can loop through the products added to the order:
- hidden link

I see the following code available with the above link to loop through the products added to order:

// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id();
   $variation_id = $item->get_variation_id();
   $product = $item->get_product(); // see link above to get $product info
   $product_name = $item->get_name();
   $quantity = $item->get_quantity();
   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();
   $tax_class = $item->get_tax_class();
   $tax_status = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $somemeta = $item->get_meta( '_whatever', true );
   $item_type = $item->get_type(); // e.g. "line_item", "fee"
}
 

Once you have product ID - you can use the native WrodPress function get_post_meta(), for instance:
If you have created the custom field with slug "bookname" then it will be stored in the postmeta table with the key "wpcf-bookname". To get its value, you can use:

   $product_id = $item->get_product_id();
$book_name  = get_post_meta($product_id, 'wpcf-bookname', true);

I hope this helps.

More info:
- https://toolset.com/documentation/customizing-sites-using-php/functions/
- https://toolset.com/documentation/customizing-sites-using-php/loading-wordpress-content-database/

#2677011

Hi Minesh

I'not that in to php so you have excuse me.

The first code is to get the Product ID from the Order, right? What changes do I have to make in it to fit my me?

The second code is to get the value out of the custom field, what changes to I have to make? Can add more then one field , e.g.:
$product_id = $item->get_product_id();
$book_name = get_post_meta($product_id, 'wpcf-bookname, ', true);
$dvd_name = get_post_meta($product_id, 'wpcf-dvd', true);
$video_name = get_post_meta($product_id, 'wpcf-video', true);

Is it the slug for the custom field I then use as placeholder e.g. {wpch-bookname}?

BR

Tobias

#2677023

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

how many order items you will have in the order?

When there will be multiple products added to single order - how you want to display the placeholder and for what custom fields you want to display the placeholder?

#2677030

Hi Minesh

There will never be more than one product to a order.

There will be 3 custom fields I want to display after purchase in the order confirmation mail and perheps on the order confirmation page.

BR
Tobias

#2677031

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should try to add the following code to your current theme's functions.php file or "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

function func_add_custom_email_placeholders( $string, $email ) {
    // Get WC_Order object from email
    $order = $email->object;

/ Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id();

   $dvd_name = get_post_meta($product_id, 'wpcf-dvd', true);
  $video_name = get_post_meta($product_id, 'wpcf-video', true);
}
 
    
    // Add new placeholders
    $new_placeholders = array(
        '{_dvd_name}'       => $dvd_name,
        '{_video_name}'      => $video_name, 
       
    );
    // return the string with new placeholder replacements
    return str_replace( array_keys( $new_placeholders ), array_values( $new_placeholders ), $string );
}
add_filter( 'woocommerce_email_format_string' , 'func_add_custom_email_placeholders', 20, 2 );

Where:
- You can modify the code as required.

#2677053

Hi Minesh

Thank you Minesh

Ok, I think I get it. Placeholder will be {_dvd_name}. I will try the code. It might take a while, but I get back.

BR
Tobias

#2677093

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok fine - it should work.

#2677272

Hi Minesh

I got a critical error when I tried the code above.
I changed the slugs to the ones of my custom fields and post it in the themes functions.php.

The only difference I can see between your code and the one from the link is that you used the $product_id instead of $product .

Does this make any difference or do you any other idea what caused the error?

#2677286

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share the code you are using currently?

#2677288

Hi

Sure here it is:

function func_add_custom_email_placeholders( $string, $email ) {
// Get WC_Order object from email
$order = $email->object;

/ Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();

$for-och-efternamn_name = get_post_meta($product_id, 'wpcf-for-och-efternamn', true);
$e-post-uppdragsgivare_name = get_post_meta($product_id, 'wpcf-e-post-uppdragsgivare', true);
$ telefonnummer-uppdragsgivare_name = get_post_meta($product_id, 'wpcf-telefonnummer-uppdragsgivare', true);

}

// Add new placeholders
$new_placeholders = array(
'{_for-och-efternamn_name}' => $for-och-efternamn_name,
'{_e-post-uppdragsgivare_name}' => $e-post-uppdragsgivare_name,
'{_telefonnummer-uppdragsgivare_name}' => $telefonnummer-uppdragsgivare_name,

);
// return the string with new placeholder replacements
return str_replace( array_keys( $new_placeholders ), array_values( $new_placeholders ), $string );
}
add_filter( 'woocommerce_email_format_string' , 'func_add_custom_email_placeholders', 20, 2 );

#2677294

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please try to use the following code:

function func_add_custom_email_placeholders( $string, $email ) {
////// Get WC_Order object from email
$order = $email->object;

//// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();

$for_och_efternamn_name = get_post_meta($product_id, 'wpcf-for-och-efternamn', true);
$e_post_uppdragsgivare_name = get_post_meta($product_id, 'wpcf-e-post-uppdragsgivare', true);
$telefonnummer_uppdragsgivare_name = get_post_meta($product_id, 'wpcf-telefonnummer-uppdragsgivare', true);

}

// Add new placeholders
$new_placeholders = array(
'{_for_och_efternamn_name}' => $for_och_efternamn_name,
'{_e_post_uppdragsgivare_name}' => $e_post_uppdragsgivare_name,
'{_telefonnummer_uppdragsgivare_name}' => $telefonnummer_uppdragsgivare_name,

);
// return the string with new placeholder replacements
return str_replace( array_keys( $new_placeholders ), array_values( $new_placeholders ), $string );
}
add_filter( 'woocommerce_email_format_string' , 'func_add_custom_email_placeholders', 20, 2 );

As you can see I've changed the variable names as:

$for_och_efternamn_name = get_post_meta($product_id, 'wpcf-for-och-efternamn', true);
$e_post_uppdragsgivare_name = get_post_meta($product_id, 'wpcf-e-post-uppdragsgivare', true);
$telefonnummer_uppdragsgivare_name = get_post_meta($product_id, 'wpcf-telefonnummer-uppdragsgivare', true);

As well as changed the placeholder names:

// Add new placeholders
$new_placeholders = array(
'{_for_och_efternamn_name}' => $for_och_efternamn_name,
'{_e_post_uppdragsgivare_name}' => $e_post_uppdragsgivare_name,
'{_telefonnummer_uppdragsgivare_name}' => $telefonnummer_uppdragsgivare_name,

);
#2677380

Hi Minesh

It's still someting wrong with the code. I get a Critical Error for the whole website when I past the code in my function.php.

Can you check what causing it?

#2677519

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2677723

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When I try to access the wp-admin to login to admin panel:

I can see its showing the following messages:

SK
Z dôvodu ochrany hostingov pred opakujúcimi útokmi sme boli nútení pre vybrané krajiny obmedziť prístup do administratívneho rozhrania webových stránok. Pokiaľ potrebujete s administráciou pracovať zo zahraničia, prosím prihláste sa do Webadminu, prejdite do správy domény a v sekcii "CMS - redakčné systémy" kliknite na "Vypnúť geoip filter".

EN
Due to protection of web servers from repeated attacks, we were forced to restrict access to administrative interface of web pages to selected countries. If you are currently in a foreign country, please sign in to WebAdmin, proceed to your domain management and disable this GeoIP filter in CMS section.

CZ
Z důvodu ochrany webhostingů před opakujícími se útoky jsme byli nuceni pro vybrané země omezit přístup do administračního rozhraní webových stránek. Pokud potřebujete s administrací pracovat ze zahraničí, prosíme, přihlašte se do WebAdminu, přejděte do správy domény a v sekci "CMS - redakční systémy" klikněte na "Vypnout geoip filtr".

HU
Szervereink védelme érdekében az ismételt támadások ellen kénytelenek voltunk blokkolni a "CMS - tartalomkezelő rendszerek" admin felületére szóló bejelentkezést bizonyos országokból. Részletes információkért keresse ügyfélszolgálatunkat a helpdesk@webonic.hu címen vagy a (06) 1 501 49 49-es telefonszámon. Köszönjük a megértést!

Can you please lift the restrictions and also tell me what is the problem URL and where I can see the filter hook code you added.

I have set the next reply to private which means only you and I have access to it.