I don't use a vendor plugin, but use the wordpress user authorization feature to allow a custom user "chu_xe" to post and manage the products they publish. I want when there is an order arising from a product they have published, there will be an email notification of a new order sent to them (Email of the product publisher). I have configured it so that customers are only allowed to order 1 product per order. Please help me, thank you. Here is the code I applied but it is not working.
add_action('woocommerce_checkout_order_processed', 'send_email_to_product_publisher_on_new_order', 10, 1);
function send_email_to_product_publisher_on_new_order($order_id) {
if (!$order_id) return;
// Lấy đối tượng đơn hàng
$order = wc_get_order($order_id);
if (!$order) return;
// Vì chỉ có một sản phẩm, chúng ta lấy sản phẩm đầu tiên trong đơn hàng
$items = $order->get_items();
$item = reset($items);
if (!$item) return;
$product_id = $item->get_product_id();
$author_id = get_post_field('post_author', $product_id);
$author = get_userdata($author_id);
if (!$author) return;
$author_email = $author->user_email;
$author_name = $author->display_name;
if (!$author_email) return; // Nếu không có email, không tiếp tục
// Tiêu đề và nội dung email
$subject = 'Thông báo: Bạn vừa nhận được đơn hàng mới!';
$message = "Xin chào $author_name,\n\nBạn vừa nhận được một đơn hàng mới cho sản phẩm mà bạn đã đăng trên website của chúng tôi.\n";
$message .= "Thông tin đơn hàng:\n";
$message .= "Số đơn hàng: " . $order->get_order_number() . "\n";
$message .= "Tổng giá trị: " . wc_price($order->get_total()) . "\n";
$message .= "Bạn có thể xem chi tiết đơn hàng tại đây: " . $order->get_view_order_url() . "\n\n";
$message .= "Cảm ơn bạn đã đóng góp sản phẩm tuyệt vời của bạn cho cộng đồng của chúng tôi!";
// Headers
$headers = array('Content-Type: text/plain; charset=UTF-8');
// Gửi email
wp_mail($author_email, $subject, $message, $headers); }
I tried many codes but it doesn't work, no email is sent to the person who posted the product
There are mistakes in your code. Try the following instead:
// Utility function: Send an email to a product publisher from an order item
function send_email_to_product_publisher( $item, $order ) {
$product_id = $item->get_product_id();
$author_id = get_post_field('post_author', $product_id);
$author = get_userdata($author_id);
if ( $author_email = $author->user_email ) {
$subject = 'Notification: You just received a new order!';
$message = "Hello {$author->display_name},\n\nYou have just received a new order for a product you posted on our website\n";
$message .= "Order information:\n";
$message .= "Order number: " . $order->get_order_number() . "\n";
$message .= "Total value: " . wc_price($order->get_total()) . "\n";
$message .= "You can view order details here: " . $order->get_view_order_url() . "\n\n";
$message .= "Thank you for contributing your great product to our community!";
$headers = "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= 'From: ' . get_bloginfo('admin_email') . "\r\n";
wp_mail( $author_email, $subject, $message, $headers );
}
}
// Trigger an email to related product publishers from an order on a new order
add_action( 'woocommerce_order_status_pending_to_processing_notification', 'new_order_email_to_product_publisher', 10, 2 );
add_action( 'woocommerce_order_status_pending_to_completed_notification', 'new_order_email_to_product_publisher', 10, 2 );
add_action( 'woocommerce_order_status_pending_to_on-hold_notification', 'new_order_email_to_product_publisher', 10, 2 );
function send_email_to_product_publisher_on_new_order( $order_id, $order = false ) {
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( ! $order->get_meta('_new_order_email_sent_to_product_publishers') ) {
// Loop through order items
foreach( $order->get_items() as $item ) {
send_email_to_product_publisher( $item, $order );
}
$order->update_meta_data( '_new_order_email_sent_to_product_publishers', '1' );
$order->save();
}
}
Code goes on functions.php file of your child theme (or in a plugin). It should work.