우커머스 고객 이메일에 안내문구 추가

우커머스 고객 이메일 고객메모 하단에 커스텀필드의 안내문구 추가

add_action('woocommerce_email_after_order_table', 'add_custom_message_after_customer_note', 20, 4);

function add_custom_message_after_customer_note($order, $sent_to_admin, $plain_text, $email) {
    // 주문 아이템 가져오기
    $items = $order->get_items();
    
    if (empty($items)) {
        return;
    }
    
    $custom_messages = array();
    
    // 각 상품의 custom_message 수집
    foreach ($items as $item) {
        $product = $item->get_product();
        
        if (!$product) {
            continue;
        }
        
        $product_id = $product->get_id();
        $custom_message = get_field('custom_message', $product_id);
        
        if ($custom_message) {
            $product_name = $product->get_name();
            $custom_messages[] = array(
                'product' => $product_name,
                'message' => $custom_message
            );
        }
    }
    
    // 커스텀 메시지가 있으면 표시
    if (!empty($custom_messages)) {
        if ($plain_text) {
            // 텍스트 이메일 형식
            echo "\n----------------------------------------\n";
            echo "상품 안내 메시지\n";
            echo "----------------------------------------\n\n";
            
            foreach ($custom_messages as $msg) {
                echo "[" . $msg['product'] . "]\n";
                echo $msg['message'] . "\n\n";
            }
        } else {
            // HTML 이메일 형식
            echo '<div style="margin-top: 20px; margin-bottom: 40px; padding: 15px; background-color: #f7f7f7; border: 1px solid #e5e5e5;">';
            echo '<h3 style="margin-top: 0;">멤버십 안내</h3>';
            
            foreach ($custom_messages as $msg) {
                echo '<p style="margin: 10px 0;">';
                echo '<strong>' . esc_html($msg['product']) . '</strong><br>';
                echo esc_html($msg['message']);
                echo '</p>';
            }
            
            echo '</div>';
        }
    }
}

ACF에서 custom_message 필드 추가 후 안내문구

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다