Wocommerce 이메일 알림에서 원하지 않는 주문 항목 메타 데이터 필터링
: ( :email-order-items.php합니다), WooCommerce 합니다.wc_display_item_meta주문표에 제품 상세 정보를 표시합니다.다에 .wc-template-functions.php파일(라인 번호 3011).합니다를 합니다.
function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html = '';
$args = wp_parse_args( $args, array(
'before' => '<ul class="wc-item-meta"><li>',
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => true,
'autop' => false,
) );
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
$value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
$strings[] = '<strong class="wc-item-meta-label">' . wp_kses_post( $meta->display_key ) . ':</strong> ' . $value;
}
if ( $strings ) {
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}
$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );
if ( $args['echo'] ) {
echo $html; // WPCS: XSS ok.
} else {
return $html;
}
}
문제는 주문 메일에 표시하지 않으려는 항목 데이터를 걸러내는 데 도움이 되는 인수가 필요하지 않다는 것입니다.에서 이 기능을 변경하고 싶지 않습니다.wc-template-functions.php핵심 파일이기 때문에.서할 수 알고 functions.phpwc_display_item_meta특정 항목 메타를 필터링하는 함수입니다.
참고: 제품 세부 정보에서 특정 항목 데이터를 삭제하는 것이 아니라 내부 주문 처리에 데이터가 필수적인 이유를 누군가가 제안할 수도 있습니다.저는 단지 그것이 고객들에게 보여지기를 원하지 않습니다.
업데이트 #1: 주문 이메일에 표시하지 않을 메타 데이터는 무엇입니까?아래는 주문 이메일의 스크린샷입니다.3가지 항목 데이터를 강조했습니다."Qty Selector", "Qty" 및 "Total".이 세 가지 모두 주문 메일에 표시되지 않았으면 합니다.
보증 없이 다음을 시도해 보십시오(사실 필요한 키가 없기 때문에).
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'unset_specific_order_item_meta_data', 10, 2);
function unset_specific_order_item_meta_data($formatted_meta, $item){
// Only on emails notifications
if( is_admin() || is_wc_endpoint_url() )
return $formatted_meta;
foreach( $formatted_meta as $key => $meta ){
if( in_array( $meta->key, array('Qty Selector', 'Qty', 'Total') ) )
unset($formatted_meta[$key]);
}
return $formatted_meta;
}
코드가 작동합니다.활성 하위 테마(활성 테마)의 php 파일입니다.당신의 메타데이터가 아닌 다른 메타데이터로 테스트를 하여 작동합니다.당신에게도 효과가 있기를 바랍니다.
이 코드와 함께 사용되는 후크는 오른쪽 필터 후크입니다.에 위치해 있습니다.
WC_Order_Itemmethod 및 주문품목 메타데이터를 필터링할 수 있도록 합니다.
승인된 답변에 버그가 있고, 제가 인터넷 주변에서 발견한 다른 모든 토막글들이 있습니다. 그래서 전세계 상점들이 실수로 정보를 유출하지 않기를 바라는 마음으로 여기에 제 자신의 답변을 올립니다.
문제는 Order actions meta 박스를 사용하여 이메일을 재전송할 때 필터 체크가 실패한다는 것입니다.is_admin() === true.
주문 작업은 주문 페이지 측면의 메타 상자입니다.
처음에는 주문이 생성되면 원하는 대로 전자 메일을 필터링하지만, 관리자가 고객에게 전자 메일을 전송하면 전자 메일이 끊어지고 모든 메타 필드가 다시 전자 메일에 표시됩니다.
이 시나리오를 수정하는 코드는 다음과 같습니다.
$is_resend = isset($_POST['wc_order_action']) ? wc_clean( wp_unslash( $_POST['wc_order_action'] ) ) === 'send_order_details' : false;
if ( !$is_resend && (is_admin() || is_wc_endpoint_url() ) ) {
return $formatted_meta;
}
를 을 볼 수 .$_POST 식으로 도 해야 될 거예요 그것도 그렇게 청소를 해야 합니다. 그렇지 않으면 일치하지 않습니다.
수용된 솔루션의 답변에 통합된 전체 예시는 다음과 같습니다.
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'unset_specific_order_item_meta_data', 10, 2);
function unset_specific_order_item_meta_data($formatted_meta, $item){
// Only on emails notifications
$is_resend = isset($_POST['wc_order_action']) ? wc_clean( wp_unslash( $_POST['wc_order_action'] ) ) === 'send_order_details' : false;
if ( !$is_resend && (is_admin() || is_wc_endpoint_url() ) ) {
return $formatted_meta;
}
foreach( $formatted_meta as $key => $meta ){
if( in_array( $meta->key, array('Qty Selector', 'Qty', 'Total') ) )
unset($formatted_meta[$key]);
}
return $formatted_meta;
}
관리 백엔드에만 주문 항목 메타 데이터를 표시하고 싶다고 들었습니다.그건 사실 까다로운 문제입니다.몇 시간 동안 장난을 쳤지만 해결책을 찾지 못했습니다. 고객에게 보낸 이메일에 주문 항목 메타 데이터가 나타나지 않는다는 것을 확인하십시오.
문제는 이러한 전자 메일이 여러 가지 방법(예: @rtpHarry가 언급하는 메타 상자 재발송)을 통해 수행되거나 주문 개요, 단일 주문 보기 또는 자동/프로그램 주문 상태 변경을 통해 수행됩니다.따라서 주문 항목 메타 데이터를 설정 해제해야 하는 경우가 많습니다. 관리 백엔드를 제외한 모든 경우를 찾아야 합니다.
한 합니다를 이 제 제안입니다.woocommerce_order_item_get_formatted_meta_data합니다와 추가합니다.woocommerce_before_order_itemmeta관리 백엔드에서만 발사됩니다. 메타데이터가 할 수 .get_formatted_meta_data데이터를 가져오는 방법.다 할 수 .wc_get_order_item_meta.
전체 코드(테스트 및 작동):
//Hide 'Qty Selector', 'Qty' and 'Total' completely
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'unset_specific_order_item_meta_data');
function unset_specific_order_item_meta_data($formatted_meta){
foreach( $formatted_meta as $key => $meta ){
if( in_array( $meta->key, array('Qty Selector', 'Qty', 'Total') ) )
unset($formatted_meta[$key]);
}
return $formatted_meta;
}
//Add 'Qty Selector', 'Qty' and 'Total' in the admin backend only
add_action('woocommerce_before_order_itemmeta', 'add_specific_order_item_meta_data_in_backend', 10, 2);
function add_specific_order_item_meta_data_in_backend( $item_id, $item ) {
//Only applies for line items
if( $item->get_type() !== 'line_item' ) return;
$qty_sel_lines = wc_get_order_item_meta($item_id, 'Qty Selector', false);
$qty_lines = wc_get_order_item_meta($item_id, 'Qty', false);
$total_lines = wc_get_order_item_meta($item_id, 'Total', false);
foreach ($qty_sel_lines as $qty_sel_line){
echo $qty_sel_line . '<br>';
}
foreach ($qty_lines as $qty_line){
echo $qty_line . '<br>';
}
foreach ($total_lines as $total_line){
echo $total_line. '<br>';
}
}
참고: 주문 항목 메타 데이터를 관리자 이메일에 추가해야 하는 경우에는 별도로 추가해야 합니다.나는 그것에 대한 옵션을 검토하지 않았습니다.
저는 @pstidsen의 주장에 동의합니다.그래서 메타데이터를 다시 추가하지 않고 어떻게 해결할 수 있을지 고민했습니다. 이전에 추가된 것과 같은 방식으로 처리하지 않는 것이 방해가 되었기 때문입니다.메타데이터에 css 클래스 등을 추가할 필터 등이 있습니다.그러니 신경 쓸 필요가 있었을 겁니다
이메일, 사용자 정의 이메일, PDF 송장 또는 유사한 시나리오에 사용할 수 있는 기회를 제공하는 제 접근 방식입니다.또한 폴백을 사용하여 프론트엔드 또는 고려하지 않은 모든 상황을 필터링합니다.그 외의 것은 순서를 염두에 두시기 바랍니다.마지막으로 관리자 필터를 확인했는데, 다른 필터가 발화되기 전인지 확인했습니다.예를 들어 이메일의 상황은 다음과 같습니다.관리자 인터페이스에서 전송되므로 관리자 필터는 사실이지만 이메일 필터도 마찬가지입니다.
관리자 이메일에 대한 다른 필터에 대한 기능도 제공됩니다.
/**
* This function filters all unwanted item metadata, if the specific filter are hooked in
* we also use a fallback filter, if none of the hooks are fired
*
* @params array() $metadata
*
*/
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'custom_filter_item_meta_data', 50, 1);
function custom_filter_item_meta_data( $metadata ){
if ( empty( $metadata ) ) return $metadata;
$filter_array = array();
if ( apply_filters( 'custom_filter_item_meta_email', false ) ){
// email filter goes here
$filter_array = array( 'whatever','you', 'wanna', 'filter, 'for', 'email' );
}elseif ( apply_filters( 'custom_filter_item_meta_admin_email', false ) ){
// admin email filter goes here
// pass
elseif ( apply_filters( 'custom_filter_item_meta_invoice', false ) ){
// invoice filter goes here
$filter_array = array( 'whatever','you', 'wanna', 'filter, 'for', 'invoices' );
}elseif ( apply_filters( 'custom_filter_item_meta_admin', false ) ){
// general admin filter goes here
$filter_array = array( 'whatever','you', 'wanna', 'filter, 'for', 'admin_backend' );
}else{
// fallback filter
$filter_array = array( 'whatever','you', 'wanna', 'filter, 'for', 'fallback' );
}
foreach ( $metadata as $key => $meta ){
if ( in_array( $meta->key, $filter_array ) ){
unset ( $metadata[ $key ] );
}
}
return $metadata;
}
/**
* Is used to enable our item meta filter for our admin backend
* Hooked:
* @admin_init
*/
add_action( 'admin_init', 'custom_init_item_meta_filter_admin', 50, 1 );
function custom_init_item_meta_filter_admin(){
add_filter( 'custom_filter_item_meta_admin', function(){ return true; });
}
/**
* Is used to enable our item meta filter for emails
* Hooked:
* @woocommerce_email_order_details
*/
add_action( 'woocommerce_email_order_details', 'custom_init_item_meta_filter_email' ), 10, 2);
function custom_init_item_meta_filter_email( $order, $sent_to_admin ){
if ( $sent_to_admin ){
add_filter('custom_filter_item_meta_admin_email', function(){ return true; } );
}else{
add_filter('custom_filter_item_meta_email', function(){ return true; } );
}
}
/**
* Is used to enable our item meta filter for invoices
* Hooked:
* @wpo_wcpdf_before_order_details
*/
add_filter( 'wpo_wcpdf_before_order_details', 'custom_init_item_meta_filter_invoice', 10, 1);
function custom_init_item_meta_filter_invoice(){
add_filter( 'custom_filter_item_meta_invoice', function(){ return true; });
}
저는 그런 "평판화" 형식으로 테스트하지 않았습니다.op 코딩된 플러그인의 다른 클래스에서 사용하고 여기에 게시하기 위해 편집했습니다.
meta_data 2 및 meta_data 3을 삭제하려면:
add_filter( 'woocommerce_display_item_meta', 'filter_woocommerce_display_item_meta', 10, 3 );
function filter_woocommerce_display_item_meta( $html, $item, $args ) {
$arrayPortionsTags = explode("<li", $html);
unset($arrayPortionsTags[2],$arrayPortionsTags[3]);
$firstLi = array( '<li' );
$lastUl = array( '</ul>' );
array_splice( $arrayPortionsTags, 1, 0, $firstLi );
array_splice( $arrayPortionsTags, 3, 0, $lastUl );
$html= implode('',$arrayPortionsTags);
return $html;
};
언급URL : https://stackoverflow.com/questions/52684334/filter-out-unwanted-order-item-meta-data-from-woocommerce-email-notifications
'programing' 카테고리의 다른 글
| WordPress 플러그인에서 여러 파일 사용 - 정의되지 않은 함수 add_action() 호출 (0) | 2023.10.04 |
|---|---|
| OpenBSD 7.3에서 Wordpress API 및 Plugins API에 액세스할 수 없음 (0) | 2023.10.04 |
| PL/SQL에서 변수의 종류를 보는 방법은? (0) | 2023.10.04 |
| 단순 유형 별칭 - Oracle 모범 사례 (0) | 2023.10.04 |
| 데이터베이스 오류: ORA-00911: 잘못된 문자 (0) | 2023.10.04 |

