问题
I've added the following snippet to display a custom field (that don't display taxonomy field if product has field) with the estimated delivery time. This is working.
<?php add_action( 'woocommerce_before_add_to_cart_form', 'geschatte_levertijd', 10 );
function geschatte_levertijd (){
if( get_field('plevertijd') ): ?>
<span class="product_melding_kleur"><i class="fa fa-truck"></i>Levertijd: <a href="/verzending-en-levertijd/" alt="Verzending en levertijd" ><?php the_field( 'plevertijd' ); ?></a></span>
<?php else: ?>
<? $terms = get_the_terms( $post->ID, 'product_cat' );
if ( !empty($terms)):
$term = array_pop($terms);
$text= get_field('levertijd', $term);
if (!empty($levertijd))?>
<span class="product_melding_kleur"><i class="fa fa-truck"></i>Levertijd: <a href="/verzending-en-levertijd/" alt="Verzending en levertijd" ><?php the_field( 'levertijd', $term ); ?></a></span>
<?php endif; ?>
<?php endif;
}
?>
But now I'm trying to display that field inside the order notification mail. Below the product title. Could someone point me into the right direction on how to do that?
回答1:
The following will use your code to make happen the display in order email notifications under order item product name:
add_action( 'woocommerce_order_item_meta_start', 'add_estimated_delivery_time_to_emails', 10, 3 );
function add_estimated_delivery_time_to_emails( $item_id, $item, $order ) {
// On email notifications and for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
if( $plevertijd = get_field('plevertijd', $item->get_product_id() ) ) {
echo '<span class="product_melding_kleur"><i class="fa fa-truck"></i>Levertijd: <a href="/verzending-en-levertijd/" alt="Verzending en levertijd" >' . $plevertijd . '</a></span>';
} else {
$terms = get_the_terms( $item->get_product_id(), 'product_cat' );
if ( ! empty($terms) ) {
$term = array_pop( $terms );
if( $levertijd = get_field('levertijd', $term ) ) {
echo '<span class="product_melding_kleur"><i class="fa fa-truck"></i>Levertijd: <a href="/verzending-en-levertijd/" alt="Verzending en levertijd" >' . $levertijd . '</a></span>';
}
}
}
}
}
Code goes in functions.php file of your active child theme (or active theme). It should works.
Displaying on frontend orders:
If you want that to be displayed on customer order (front end), you can remove from the
IF
statement the following:! is_wc_endpoint_url() &&
You can also use the woocommerce_order_item_meta_end
hook instead, to get the display after the product attributes on product variations.
来源:https://stackoverflow.com/questions/62301458/woocommerce-display-avanced-custom-fields-acf-inside-order-notification