通知新订单的电子邮件将发送给在 woocommerce 上发布产品的人员

问题描述 投票:0回答:1

我不使用供应商插件,而是使用wordpress用户授权功能来允许自定义用户“chu_xe”发布和管理他们发布的产品。 我希望当他们发布的产品产生订单时,将会向他们发送一封新订单的电子邮件通知(产品发布者的电子邮件)。 我已将其配置为每个订单仅允许客户订购 1 个产品。 请帮助我,谢谢。这是我应用的代码,但它不起作用。

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); }

我尝试了很多代码,但不起作用,没有电子邮件发送给发布产品的人

woocommerce
1个回答
0
投票

您的代码中有错误。请尝试以下方法:

// 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();
    }
}

代码位于子主题的functions.php 文件中(或插件中)。应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.