WooCommerce 이메일 알림의 상품 카테고리에 따라 받는 사람이 다름
가상상품(수수료, 수학여행비)과 물리(유니폼)을 함께 판매하는 학교 사이트를 만들고 있는데, 각 카테고리별 주문 알림을 부서별로 처리해서 따로 받는 분들께 보내고 싶습니다.
예를 들어, 모든 균일한 범주 주문은 수신자 1로 가고, 다른 범주 주문은 수신자 2로 갑니다.제품에 따라 맞춤형 이메일 메시지를 보낼 수 있는 방법을 여러 가지 발견했지만, 이 중 어느 것도 제 요구사항에 부합하지 않습니다.
또한 Woocommerce Advanced Notifications와 같은 플러그인을 사용하지 않고도 이것을 할 수 있기를 바랍니다.
어떤 도움이라도 주시면 대단히 감사하겠습니다.
다음은 사용자의 제품 범주를 기반으로 한 "새 주문" 전자 메일 알림에 전자 메일 수신자/제품 범주 쌍으로 색인화된 배열로 정의할 수신자를 추가합니다.
add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
function custom_email_recipient_new_order( $recipient, $order ) {
// Not in backend when using $order (avoiding an error)
if( ! is_a($order, 'WC_Order') ) return $recipient;
// Define the email recipients / categories pairs in the array
$recipients_categories = array(
'email.one@email.com' => 'category-one',
'email.two@email.com' => 'category-two',
'email.three@email.com' => 'category-three',
);
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Loop through defined product categories
foreach ( $recipients_categories as $email => $category ) {
if( has_term( $category, 'product_cat', $item->get_product_id() ) && strpos($recipient, $email) === false ) {
$recipient .= ',' . $email;
}
}
}
return $recipient;
}
코드는 기능을 합니다.활성 하위 테마(또는 활성 테마)의 php 파일입니다.테스트를 거쳐 작동합니다.
주의:
- 정의된 제품 범주는 용어 ID, 용어 슬러그 또는 용어 이름일 수 있습니다.
- has_term() WordPress 조건부 함수가 상위 용어를 처리하지 않으므로 각 상품 카테고리를 관련 상품에 정의해야 합니다.
###부모 제품 범주를 처리하기 위한 추가:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
// Adding custom recipients based on product categories
add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
function custom_email_recipient_new_order( $recipient, $order ) {
// Not in backend when using $order (avoiding an error)
if( ! is_a($order, 'WC_Order') ) return $recipient;
// Define the email recipients / categories pairs in the array
$recipients_categories = array(
'email.one@email.com' => 'category-one',
'email.two@email.com' => 'category-two',
'email.three@email.com' => 'category-three',
);
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Loop through defined product categories
foreach ( $recipients_categories as $email => $category ) {
if( has_product_categories( $item->get_product_id(), array( $category ) ) && strpos($recipient, $email) === false ) {
$recipient .= ',' . $email;
}
}
}
return $recipient;
}
코드는 기능을 합니다.활성 하위 테마(또는 활성 테마)의 php 파일입니다.테스트를 거쳐 작동합니다.
참고: 제품 범주는 용어 ID, 용어 슬러그 또는 용어 이름이 될 수 있습니다.
유사 : WooCommerce e-메일 알림에서 판매되는 상품에 따라 받는 사람이 다름
언급URL : https://stackoverflow.com/questions/54034812/different-recipients-based-on-product-category-in-woocommerce-email-notification
'programing' 카테고리의 다른 글
Facebook like 버튼을 동적으로 로드된 컨텐츠와 통합합니다. (0) | 2023.09.19 |
---|---|
JavaScript를 사용하여 URL 유효성 검사 시도 중 (0) | 2023.09.19 |
페이지를 기록에서 제거하면 "뒤로"가 제대로 작동합니다. (0) | 2023.09.19 |
jQuery를 사용하여 DIV 배경 영상 (0) | 2023.09.19 |
JQuery vs ExtJS (0) | 2023.09.19 |