在订单电子邮件通知中输出产品自定义字段值
Output product custom field values in order email notification
我有一个 Woocommerce 产品的自定义字段,我想在订单电子邮件中显示它的价值。
因为我使用的是自定义产品提交表单,所以我在下面为自定义字段添加了此代码以创建自定义字段:
<?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') )
) );
?>
我也尝试将下面的代码添加到 functions.php,但这只会显示 "What time?" 而没有订单电子邮件的价值...
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>';
}
可能是什么问题?
您使用了正确的钩子,但您只是忘记了钩子函数中的钩子参数。
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.
试试下面的代码,现在可以用了:
// 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>';
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
我有一个 Woocommerce 产品的自定义字段,我想在订单电子邮件中显示它的价值。
因为我使用的是自定义产品提交表单,所以我在下面为自定义字段添加了此代码以创建自定义字段:
<?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') )
) );
?>
我也尝试将下面的代码添加到 functions.php,但这只会显示 "What time?" 而没有订单电子邮件的价值...
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>';
}
可能是什么问题?
您使用了正确的钩子,但您只是忘记了钩子函数中的钩子参数。
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.
试试下面的代码,现在可以用了:
// 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>';
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。