[3天,我尝试仅通过电子邮件为新订单的管理员和后台订单显示个性化数据。不适合客户,这只是产品存储在库存中的地方。
// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'bbloomer_save_rangement_variations', 10, 2 );
function bbloomer_save_rangement_variations( $variation_id, $i ) {
$rangement = $_POST['rangement'][$i];
if ( isset( $rangement ) ) update_post_meta( $variation_id, 'rangement', esc_attr( $rangement ) );
}
// -----------------------------------------
// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'bbloomer_add_rangement_variation_data' );
function bbloomer_add_rangement_variation_data( $variations ) {
$variations['rangement'] = '<div class="woocommerce_rangement">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'rangement', true ) . '</span></div>';
return $variations;
}
// 4. afficher le champ rangement dans l'email envoyer à l'admin
add_action( 'woocommerce_email_order_details', 'add_rangement_to_order_item_meta', 20, 4 );
function add_rangement_to_order_item_meta( $order, $sent_to_admin, $plain_text, $email ) {
$rangement_value = get_post_meta( $item->get_product_id(), 'rangement', true );
if ( ! empty($rangement_value) ){
$item->update_meta_data( __('Rangement', 'woocommerce'), $rangement_value );
}
}
假定其余代码正常工作
使用$sent_to_admin
参数作为额外的检查
add_action( 'woocommerce_email_order_details', 'add_rangement_to_order_item_meta', 20, 4 );
function add_rangement_to_order_item_meta( $order, $sent_to_admin, $plain_text, $email ) {
// Only admin need to know
if ( !$sent_to_admin ) {
return;
}
}