在特定的 WooCommerce 电子邮件中显示产品 ACF 字段

Display product ACF fields on a specific WooCommerce email

所以我几乎明白了,但是指定 WooCommerce 电子邮件的最后一个细节让我有点头疼。

我需要显示产品的 ACF(高级自定义字段)字段(在本例中是自定义发货时间)


这是我找到的主要方式: Thank you in advance @LoicTheAztec

我还添加了一些条件设置(请注意,我的 PHP 刚开始是复制粘贴式的) 效果很好。

然而,我无法绕过的是让它只适用于新订单电子邮件。 我的设置是这样的,它可以工作,但是它会显示在所有包含电子邮件订单详细信息的电子邮件中,而且我无法在已完成的订单电子邮件中显示发货时间,因为这会造成混淆。

// Display product ACF custom field value shipping in email notification
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
    // Targeting email notifications only
    if ( 'new_order' == $email->id )   
        return $item_name;

    // Get the WC_Product object (from order item)
    $product = $item->get_product();
    $othershipping = get_field( 'shipping_custom', $product->get_id());

    if( $shpng_value = get_field('shipping_', $product->get_id())== "24h") {
        $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
        <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>Get it tomorrow(24h)</p>' . '</p>';
    }
    elseif( $shpng_value = get_field('shipping_', $product->get_id())== "2-5 days") {
        $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
        <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>2-5 days</p>' . '</p>';
    }
    elseif( $shpng_value = get_field('shipping_', $product->get_id())== "other") {
        $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
        <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . $othershipping . '</p>';
    }

    return $item_name;
}

我试过切换

if ( 'new_order' == $email->id ) 

if ( 'new_order' != $email->id ) 

但这只会让它无法在任何地方工作。


我也觉得可能是这一段

function custom_order_item_name( $item_name, $item ) {

需要添加的地方($order, $sent_to_admin, $plain_text, $email )

function custom_order_item_name( $item_name, $item, $order, $sent_to_admin, $plain_text, $email ) 

但这会使电子邮件 return 出错。

通常这应该适用于下面的代码

Note: my answer is largely based on: . CREDITS: @Loictheaztec so don't forget to upvote that answer to!

// Setting the "sent_to_admin" as a global variable
function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email) {
    $GLOBALS['email_data'] = array(
        'sent_to_admin' => $sent_to_admin, // <== HERE we set "$sent_to_admin" value
        'email_id' => $email->id, // The email ID (to target specific email notification)
    );
}
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);

function custom_order_item_name( $item_name, $item ) {
    if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {

        // Getting the custom 'email_data' global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_data = $refNameGlobalsVar['email_data'];

        // Only for new order
        if( is_array( $email_data ) && $email_data['email_id'] == 'new_order' ) {

            // Get the WC_Product object (from order item)
            $product = $item->get_product();

            $othershipping = get_field( 'shipping_custom', $product->get_id());

            if( $shpng_value = get_field('shipping_', $product->get_id())== "24h") {
                $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
                <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>Get it tomorrow(24h)</p>' . '</p>';
            }
            elseif( $shpng_value = get_field('shipping_', $product->get_id())== "2-5 days") {
                $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
                <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>2-5 days</p>' . '</p>';
            }
            elseif( $shpng_value = get_field('shipping_', $product->get_id())== "other") {
                $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
                <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . $othershipping . '</p>';
            }
        }
    }

    return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );