在 WooCommerce 4+ 中添加发送电子邮件通知的新订单状态 [英] Add a new order status that Send an email notification in WooCommerce 4+

查看:31
本文介绍了在 WooCommerce 4+ 中添加发送电子邮件通知的新订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将新的订单状态和电子邮件通知添加到我的网站并与此订单状态相关联.我试过这个代码:

I would like to add a new order status and email notification to my site in conjunction with this order status. I tried this code:

// register a custom post status 'awaiting-delivery' for Orders
add_action( 'init', 'register_custom_post_status', 20 );
function register_custom_post_status() {
    register_post_status( 'wc-awaiting-delivery', array(
        'label'                     => _x( 'Kargoya Verildi', 'Order status', 'woocommerce' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Kargoya Verildi <span class="count">(%s)</span>', 'Kargoya Verildi <span class="count">(%s)</span>', 'woocommerce' )
    ) );
}

// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses', 20, 1 );
function custom_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-awaiting-delivery'] = _x( 'Kargoya Verildi', 'Order status', 'woocommerce' );
    return $order_statuses;
}

// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
    $actions['mark_awaiting-delivery'] = __( 'Kargoya Verildi', 'woocommerce' );
    return $actions;
}

// Adding action for 'awaiting-delivery'
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $action ) {
    $actions[] = 'woocommerce_order_status_wc-awaiting-delivery';
    return $actions;
}

add_action( 'woocommerce_order_status_wc-awaiting-delivery', array( WC(), 'send_transactional_email' ), 10, 1 );

// Sending an email notification when order get 'awaiting-delivery' status
add_action('woocommerce_order_status_awaiting-delivery', 'backorder_status_custom_notification', 20, 2);
function backorder_status_custom_notification( $order_id, $order ) {
    // HERE below your settings
    $heading   = __('Kargoya Verildi','woocommerce');
    $subject   = '[{site_title}] Siparişiniz Kargoya Verildi ({order_number}) - {order_date}';

    // Getting all WC_emails objects
    $mailer = WC()->mailer()->get_emails();

    // Customizing Heading and subject In the WC_email processing Order object
    $mailer['WC_Email_Customer_Processing_Order']->heading = $heading;
    $mailer['WC_Email_Customer_Processing_Order']->subject = $subject;

    // Sending the customized email
    $mailer['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}

但代码不发送电子邮件.我该如何解决这种情况?我尝试了其他一些代码.但它们都没有奏效.

But the code doesn't send e-mail. How can I fix this situation? I tried a few other codes. But none of them worked.

注意:我正在 function.php 文件中输入此代码,我需要将它写在不同的地方吗?

NOTE: I am typing this code in the function.php file, do I need to write it somewhere different?

推荐答案

基于 此答案代码这个未接受的回答线程,这是重新访问的代码,它将为 WooCommerce 订单添加自定义状态并触发自定义电子邮件通知对于此自定义状态:

Based on this answer code and this unaccepted answer thread, Here is the revisited code, that will add a custom status to WooCommerce orders and will trigger a customized email notification for this custom status:

// register a custom post status 'awaiting-delivery' for Orders
add_action( 'init', 'register_custom_post_status', 20 );
function register_custom_post_status() {
    register_post_status( 'wc-awaiting-delivery', array(
        'label'                     => _x( 'Kargoya Verildi', 'Order status', 'woocommerce' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Kargoya Verildi <span class="count">(%s)</span>', 'Kargoya Verildi <span class="count">(%s)</span>', 'woocommerce' )
    ) );
}

// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-awaiting-delivery'] = _x( 'Kargoya Verildi', 'Order status', 'woocommerce' );
    return $order_statuses;
}

// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
    $actions['mark_awaiting-delivery'] = __( 'Kargoya Verildi', 'woocommerce' );
    return $actions;
}

// Adding action for 'awaiting-delivery'
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $actions ) {
    $actions[] = 'woocommerce_order_status_wc-awaiting-delivery';
    return $actions;
}

add_action( 'woocommerce_order_status_wc-awaiting-delivery', array( WC(), 'send_transactional_email' ), 10, 1 );

// Sending an email notification when order get 'awaiting-delivery' status
add_action('woocommerce_order_status_awaiting-delivery', 'awaiting_delivery_order_status_email_notification', 20, 2);
function awaiting_delivery_order_status_email_notification( $order_id, $order ) {
    // HERE below your settings
    $heading   = __('Kargoya Verildi','woocommerce');
    $subject   = '[{site_title}] Siparişiniz Kargoya Verildi ({order_number}) - {order_date}';

        // The email notification type
        $email_key   = 'WC_Email_Customer_Processing_Order';

        // Get specific WC_emails object
        $email_obj = WC()->mailer()->get_emails()[$email_key];

        // Sending the customized email
        $email_obj->trigger( $order_id );
}

// Customize email heading for this custom status email notification
add_filter( 'woocommerce_email_heading_customer_processing_order', 'email_heading_customer_awaiting_delivery_order', 10, 2 );
function email_heading_customer_awaiting_delivery_order( $heading, $order ){
    if( $order->has_status( 'awaiting-delivery' ) ) {
        $email_key   = 'WC_Email_Customer_Processing_Order'; // The email notification type
        $email_obj   = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
        $heading_txt = __('Kargoya Verildi','woocommerce'); // New heading text

        return $email_obj->format_string( $heading_txt );
    }
    return $heading;
}

// Customize email subject for this custom status email notification
add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_customer_awaiting_delivery_order', 10, 2 );
function email_subject_customer_awaiting_delivery_order( $subject, $order ){
    if( $order->has_status( 'awaiting-delivery' ) ) {
        $email_key   = 'WC_Email_Customer_Processing_Order'; // The email notification type
        $email_obj   = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
        $subject_txt = sprintf( __('[%s] Siparişiniz Kargoya Verildi (%s) - %s', 'woocommerce'), '{site_title}', '{order_number}', '{order_date}' ); // New subject text

        return $email_obj->format_string( $subject_txt );
    }
    return $subject;
}

代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于在 WooCommerce 4+ 中添加发送电子邮件通知的新订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆