【问题标题】:Display product ACF fields on a specific WooCommerce email在特定的 WooCommerce 电子邮件上显示产品 ACF 字段
【发布时间】:2020-09-01 17:32:42
【问题描述】:

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

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

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

这是我找到的主要方式:"Display product ACF field value in Woocommerce transaction emails"提前谢谢@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 ) 

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

【问题讨论】:

    标签: php wordpress woocommerce advanced-custom-fields hook-woocommerce


    【解决方案1】:

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

    注意:我的回答主要基于:"Customize order item meta only for WooCommerce admin email notifications"。致谢:@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 );
    

    【讨论】:

    • 谢谢!最初这个 ust 发送到 new_ordr 并没有像我希望的那样发送到多个电子邮件(customer_on_hold_order customer_processing_order 和 new_order)所以我想我可以重复 // Only for new order if 语句的行并将电子邮件 ID 更改为正确的那个就行了!非常感谢
    猜你喜欢
    • 1970-01-01
    • 2021-11-26
    • 2020-12-07
    • 1970-01-01
    • 2019-10-07
    • 2019-03-27
    • 2021-04-12
    • 2020-11-05
    相关资源
    最近更新 更多