在订单电子邮件通知中输出产品自定义字段值 [英] Output product custom field values in order email notification

查看:43
本文介绍了在订单电子邮件通知中输出产品自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Woocommerce 产品的自定义字段,我想在订单电子邮件中显示它的价值.

当我使用自定义产品提交表单时,我为下面的自定义字段添加了此代码以创建自定义字段:

$object_id,'类' =>'选择2','id' =>'wcv_custom_product_ingredients','标签' =>__( '什么时间?', 'wcvendors-pro' ),'占位符' =>__( '选择时间', 'wcvendors-pro' ),'wrapper_start' =>'

','wrapper_end' =>'</div>','desc_tip' =>'真的','说明' =>__( '选择时间', 'wcvendors-pro' ),'选项' =>数组('12:00 午夜' => __('12:00 午夜','wcv_custom_product_ingredients'),'12:15 午夜'=> __('12:15 午夜','wcv_custom_product_ingredients'))) );?>

我也尝试将下面的代码添加到functions.php,但这只显示什么时间?"订单电子邮件没有价值...

add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email');函数 wcv_ingredients_email() {$output = get_post_meta( get_the_ID(), 'wcv_custom_product_ingredients', true );回声'什么时候?' .$输出.'
';}

可能是什么问题?

解决方案

您正在使用正确的钩子,但您只是忘记了钩子函数中的钩子参数.

<块引用>

此外,由于您的目标是产品自定义字段值,并且在一个订单中您可以拥有多个商品(产品),下面的代码将显示每个订单商品的此值.

试试下面的代码,它现在可以工作了:

//在 WooCommerce 版本 2.6.x 和 3+ 上测试 — 仅适用于简单产品.add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email', 10, 4);函数 wcv_ingredients_email( $order, $sent_to_admin, $plain_text, $email ){foreach($order->get_items() as $item_values){//获取简单产品(非可变产品)的产品 ID$product_id = $item_values['product_id'];$output = get_post_meta( $product_id, 'wcv_custom_product_ingredients', true );回声'什么时候?' .$输出.'
';}}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

I have a custom field for a Woocommerce's product, and I want to display it's value in order emails.

As I'm using custom product submission form, I added this code for custom field below to create a custom field:

<?php 
    WCVendors_Pro_Form_Helper::select( array(  
        'post_id'       => $object_id,
        'class'         => 'select2',
        'id'            => 'wcv_custom_product_ingredients', 
        'label'         => __( 'What time?', 'wcvendors-pro' ), 
        'placeholder'   => __( 'Pick a time', 'wcvendors-pro' ),
        'wrapper_start' => '<div class="all-100">',
        'wrapper_end'   => '</div>',
        'desc_tip'      => 'true', 
        'description'   => __( 'Pick a time', 'wcvendors-pro' ),
        'options'       => array( '12:00 midnight' => __('12:00 midnight', 'wcv_custom_product_ingredients'), '12:15 midnight'=> __('12:15 midnight', 'wcv_custom_product_ingredients') )
) );
?>

I also tried adding code below to functions.php, but this only displays "What time?" without value in order emails...

add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email');
function wcv_ingredients_email() {
    $output = get_post_meta( get_the_ID(), 'wcv_custom_product_ingredients', true );
    echo 'What time? ' . $output . '<br>';
}

What could be the issue?

解决方案

You are using the correct hook, but you just forgot the hook arguments in the hooked function.

Also as you are targeting a product custom field value, and as in an order you can have many items (products), the code below will display this value for each order item.

Try the code below, it works now:

// Tested on WooCommerce version 2.6.x and 3+ — For simple products only.
add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email', 10, 4);
function wcv_ingredients_email( $order,  $sent_to_admin,  $plain_text,  $email ){
    foreach($order->get_items() as $item_values){
        // Get the product ID for simple products (not variable ones)
        $product_id = $item_values['product_id'];
        $output = get_post_meta( $product_id, 'wcv_custom_product_ingredients', true );
        echo 'What time? ' . $output . '<br>';
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

这篇关于在订单电子邮件通知中输出产品自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆