簡體   English   中英

在 Woocommerce 電子郵件通知上顯示自定義元字段值

[英]Display custom meta field value on Woocommerce email notifications

我能夠在購物車和結帳頁面中獲取元字段,但是在電子郵件中我嘗試使用$order數組,但在電子郵件中找不到任何自定義字段。

如何在電子郵件通知(管理員、客戶)中獲取自定義元字段?

我的代碼如下:

function wdm_add_custom_order_line_item_meta($item, $cart_item_key, $values, $order)
{

    if(array_key_exists('wdm_name', $values))
    {
        $item->add_meta_data('_wdm_name',$values['wdm_name']);
    }
}

add_action( 'woocommerce_email_order_details', 'action_email_order_details', 10, 4 );
    function action_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
     if( $sent_to_admin ): // For admin emails notification

        echo get_post_meta( $order->id, '_wdm_name', true );

     endif;
}

任何幫助表示贊賞。

您可以使用以下代碼實現。

電子郵件代碼

 add_filter( 'woocommerce_email_order_meta_fields',    'woocommerce_email_order_meta_fields_func', 10, 3 );
 function woocommerce_email_order_meta_fields_func( $fields, $sent_to_admin, $order ) {

$fields['appointment_date'] = array(
    'label' => __( 'Wdm name', 'woocommerce' ),
    'value' => wptexturize( get_post_meta( $order->id, '_wdm_name', true ) )
);

$fields['appointment_time'] = array(
    'label' => __( 'Wdm Name', 'woocommerce' ),
    'value' => wptexturize( get_post_meta( $order->id, '_wdm_name', true ) )
);

//... more meta fields goes here

return $fields;
}

管理端訂單添加/編輯頁面

add_action( 'woocommerce_email_after_order_table', 'woocommerce_email_after_order_table_func' );

函數 woocommerce_email_after_order_table_func( $order ) { ?>

<h3>Wdm Name (Custom field)</h3>
<table>
    <tr>
        <td>Date: </td>
        <td><?php echo wptexturize( get_post_meta( $order->id, '_wdm_name', true ) ); ?></td>
    </tr>
    <tr>
        <td>Time: </td>
        <td><?php echo wptexturize( get_post_meta( $order->id, '_wdm_name', true ) ); ?></td>
    </tr>
    <!--additional custom meta and HTML code goes here-->
</table>

<?php
}

您的代碼不完整且不正確。 請將其替換為以下內容:

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    if( isset( $values['wdm_name'] ) )
        $item->add_meta_data( __('Custom label'), $values['wdm_name'] );
}

現在一旦下訂單(提交),這個自定義字段就會出現在訂單項目和電子郵件通知上的任何地方......所以你不需要其他任何東西。 您只需要為此自定義字段值設置正確的標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM