当创建默认WooCommerce新订单时,我正在try 将额外的邮箱发送到所需的邮箱地址.并希望默认的新订单仍然有效.一切都很好,也收到了邮件.

但对于定制邮箱,我没有得到订单细节-我只想要没有价格,客户笔记和订单编号和日期的项目细节.

以下是代码:

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 = 'desired@email.com'; // Replace with the desired email address

    // Send the desired email with the order details
    wp_mail( $desired_email, $email_subject, $email_content );
}

推荐答案

try 以下重新访问和增强的代码版本:

add_action( 'woocommerce_new_order', 'send_custom_new_order_email', 10, 2 );
function send_custom_new_order_email( $order_id, $order = null ) {
        // Get the order object if it doesn't exist
    if ( ! $order && $order_id > 0 ) {
        $order = wc_get_order( $order_id );
    }

    $order_details = ''; // Initialize

    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        $product        = $item->get_product(); // get the WC_Product Object
        $product_name   = $product ? strip_tags($product->get_formatted_name()) : $item->get_name(); // Product name

        // Add to the order details (for each item)
        $order_details .= "Product: {$product_name} - Quantity: {$item->get_quantity()}\n";
    }

    // Email content: Include the desired details below
    $email_content  = "Order Date: {$order->get_date_created()->format( 'F j, Y' )}\n";
    $email_content .= "Order Number: {$order->get_order_number()}\n\n";
    $email_content .= "Order Details:\n{$order_details}\n";
    $email_content .= "Customer Note: {$order->get_customer_note()}\n";

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

    // Email recipient: Replace with the desired email recipient
    $desired_email  = 'someone@email.com';

    // Avoid sending the same email multiple times
    if( did_action('woocommerce_new_order') == 1 ) {
        // Send the desired email with the order details
        wp_mail( $desired_email, $email_subject, $email_content );
    }
}

这一次,它将显示购买的产品名称(带有数量),并避免多次发送相同的邮箱.

Php相关问答推荐

如何隐藏x轴图表上的值

过滤WooCommerce管理员订单列表(+HPOS)中包含其作者产品的订单

PHP DateInterval天数不一致

为什么本地主机(Xampp)上的laravel运行速度比普通html站点慢?

根据选定的字段值显示或隐藏WooCommerce注册字段

删除了数组中的值,而不是多个数组

如何在Livewire 3 Form类中做依赖注入?

为什么只有最后一次点击的点赞/心形会按预期改变 colored颜色 ,而其他的保持正常 colored颜色 ?

FlySystem v3:使用本地适配器时出现问题

在PHP中使用curl命令执行SCRAPYD操作.json不返回任何内容

shell_exec运行大型数据处理

为什么PHP PDO忽略NOT NULL约束?

更改WooCommerce checkout 中的错误消息以获得不可用的送货方式

调用模型[App\Models\Employee]上未定义的关系[title]

如何在 Laravel 迁移中使用日期时间和天数?

如何正确判断 PHP 是否已正确配置为使用 DOMDocument?

在 vue 自定义渲染器中访问变量

如何使用动态变量定义 bind_result?

在 WordPress 中使用 $wpdb 运行 MySQL 事务是否安全?

为什么非贪婪匹配会消耗整个模式,即使后面跟着另一个非贪婪匹配