Woocommerce - Print Product's Custom Field In Email - wordpress

In Woocommerce, I need to print a custom field on the Completed Order email.
The template is completely unique in my themes woocommerce/emails folder because the default template doesn't work for my needs.
As such I'm trying to display the following:
Title, Quantity and two custom fields
This needs to be included in the customer-completed-order.php email template but I am not certain what syntax is necessary to print it.
I found something by Googling but it doesn't work:
<?php foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
?>
<p>Class: <?php echo get_post_meta($_product->id,'name',true); ?></p>
<p>Date: <?php echo get_post_meta($_product->id,'date_text_field',true); ?></p>
<p>Time: <?php echo get_post_meta($_product->id,'time_text_field',true); ?></p>
<p>Number of Spaces: <?php echo get_post_meta($_product->id,'qty',true); ?></p>
<?php endforeach; ?>

Try adding to action hook,
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
OR
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
This could be used in following way:
add_action( 'woocommerce_order_item_name', 'action_custom_order_meta', 10, 3 );
OR
add_action( 'woocommerce_order_item_name', 'action_custon_order_meta', 10, 3 );
Following is the callback handler function.
function action_custom_order_meta($item_name, $item, $false){
$product_id = $item['product_id'];
$product_custom_date = get_post_meta($product_id, 'product_custom_date', true);
$product_custom_time = get_post_meta($product_id, 'product_custom_time', true);
$product_custom_meta = 'Date: ' . $product_custom_date . ' <br> Time: ' . $product_custom_time;
echo $product_custom_meta;
}
Change the custom field names to appropriate. You might also need to wrap the output according to the required markup for your custom email.
Hope this helps.

Related

Add product page title to WooCommerce product description heading and tab title

What I'm trying to achieve in WooCommerce is that on the single product page, the Description tab, I'm trying to add the product page title before the word Description.
Here is my current WooCommerce code:
defined( 'ABSPATH' ) || exit;
global $post;
$heading = apply_filters( 'woocommerce_product_description_heading', __( 'Description', 'woocommerce' ) ); ?>
<?php if ( $heading ) : ?>
<h2>PRODUCT PAGE TITLE HERE <?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>
<?php the_content(); ?>
The problem here, however, is that:
I have to overwrite the template file, is it possible via a hook?
The product title is not dynamic.
As a result, id like the tab to go from Description to BLACK NIKE SHOES Description
Example:
Any advice?
You can use the woocommerce_product_{$tab_key}_tab_title composite filter hook. Where $tab_key is in your case description
Use global $product and $product->get_name() to get the product title. This result can then be added to the existing string.
So you get:
function filter_woocommerce_product_description_tab_title( $title ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get title and append to existing string
$title = $product->get_name() . ' ' . $title;
}
return $title;
}
add_filter( 'woocommerce_product_description_tab_title', 'filter_woocommerce_product_description_tab_title', 10, 1 );
Optional: to change the WooCommerce product description heading instead, use the woocommerce_product_description_heading filter hook:
function filter_woocommerce_product_description_heading( $heading ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get title and append to existing string
$heading = $product->get_name() . ' ' . $heading;
}
return $heading;
}
add_filter( 'woocommerce_product_description_heading', 'filter_woocommerce_product_description_heading', 10, 1 );

Get a Value in the Author Page from a Custom Taxonomy

I have created a Custom Taxonomy called nationality, then I added it to let the user chose his/her nationality through a Custom Account Edit Form using ACF.
I am trying to get the Tax value into the Author.php page but it returns as Array ,
This is my code which I am using:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = wp_get_post_terms($post->ID, 'nationality' , $curauth);
if ($terms){
$out = array(
'name'=>'<span>Nationality: </span>'
);
foreach ($terms as $term)
{
$out[] = '<a " ' . ' href="' . esc_url(get_term_link( $term->slug, 'nationality')) .'">' .$term->name .'</a>' ;}
echo join( ' ', $out );
}
?>
I have also tried the following code:
<?php
$nationality = get_field('nationality', $curauth);
$nationality = get_field_object('nationality', $curauth);
echo $nationality['value'];
?>
still giving me an Array.
The Field type is select and Return Value is set to Term Object either Term ID
the error then is
Object of class WP_Term could not be converted to string
any Ideas how to make this Correct.
Thank you!
Ahmad
are you trying to get the custom taxonomy from the current user? try like this:
get_the_terms( get_the_ID(), 'nationality' )
and if you are sure it will always get only one (take into account some people have more than one nationality) just access the first element:
get_the_terms( get_the_ID(), 'nationality' )[0]
I have the right Code here :
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = get_field('nationality', $curauth);
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo $term->name ; ?>
<?php endforeach; ?>
<?php endif; ?>

Iterate Wordpress custom field?

I have a custom field in an Apus theme of type multi checkbox and I need to display the selected values.
I am using this code:
<?php $keywords = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'keywords', true ); ?>
<?php if ( ! empty( $keywords ) ) : ?>
<li><span><?php echo esc_html__( 'Keywords:', 'apushome' ); ?></span> <?php echo esc_attr( $keywords ); ?></li>
<?php endif; ?>
What this code is showing is
Keywords: Array
How can I loop the field's values to show them on the page?
Best regards
Americo
you may also use
echo implode(",",$keywords);
maybe this would help if this is indexed array, otherwise use
foreach ($keywords as $val){
echo $val. "\n";
}

Show custom product field in new order email

i created a custom field in a product called course-date. I asissnged it a date e.g Jan 30. This is what I have in the email yet nothing shows.. am i missing something? code edited with new snippet below
<?php
/**
* Customer processing order email
*
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$course_date=get_post_meta($product_id, 'course-date', true);
//Note $course_date will get overwritten if there are more than one items in the order.
}
?>
<?php do_action('woocommerce_email_header', $email_heading); ?>
<p>This email is to notify you of your recent registration for the <b>Level 1 - Think and Grow Rich Institute Course</b>!</p>
<p>You are registered to attend on:</p>
<p><?php printf( __( '<b>Registration ID:</b> %s', 'woocommerce' ), $order->get_order_number() ); ?></p>
<p><b>Course Registration Date:</b> <?php echo get_post_meta($post->id, 'course-date', true); ?></p>
<p><b>Time:</b> 8am Registration - 9am Event Begins (See Full Schedule)</p>
<p><b>Location:</b> 240 Belfield Rd, Toronto, ON M9W 1H3</p>
<p>If you have any questions please contact us by e-mail: contact#thinkandgrowrichinstitute.com.</p>
<p>Sincerely,<BR/>
The Think and Grow Rich Institute Team</p><BR/>
<p><em><b>"Whatever The Mind Can Conceive and Believe, It Will Achieve."</b> - Napoleon Hill</em></p>
<?php do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_footer' ); ?>
Hope you get to learn more from this answer and it adds to you knowledge of WooCommerce.
For the Snippet you sent of the "Customer processing order email".
The system is unable to find the value of $_product->id here. So it is not working.
You have to debug it by print the Product id itself in the initial test emails.
If you are unable to get the product id. Here is a bit more information.
An order may have more than one products. So finding the product with the meta fields can be troublesome especially when you are having a question of which product you should show the meta field of.
Easy way is to use the given object $order retrieve the order id from this object then find all the products associated in this order and then find meta field value for those products.
<?php
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$course_date=get_post_meta($product_id, 'course-date', true);
if(isset($course_date) && $course_date!=""){
echo "<p><b>Course Registration Date:</b>".$course_date."</p>";
}
}
?>
Let us know whether this gave you any insight of the issue.
Here's an untested example of how to add a string to the "customer_processing_email" without overriding the template. You would add this code to your theme's functions.php and the text should appear prior to the order table.
add_action( 'woocommerce_email_order_details', 'kia_custom_order_details', 5, 4 );
function kia_custom_order_details( $order, $sent_to_admin, $plain_text, $email ){
if( $email->id == "customer_processing_order" ){
$course_date = null;
// borrowing the loop from WisdmLabs's answer
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$course_date = get_post_meta($product_id, 'course-date', true);
if( $course_date != '' ) break; //break out of the loop if we find a course date
}
if( $course_date && $plain_text ){
echo "Course Registration Date: " . $course_date . "\n\n";
} else if( $course_date && ! $plain_text ){
echo "<p><b>Course Registration Date:</b> " . $course_date . "</p>" . "/n/n";
}
}
}
Don't forget the different formatting for rich text versus plain text emails. I left most of the formatting for the rest of your text to you as the important part was finding/printing the course date meta.
I still think that using a separate email would be the best approach here, but as far as I can tell you were only missing echoing out $course_date in the right place.

wordpress simple paypal shopping cart shortcode in custom fields

i”m using wordpress simple paypal shopping cart and i want to parse the shortcode into custom field, i”ve found an example but it”s not working
above the loop i”ve put this
<?php $price = get_post_meta( $post->ID, ‘price’, true ); ?>
and where i want to display the add to cart button i have this
<?php echo print_wp_cart_button_for_product($name, $price); ?>
it”s not working right
the add to cart button apear on every post, when u press it it add product in cart with no name and no price
when i add key=price and value= 25 it add to cart same noname and noprice product :(
btw the shortcode i want to parse it”s looks like this [wp_cart:PRODUCT NAME:price:PRODUCT PRICE:end]
this is how u do it
<?php $price = get_post_meta( $post->ID, 'price', true ); ?>
<?php $name = get_the_title(); ?>
<?php echo print_wp_cart_button_for_product($name, $price); ?>
that code display the add to cart button
Price: <?php echo get_post_meta($post->ID, 'price', true); ?> $
and that”s for the price to be displayed
have fun
To keep things organize the code it will look like this
<?php $price = get_post_meta( $post->ID, 'PRODUCT-PRICE', true ); ?>
Price: $<?php echo get_post_meta($post->ID, 'PRODUCT-PRICE', true); ?>
Product Name: <?php echo get_post_meta($post->ID, 'PRODUCT-NAME', true); ?>
<?php $name = get_the_title(); ?> <?php echo print_wp_cart_button_for_product
($name, $price); ?>

Resources