programing

WooCommerce 이메일 알림의 상품 카테고리에 따라 받는 사람이 다름

starjava 2023. 9. 19. 20:42
반응형

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

반응형