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

海梅·马托斯(Jaime Matos)

所以我几乎理解了这一点,但是指定WooCommerce电子邮件的最后一个细节让我有些疑惑。

我需要显示产品的ACF(高级自定义字段)字段(在这种情况下,这是自定义发货时间)

  • 但是仅在新订单电子邮件中(处理订单和等待付款的订单已发送给客户),然后将新订单电子邮件发送给管理员。

这是我发现的主要方法:“在Woocommerce交易电子邮件中显示产品ACF字段值”预先感谢@ 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 ) 

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

7uc1f3r

通常这应该与下面的代码一起使用

注意:我的回答主要基于:“仅针对WooCommerce管理员电子邮件通知定制订单项元”致谢:@ Loictheaztec,所以不要忘记对这个答案进行投票

// 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 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Woocommerce交易电子邮件中显示产品ACF字段值

在WooCommerce电子邮件通知中显示特定的产品属性

在Woocommerce管理员电子邮件通知中显示产品ACF值

在特定的WooCommerce电子邮件通知上显示自定义字段

在WooCommerce电子邮件订购项目中显示可变产品的产品自定义字段

在Woocommerce管理员电子邮件通知中显示产品自定义字段值

在Woocommerce订单和电子邮件中显示产品变体的自定义字段

在woocommerce订单电子邮件中显示多个产品的自定义字段

在WooCommerce电子邮件通知上显示自定义产品图片

在WooCommerce电子邮件通知上显示第二个自定义字段

在电子邮件通知上显示WooCommerce自定义结帐字段值

在Woocommerce中的特定电子邮件通知上为特定产品添加自定义文本

WooCommerce电子邮件模板中的ACF自定义字段

在WooCommerce“订单已完成”电子邮件中添加自定义产品字段

在woocommerce电子邮件中输出产品自定义字段(图像)

在 woocommerce 电子邮件标题中输出产品自定义字段

如何在新订单电子邮件中显示WooCommerce自定义产品元?

在WooCommerce的电子邮件和订单页面中保存并显示产品自定义数据

在Woocommerce订单和电子邮件通知中显示产品品牌和名称

在 WooCommerce 订单接收页面和电子邮件的新行中显示产品(变体)描述

电子邮件给特定的商店经理:Woocommerce

在电子邮件模板中获取产品ID:WooCommerce

产品标签的WooCommerce自定义电子邮件

删除woocommerce电子邮件通知中的产品下载部分

从WooCommerce 3中的电子邮件模板获取产品ID

修改woocommerce电子邮件中某些产品的数量

WooCommerce-每个产品的自定义电子邮件

在WooCommerce订单和电子邮件通知中显示自定义字段值

在Woocommerce管理员,订单和电子邮件中显示自定义付款字段