我在主题functions.php
中具有以下代码,用于向产品中添加供应商字段:
// 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 ) );
}
这一切都很好。现在,我需要将此新供应商字段添加到管理订单电子邮件中。我的代码不太流利,因此尝试了以下发现的尝试,但均未成功:
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;
}
我需要什么代码才能将供应商字段添加到管理电子邮件中?
非常感谢
$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
// 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);