为了账号安全,请及时绑定邮箱和手机立即绑定

WooCommerce 在订单通知中显示高级自定义字段 (ACF)

WooCommerce 在订单通知中显示高级自定义字段 (ACF)

PHP
忽然笑 2023-05-12 15:52:14
我添加了以下代码片段以显示自定义字段(如果产品有字段,则不显示分类字段)和预计交货时间。这是工作。    <?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;         }    ?>但现在我正试图在订单通知邮件中显示该字段。在产品标题下方。有人可以指出我如何做到这一点的正确方向吗?
查看完整描述

1 回答

?
萧十郎

TA贡献1815条经验 获得超12个赞

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


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 文件。它应该有效。


在前端订单上显示:


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


您也可以改用woocommerce_order_item_meta_end挂钩,以在产品变体的产品属性之后显示。


查看完整回答
反对 回复 2023-05-12
  • 1 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信