本文介绍了WooCommerce自定义常规字段-添加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的主题functions.php
中具有以下代码,用于将供应商"字段添加到产品中:
I have the following code in my theme functions.php
to add a Supplier field to a product:
// Add supplier to products
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields_supplier' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_supplier_save' );
function woo_add_custom_general_fields_supplier() {
global $woocommerce, $post;
// Text Field
woocommerce_wp_text_input(
array(
'id' => 'supplier',
'label' => __( 'Supplier', 'woocommerce' ),
'placeholder' => 'supplier',
'desc_tip' => 'true',
'description' => __( 'Product supplier.', 'woocommerce' )
)
);
}
function woo_add_custom_general_fields_supplier_save( $post_id ){
// Textarea
$woocommerce_supplier = $_POST['supplier'];
if( !empty( $woocommerce_supplier ) )
update_post_meta( $post_id, 'supplier', esc_html( $woocommerce_supplier ) );
}
这一切都很好.现在,我需要将此新供应商字段添加到管理订单电子邮件中.我的代码不太流利,因此尝试了以下尝试,但均未成功:
This all works great. I now need to add this new supplier field to the admin order email. I'm not very fluent in code so I tried the following that I found without any success:
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['supplier'] = array(
'label' => __( 'Supplier' ),
'value' => get_post_meta( $order->id, 'supplier', true ),
);
return $fields;
}
要将供应商字段添加到管理电子邮件中,我需要什么代码?
What code would I need to be able to add the supplier field to the admin email?
非常感谢
推荐答案
一些评论和调整
- 在第1步和第2步(创建字段并保存它)中,使用
$product_id
.但是,在第3步中使用$order_id
.$order_id
不等于$product_id
- 使用
woocommerce_admin_process_product_object
保存而不是过时的woocommerce_process_product_meta
钩子 -
woocommerce_email_order_meta_fields
已替换为woocommerce_checkout_create_order_line_item
- In step 1 and 2 (creating the field and saving it, you make use of the
$product_id
.However, in step 3 you use the$order_id
.$order_id
is not equal to$product_id
- Use
woocommerce_admin_process_product_object
to save instead of the outdatedwoocommerce_process_product_meta
hook woocommerce_email_order_meta_fields
has been replaced withwoocommerce_checkout_create_order_line_item
所以你得到
So you get
// Add supplier to products
// Display Fields
function woo_add_custom_general_fields_supplier() {
// Text Field
woocommerce_wp_text_input( array(
'id' => 'supplier',
'label' => __( 'Supplier', 'woocommerce' ),
'placeholder' => 'supplier',
'desc_tip' => 'true',
'description' => __( 'Product supplier.', 'woocommerce' )
));
}
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields_supplier', 10, 0 );
// Save Fields
function woo_add_custom_general_fields_supplier_save( $product ){
if( isset($_POST['supplier']) ) {
$product->update_meta_data( 'supplier', esc_html( $_POST['supplier'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'woo_add_custom_general_fields_supplier_save', 10, 1 );
// Displaying custom fields in the WooCommerce order and email confirmations
function action_woocommerce_checkout_create_order_line_item($item, $cart_item_key, $values, $order) {
// Get meta
$supplier = $values['data']->get_meta('supplier');
// True
if ( $supplier ) {
$item->update_meta_data( 'supplier', $supplier );
}
}
add_action('woocommerce_checkout_create_order_line_item', 'action_woocommerce_checkout_create_order_line_item', 20, 4);
这篇关于WooCommerce自定义常规字段-添加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!