WooCommerce 在订单通知中显示高级自定义字段 (ACF) [英] WooCommerce Display avanced custom fields (ACF) inside order notification

查看:32
本文介绍了WooCommerce 在订单通知中显示高级自定义字段 (ACF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了以下代码段以显示带有估计交货时间的自定义字段(如果产品有字段,则不显示分类字段).这是有效的.

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?

推荐答案

以下将使用您的代码来实现在订单商品名称下的订单电子邮件通知的显示:

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>';
                }
            }
        } 
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.它应该有效.

Code goes in functions.php file of your active child theme (or active theme). It should works.

在前端订单上显示:

如果您希望将其显示在客户订单(前端)上,您可以从 IF 语句中删除以下内容:!is_wc_endpoint_url() &&

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() &&

您也可以使用 woocommerce_order_item_meta_end 钩子代替,以在产品变体的产品属性之后显示.

You can also use the woocommerce_order_item_meta_end hook instead, to get the display after the product attributes on product variations.

这篇关于WooCommerce 在订单通知中显示高级自定义字段 (ACF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆