Skip Navigation

[Resolved] using types_render_field in woocommerce email template with Product custom field

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to render checkboxes filed labels in a WooCommerce notification, he was only getting values.

Solution:
Try the following code:

<?php echo types_render_field( '_child-1-gender', array( 'item' => $product_id )); ?>

Notice that I used the field slug without the "wpcf-" prefix. And that I used "item" instead of "id" on the second argument.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by Joe H. 4 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#1842963

Tell us what you are trying to do?
I am in the process of customizing my woocommerce customer-completed-order.php email template by replacing the default email template with my own in my child theme. I've managed to write a function in functions.php that injects a table containing data from the Product rather than the Order.

Its working except for 2 issues:

1. I have been unable to successfully render the Display text rather than the value of the custom field:

add_action( 'woocommerce_email_after_order_table', 'jh_add_content_specific_email', 20, 4 );
  
function jh_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {

    // Get the product ID of the current order and assign to variable $product_id 
      foreach ( $order->get_items() as $item ) {
            $product_id = $item->get_product_id();
    } ?>

    <h2>Your Sponsored Family Details</h2>

<!-- BEGIN Family Details Table -->
<div style="margin-bottom: 40px;">
	<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
		<thead>
			<tr>
				<th class="td" scope="col" style="">Child Gender</th>
				<th class="td" scope="col" style="">Child Age</th>
				<th class="td" scope="col" style="">Gift Suggestion</th>
			</tr>
		</thead>
		<tbody>
            <tr>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-1-gender', true ); ?></td>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-1-age', true ); ?></td>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-1-gift', true ); ?></td>
            </tr>
            <tr>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-2-gender', true ); ?></td>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-2-age', true ); ?></td>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-2-gift', true ); ?></td>
            </tr>
            <tr>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-3-gender', true ); ?></td>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-3-age', true ); ?></td>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-3-gift', true ); ?></td>
            </tr>
 .... etc etc etc...  up to 6 children
		</tbody>
		<tfoot>
		</tfoot>
	</table>
</div>
<!-- END Family Details Table -->

<?php }

In the first <td> what renders in the email is the raw value but I'd like it to be the Display Text instead.
I tried this but it did not work (Im sure its wrong):

<td><?php echo types_render_field( 'wpcf-_child-1-gender', array( 'id' => $product_id )); ?></td>

(My goal here is to communicate to the Sponsor that sponsored the Family the child information so that the Sponsor can buy the Christmas presents. This data is in Product custom fields, not in Order custom fields )

2. This table displays from 1 to 6 children, depending on how many children are in the Product. I'd like to only display rows of child data if that data exists rather than show all 6 rows even though 2 through 6 may be empty

The children are NOT a repeating field. I have 6 sets of sets of 3 fields each:
Child 1: age, gender, gift
Child 2: age, gender, gift
Child 3: age, gender, gift
etc
etc

So I need a conditional but I am not sure how to write the php for it to work in a function that renders in an email.

Is there any documentation that you are following?
Too many sources to list them all

What is the link to your site?
hidden link

thank you

#1843567

Hello and thank you for contacting the Toolset support.

You can wrap each row inside an if statement hidden link
Something similar to:

<?php 
$child1gender = get_post_meta( $product_id, 'wpcf-_child-1-gender', true );
if ( $child1gender ) {
?>
<tr>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-1-gender', true ); ?></td>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-1-age', true ); ?></td>
                <td><?php echo get_post_meta( $product_id, 'wpcf-_child-1-gift', true ); ?></td>
            </tr>

<?php } ?>

I hope this helps. Let me know if you have any questions.

#1847769

Sorry but this response doesnt really help. Can you please re-read my question, particularly issue #1?

"1. I have been unable to successfully render the Display text rather than the value of the custom field:

In the first <td> what renders in the email is the raw value but I'd like it to be the Display Text instead.
I tried this but it did not work (Im sure its wrong):

<td><?php echo types_render_field( 'wpcf-_child-1-gender', array( 'id' => $product_id )); ?></td>

(My goal here is to communicate to the Sponsor that sponsored the Family the child information so that the Sponsor can buy the Christmas presents. This data is in Product custom fields, not in Order custom fields )"

This response speaks to issue #2 (sort of) but I found another solution for that. I still need help with issue #1 please. Thank you

#1849687

My apologies for the late reply, but I did not work on this Wednesday.

Can you try the following code:

<?php echo types_render_field( '_child-1-gender', array( 'item' => $product_id )); ?>

Notice that I used the field slug without the "wpcf-" prefix. And that I used "item" instead of "id" on the second argument.

If this does not help, I'll need to take a copy of your website for local testing, and I'll need to know the steps to follow to produce this email. Let me know if that would be ok with you.

#1850295

That gave me enough to solve it.

My issue is resolved now. Thank you!