简体   繁体   English

通过 WooCommerce 管理员电子邮件通知中的“woocommerce_email_order_meta”挂钩显示产品自定义字段

[英]Display product custom field via "woocommerce_email_order_meta" hook in WooCommerce admin email notification

My products have a custom field called depot .我的产品有一个名为depot的自定义字段。 I need to display this field only in admin mail notifications, not to the customer order recap or customer mail notifications.我只需要在管理员邮件通知中显示此字段,而不是在客户订单回顾或客户邮件通知中显示。

I use this code to retrieve the custom field in email notifications :我使用此代码来检索电子邮件通知中的自定义字段:

  add_action( 'woocommerce_email_order_meta', 'add_depot', 10, 3 );
  
  function add_depot( $order, $sent_to_admin, $plain_text ){
    $items = $order->get_items();

     foreach ( $items as $item ){
       $depot = $item->get_meta('depot');
       $item['name'] = 'Dépôt: ' . $depot . $item['name'];
     }
  }

At the moment, it displays the field only in customer emails and not in admin emails as I would like.目前,它在客户电子邮件中显示该字段,而不是像我希望的那样在管理电子邮件中显示。 I think I need to use $sent_to_admin but I'm not sure how.我想我需要使用$sent_to_admin但我不确定如何使用。

Thank you.谢谢你。

The woocommerce_email_order_meta hook has 4 arguments, via $email->id you can target specific email notifications woocommerce_email_order_meta钩子有 4 个参数,通过$email->id你可以定位特定的电子邮件通知

So you get:所以你得到:

function action_woocommerce_email_order_meta( $order, $sent_to_admin, $plain_text, $email ) {
    // Targetting specific email notifications
    $email_ids = array( 'new_order' );
    
    // Checks if a value exists in an array
    if ( in_array( $email->id, $email_ids ) ) {
        // Get items
        $items = $order->get_items();

        // Loop trough
        foreach ( $items as $item ) {
            // Get meta
            $depot = $item->get_meta( 'depot' );

            // NOT empty
            if ( ! empty ( $depot ) ) {
               echo '<p style="color:green;font-size:50px;">Dépôt: ' . $depot . ' - ' . $item->get_name() . '</p>';
            } else {
               echo '<p style="color:red;font-size:50px;">Data not found!</p>';
            }
        }
    }
}
add_action( 'woocommerce_email_order_meta', 'action_woocommerce_email_order_meta', 10, 4 );

You can implement this hook:你可以实现这个钩子:

do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

If you see, the second parameter which you get in arguments is - sent to admin.如果您看到,您在参数中获得的第二个参数是 - 发送给管理员。 So if that is true, then you can add your custom meta.因此,如果这是真的,那么您可以添加自定义元。

Let me know if that helps.如果这有帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce管理员电子邮件通知中显示产品自定义字段值 - Display a product custom field value in Woocommerce admin email notifications 在 WooCommerce 电子邮件订单项目中显示可变产品的产品自定义字段 - Display a product custom field for variable products in WooCommerce email order items 在 WooCommerce email 通知中为管理员显示自定义字段 - Display a custom field in WooCommerce email notifications for admin 在 WooCommerce 管理订单页面上将产品自定义字段显示为订单项元数据 - Display product custom field as order item meta on WooCommerce admin order pages 在购物车、结帐、订单和 email 通知中显示 WooCommerce 自定义产品字段元数据 - Display WooCommerce custom product field meta data in cart, checkout, orders and email notifications Woocommerce,通过电子邮件订购元 - Woocommerce, Order Meta with Email 在 WooCommerce 电子邮件订单元中获取自定义字段数组值 - Get a custom field array values within WooCommerce email order meta 在 email 通知中显示自定义订单元数据值 WooCommerce - Display custom order meta data value in email notifications WooCommerce 在 Woocommerce 电子邮件通知上显示自定义元字段值 - Display custom meta field value on Woocommerce email notifications 在woocommerce电子邮件中调用自定义订单元 - Call custom order meta in woocommerce email
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM