首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在特定的WooCommerce电子邮件上显示产品ACF字段

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

Stack Overflow用户
提问于 2020-05-15 19:26:59
回答 1查看 1.6K关注 0票数 1

所以我差一点就明白了,但这最后一个关于指定WooCommerce邮件的细节让我有点头脑发热。

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

  • ,但仅在新订单电子邮件(处理订单和等待付款订单发送给客户端)中,然后才向admin.

发送新订单电子邮件。

这是我发现的主要方法:"Display product ACF field value in Woocommerce transaction emails",预先谢谢@LoicTheAztec

我还添加了一些条件设置(请注意,我的PHP是非常开始的,复制-粘贴),它们运行得很好。

然而,我无法解决的是,它只适用于新的订单电子邮件。我有这样的设置,它的工作,但它显示在所有的电子邮件,其中包含电子邮件订单的细节,我不能让发货时间显示在完成的订单电子邮件,因为它会造成混乱。

代码语言:javascript
复制
// 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;
}

我试过切换

代码语言:javascript
复制
if ( 'new_order' == $email->id ) 

代码语言:javascript
复制
if ( 'new_order' != $email->id ) 

但这只会让它在任何地方都行不通。

我还以为可能是这部分

代码语言:javascript
复制
function custom_order_item_name( $item_name, $item ) {

在这里我需要添加($order, $sent_to_admin, $plain_text, $email )

代码语言:javascript
复制
function custom_order_item_name( $item_name, $item, $order, $sent_to_admin, $plain_text, $email ) 

但这会使电子邮件返回错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-15 19:55:33

通常情况下,这应该与下面的代码一起工作。

注:我的答案主要是基于:"Customize order item meta only for WooCommerce admin email notifications"。学分:@Loictheaztec,所以别忘了投这个答案!

代码语言:javascript
复制
// 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 );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61826893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档