Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce: Print product custom field value in customer email WordPress

  • SOLVED

I'm looking for a way to print out the value of a product's custom field in the "order processing" email that gets sent out to the customer after they complete payment.

In my case the value of the product's custom field is a url - a link to a questionnaire the customer need to fill-out after they pay. Each product in the store has its "questionnaire" custom field set with a different value(url).

I've added the following line of code on the email template. It works as far as returning the value of the custom field called "questionnaire" of product with id of 497.

<?php echo get_post_meta(497, 'questionnaire', true); ?>

So with that I do get the right value printed on the email - except it will always only print out the value of the custom field of product id 497.

I need is a bit of code that will conditionally output the value contained in each product's custom field if that product was bought in the store. Assuming only 1 product at a time can be bought.

Something like this:


<?php
if ( The product bought had an ID of 497 ) {
echo get_post_meta(497, 'questionnaire', true);
}
elseif ( The product bought had an ID of 498 ) {
echo get_post_meta(498, 'questionnaire', true);
}
elseif ( The product bought had an ID of 499 ) {
echo get_post_meta(499, 'questionnaire', true);
}
...and so on for 10 products.
?>


I'm sure this is straight forward - I've just hit a brick wall!

Thank you for looking.

Answers (2)

2013-11-12

phppoet answers:

use this type of code

<?php
global $post,$product;
$id=get_the_ID();
echo get_post_meta($id, 'questionnaire', true);

?>


It will echo questionnaire custum fild value of any post here
regards


elgringo comments:

Thank you phppoet - I've got this now:


<?php
global $post,$product;
$id=get_the_ID();

if ($id=297) {
echo get_post_meta(297, 'questionnaire', true);
}
elseif ($id=287) {
echo get_post_meta(287, 'questionnaire', true);
}
elseif ($id=286) {
echo get_post_meta(286, 'questionnaire', true);
}
elseif ($id=285) {
echo get_post_meta(285, 'questionnaire', true);
}
elseif ($id=284) {
echo get_post_meta(284, 'questionnaire', true);
}
elseif ($id=267) {
echo get_post_meta(267, 'questionnaire', true);
}
?>


Unfortunately still not working - returns value for id 297 every time.

Where have I gone wrong?


phppoet comments:

sorry it wont be that easy . you will need to edit each payment gateway url so that they send post id with url (using GET or POST method) and that payment gateway will send that post id into there referal url which we can use .

i am trying . i will post here if i get workable solution . i have done something similar in past where i sent post url(http://domain.com/?page_id=10) as item name to paypal action url where they send back into there referal url which i printed on a page .

regards


elgringo comments:

Right I see - what about a conditional statement that checks for product name rather than id?

If the product name equals to some string then print out its custom field value.


phppoet comments:

But how will you get product id from product name ? That would be more difficult i think .

Best way would be to send product id instead of product title which you can easily get as

<?php if (isset($_POST['item_name'])) {
$id=$_POST['item_name'];
}


if ($id=297) {

echo get_post_meta(297, 'questionnaire', true);

}

elseif ($id=287) {

echo get_post_meta(287, 'questionnaire', true);

}

elseif ($id=286) {

echo get_post_meta(286, 'questionnaire', true);

}

elseif ($id=285) {

echo get_post_meta(285, 'questionnaire', true);

}

elseif ($id=284) {

echo get_post_meta(284, 'questionnaire', true);

}

elseif ($id=267) {

echo get_post_meta(267, 'questionnaire', true);

}

?>


phppoet comments:

you can easily print item_name from paypal refering url using $_POST['item_name']

to send product id in place of item name you need to make smalls changes to paypal payment gateway class .

open class-wc-gateway-paypal.php and on line 42 find this code

$this->title = $this->get_option( 'title' );

abd replace it with


global $post,product;
$this->title = get_the_ID();


this is a sample change for paypal only . it may or may not apply this way for other gateways .

regards


elgringo comments:

Thanks phppoet but adding that line to class-wc-gateway-paypal.php messes things up when adding orders from the dashboard. i.e. The add order page returns almost blank.

I mean I don't really need the product id - wouldn't it suffice to check against product name and write the if statement checking for item_name?


phppoet comments:

yes . that would be more simple .

<?php if (isset($_POST['item_name'])) {

$producttitle=$_POST['item_name'];

}





if ($producttitle == "xyz computer") {



echo get_post_meta(297, 'questionnaire', true);



}



elseif ($producttitle == "abc computer") {



echo get_post_meta(287, 'questionnaire', true);



}


?>


it can be done this way . you need to make conditional statement with exact product title . It will work if you change product titles .


phppoet comments:

make sure to add

global $post,product;

before the previous code .

full code here http://pastebin.com/gnGXuc3q


elgringo comments:

Thanks.

OK - so I have this:


<?php if (isset($_POST['item_name'])) {
$producttitle=$_POST['item_name'];
}
global $post,$product;
if ($producttitle == "Product Name 1") {
echo get_post_meta(297, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 2") {
echo get_post_meta(287, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 3") {
echo get_post_meta(286, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 4") {
echo get_post_meta(285, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 5") {
echo get_post_meta(284, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 6") {
echo get_post_meta(267, 'questionnaire', true);
}
?>


But still nothing gets printed on the email.

Any ideas?


phppoet comments:

use echo $producttitle; before global $post,$product; so you get exact item name for bought product then try if statement with that .

regards


elgringo comments:

Thanks - I've got this now - but still nothing:



<?php if (isset($_POST['item_name'])) {
$producttitle=$_POST['item_name'];
}
echo $producttitle;
global $post,$product;
if ($producttitle == "Product Name 1") {
echo get_post_meta(297, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 2") {
echo get_post_meta(287, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 3") {
echo get_post_meta(286, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 4") {
echo get_post_meta(285, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 5") {
echo get_post_meta(284, 'questionnaire', true);
}
elseif ($producttitle == "Product Name 6") {
echo get_post_meta(267, 'questionnaire', true);
}
?>


phppoet comments:

did you get item name in email ?


elgringo comments:

No - I'm afraid not - the item name doesn't get printed either.

2013-11-12

Arnav Joy answers:

can you share full email template please.


Arnav Joy comments:

i think in the email template , you can get product id using following variable.

$_product->id

so you can use following to get the meta field's value:-

echo get_post_meta($_product->id, 'questionnaire', true);

try this or share your full email template file with your code there


elgringo comments:

Thanks Arnav - that doesn't return anything I'm afraid.

Here's the complete email template:


<?php
/**
* Customer processing order email
*
* @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); ?>

<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>


<h3>QUESTIONNAIRE LINK GOES HERE</h3>


<?php do_action('woocommerce_email_before_order_table', $order, false); ?>

<h2><?php echo __( 'Order:', 'woocommerce' ) . ' ' . $order->get_order_number(); ?></h2>

<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">
<thead>
<tr>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php echo $order->email_order_items_table( $order->is_download_permitted(), true, ($order->status=='processing') ? true : false ); ?>
</tbody>
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
?><tr>
<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>
<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>
</tr><?php
}
}
?>
</tfoot>
</table>

<?php do_action('woocommerce_email_after_order_table', $order, false); ?>

<?php do_action( 'woocommerce_email_order_meta', $order, false ); ?>

<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>

<?php if ($order->billing_email) : ?>
<p><strong><?php _e( 'Email:', 'woocommerce' ); ?></strong> <?php echo $order->billing_email; ?></p>
<?php endif; ?>
<?php if ($order->billing_phone) : ?>
<p><strong><?php _e( 'Tel:', 'woocommerce' ); ?></strong> <?php echo $order->billing_phone; ?></p>
<?php endif; ?>

<?php woocommerce_get_template('emails/email-addresses.php', array( 'order' => $order )); ?>

<?php do_action('woocommerce_email_footer'); ?>


Arnav Joy comments:

please try this

<?php

/**

* Customer processing order email

*

* @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); ?>



<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>


<?php

$order_items = $order->get_items();
foreach ( $order_items as $item )
$product_id = $item['product_id'];
?>


<h3><?php echo echo get_post_meta($product_id, 'questionnaire', true); ?></h3>





<?php do_action('woocommerce_email_before_order_table', $order, false); ?>



<h2><?php echo __( 'Order:', 'woocommerce' ) . ' ' . $order->get_order_number(); ?></h2>



<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">

<thead>

<tr>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>

</tr>

</thead>

<tbody>

<?php echo $order->email_order_items_table( $order->is_download_permitted(), true, ($order->status=='processing') ? true : false ); ?>

</tbody>

<tfoot>

<?php

if ( $totals = $order->get_order_item_totals() ) {

$i = 0;

foreach ( $totals as $total ) {

$i++;

?><tr>

<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>

<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>

</tr><?php

}

}

?>

</tfoot>

</table>



<?php do_action('woocommerce_email_after_order_table', $order, false); ?>



<?php do_action( 'woocommerce_email_order_meta', $order, false ); ?>



<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>



<?php if ($order->billing_email) : ?>

<p><?php _e( 'Email:', 'woocommerce' ); ?> <?php echo $order->billing_email; ?></p>

<?php endif; ?>

<?php if ($order->billing_phone) : ?>

<p><?php _e( 'Tel:', 'woocommerce' ); ?> <?php echo $order->billing_phone; ?></p>

<?php endif; ?>



<?php woocommerce_get_template('emails/email-addresses.php', array( 'order' => $order )); ?>



<?php do_action('woocommerce_email_footer'); ?>


Arnav Joy comments:

or try this

<?php

/**

* Customer processing order email

*

* @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); ?>



<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>


<?php

$order_items = $order->get_items();
foreach ( $order_items as $item )
$product_id = $item['product_id'];
?>


<h3><?php echo get_post_meta($product_id, 'questionnaire', true); ?></h3>





<?php do_action('woocommerce_email_before_order_table', $order, false); ?>



<h2><?php echo __( 'Order:', 'woocommerce' ) . ' ' . $order->get_order_number(); ?></h2>



<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">

<thead>

<tr>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>

</tr>

</thead>

<tbody>

<?php echo $order->email_order_items_table( $order->is_download_permitted(), true, ($order->status=='processing') ? true : false ); ?>

</tbody>

<tfoot>

<?php

if ( $totals = $order->get_order_item_totals() ) {

$i = 0;

foreach ( $totals as $total ) {

$i++;

?><tr>

<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>

<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>

</tr><?php

}

}

?>

</tfoot>

</table>



<?php do_action('woocommerce_email_after_order_table', $order, false); ?>



<?php do_action( 'woocommerce_email_order_meta', $order, false ); ?>



<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>



<?php if ($order->billing_email) : ?>

<p><?php _e( 'Email:', 'woocommerce' ); ?> <?php echo $order->billing_email; ?></p>

<?php endif; ?>

<?php if ($order->billing_phone) : ?>

<p><?php _e( 'Tel:', 'woocommerce' ); ?> <?php echo $order->billing_phone; ?></p>

<?php endif; ?>



<?php woocommerce_get_template('emails/email-addresses.php', array( 'order' => $order )); ?>



<?php do_action('woocommerce_email_footer'); ?>


Arnav Joy comments:

and if you have multiple products in your cart then you can use this

<?php

/**

* Customer processing order email

*

* @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); ?>



<p><?php _e( "Your order has been received and is now being processed. Your order details are shown below for your reference:", 'woocommerce' ); ?></p>


<?php

$order_items = $order->get_items();
foreach ( $order_items as $item ) {
$product_id = $item['product_id'];
?>


<h3><?php echo get_post_meta($product_id, 'questionnaire', true); ?></h3>

<?php } ?>



<?php do_action('woocommerce_email_before_order_table', $order, false); ?>



<h2><?php echo __( 'Order:', 'woocommerce' ) . ' ' . $order->get_order_number(); ?></h2>



<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee;" border="1" bordercolor="#eee">

<thead>

<tr>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Product', 'woocommerce' ); ?></th>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>

<th scope="col" style="text-align:left; border: 1px solid #eee;"><?php _e( 'Price', 'woocommerce' ); ?></th>

</tr>

</thead>

<tbody>

<?php echo $order->email_order_items_table( $order->is_download_permitted(), true, ($order->status=='processing') ? true : false ); ?>

</tbody>

<tfoot>

<?php

if ( $totals = $order->get_order_item_totals() ) {

$i = 0;

foreach ( $totals as $total ) {

$i++;

?><tr>

<th scope="row" colspan="2" style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['label']; ?></th>

<td style="text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo 'border-top-width: 4px;'; ?>"><?php echo $total['value']; ?></td>

</tr><?php

}

}

?>

</tfoot>

</table>



<?php do_action('woocommerce_email_after_order_table', $order, false); ?>



<?php do_action( 'woocommerce_email_order_meta', $order, false ); ?>



<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>



<?php if ($order->billing_email) : ?>

<p><?php _e( 'Email:', 'woocommerce' ); ?> <?php echo $order->billing_email; ?></p>

<?php endif; ?>

<?php if ($order->billing_phone) : ?>

<p><?php _e( 'Tel:', 'woocommerce' ); ?> <?php echo $order->billing_phone; ?></p>

<?php endif; ?>



<?php woocommerce_get_template('emails/email-addresses.php', array( 'order' => $order )); ?>



<?php do_action('woocommerce_email_footer'); ?>


elgringo comments:

Arnav thank you!

That did it beautifully - when more than one product is added to cart all the corresponding questionnaire url's print out in the email.

This is the final snippet I added to the email template:


<?php
$order_items = $order->get_items();
foreach ( $order_items as $item ) {
$product_id = $item['product_id'];
?>

<h3><?php echo get_post_meta($product_id, 'questionnaire', true); ?></h3>

<?php } ?>



Thanks again!