首页 文章

将自定义字段值添加到Woocommerce订单电子邮件模板

提问于
浏览
1

在具有高级自定义字段插件的Woocommerce中,我们为 products 添加了一个自定义字段,并且每个 product 的字段值为 specific .

现在我正在尝试将此自定义字段值添加到我们的Woocommerce订单确认电子邮件中 .

我尝试了以下代码但没有成功:

<?php
    if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
        $order_id = int_val( $order->id ); // Older than 3.0
    } else {
        $order_id = int_val( $order->get_id() ); // 3.0+
    }

    $inst1 = get_field(‘how1’, $order_id );

    if( $inst1 ){
        echo '<p>' . $inst1 . '</p>';
    }
?> with Advanced Custom Fields plugin

1 回答

  • 0

    作为您的自定义字段 is specific to "product" post type (但不是"order"帖子类型)您需要 to get first the order items 来获取 the product ID ,您应该以这种方式使用ACF get_field() 函数:

    <?php
    
        foreach ( $order->get_items() as $item ) {
            // Get the product ID
            if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
                $product_id = $item['product_id']; // Older than 3.0
            } else {
                $product_id = $item->get_product_id(); // 3.0+
            }
    
            $inst1 = get_field( 'how1', $product_id );
    
            if( $inst1 ){
                echo '<p>' . $inst1 . '</p>';
            }
        }
    
    <?
    

    将为订单中的每个项目显示自定义字段值,因为订单中可包含许多项目 .


    参考文献:

相关问题