WooCommerce 项目无法检索详细信息

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

当创建默认的 WooCommerce 新订单时,我试图向所需的电子邮件地址发送一封额外的电子邮件。并希望默认的新订单仍然有效。一切都很好,也收到了电子邮件。

但是对于自定义电子邮件,我没有收到订单详细信息 - 我只想要商品详细信息,没有价格、客户备注以及订单号和日期。

这是代码:

add_action( 'woocommerce_new_order', 'send_custom_order_email', 10, 1 );
function send_custom_order_email( $order_id ) {
    // Get the order object
    $order = wc_get_order( $order_id );

    // Prepare order details without prices
    $order_details = '';
    foreach ( $order->get_items() as $item_id => $item ) {
        $product = $item->get_product();
        $product_name = $product ? $product->get_formatted_name() : $item['name'];
        $item_quantity = $item->get_quantity();

        $order_details .= "Product: $product_name - Quantity: $item_quantity\n";
    }

    // Include the desired details in the email content
    $details_to_include = "Order Date: {$order->get_date_created()->format( 'F j, Y' )}\nOrder Number: {$order->get_order_number()}\n\nOrder Details:\n$order_details\nCustomer Note: {$order->get_customer_note()}\n";

    // Email subject and content
    $email_subject = 'New Order: ' . $order->get_order_number();
    $email_content = $details_to_include;

    // Email recipient
    $desired_email = '[email protected]'; // Replace with the desired email address

    // Send the desired email with the order details
    wp_mail( $desired_email, $email_subject, $email_content );
}
php wordpress woocommerce hook-woocommerce
© www.soinside.com 2019 - 2024. All rights reserved.