WooCommerce Display avanced custom fields (ACF) inside order notification

你说的曾经没有我的故事 提交于 2021-02-11 16:12:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!