【问题标题】:WooCommerce Display avanced custom fields (ACF) inside order notificationWooCommerce 在订单通知中显示高级自定义字段 (ACF)
【发布时间】:2020-09-29 17:50:44
【问题描述】:

我添加了以下 sn-p 以显示一个自定义字段(如果产品有字段,则不显示分类字段)和预计交货时间。这是有效的。

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

但现在我正在尝试在订单通知邮件中显示该字段。在产品标题下方。 有人可以为我指出正确的方向吗?

【问题讨论】:

    标签: php wordpress woocommerce advanced-custom-fields email-notifications


    【解决方案1】:

    以下将使用您的代码在订单项目产品名称下的订单电子邮件通知中显示:

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

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

    【讨论】:

    • 谢谢,我会尽快尝试并通知您
    猜你喜欢
    • 2016-11-13
    • 2018-08-26
    • 2018-10-06
    • 2018-05-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2019-10-10
    • 2021-10-06
    相关资源
    最近更新 更多