WooCommerce - 发送有关自定义订单状态更改的自定义电子邮件

2024-03-24

我添加了自定义状态wc-order-confirmed:

// Register new status
function register_order_confirmed_order_status() {
    register_post_status( 'wc-order-confirmed', array(
        'label' => 'Potvrzení objednávky',
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop( 'Potvrzení objednávky <span class="count">(%s)</span>', 'Potvrzení objednávky <span class="count">(%s)</span>' )
    ) );
}
add_action( 'init', 'register_order_confirmed_order_status' );

// Add to list of WC Order statuses
function add_order_confirmed_to_order_statuses( $order_statuses ) {
    $new_order_statuses = array();
// add new order status after processing
    foreach ( $order_statuses as $key => $status ) {
        $new_order_statuses[ $key ] = $status;
        if ( 'wc-processing' === $key ) {
            $new_order_statuses['wc-order-confirmed'] = 'Potvrzení objednávky';
        }
    }
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_order_confirmed_to_order_statuses' );

我添加了自定义电子邮件wc_confirmed_order:

/**
 * A custom confirmed Order WooCommerce Email class
 *
 * @since 0.1
 * @extends \WC_Email
 */
class WC_Confirmed_Order_Email extends WC_Email {


    /**
     * Set email defaults
     *
     * @since 0.1
     */
    public function __construct() {

        // set ID, this simply needs to be a unique name
        $this->id = 'wc_confirmed_order';

        // this is the title in WooCommerce Email settings
        $this->title = 'Potvrzení objednávky';

        // this is the description in WooCommerce email settings
        $this->description = 'Confirmed Order Notification';

        // these are the default heading and subject lines that can be overridden using the settings
        $this->heading = 'Potvrzení objednávky';
        $this->subject = 'Potvrzení objednávky';

        // these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar
        $this->template_html  = 'emails/customer-confirmed-order.php';
        $this->template_plain = 'emails/plain/admin-new-order.php';

        // Trigger on confirmed orders
        add_action( 'woocommerce_order_status_pending_to_order_confirmed', array( $this, 'trigger' ) );
        add_action( 'woocommerce_order_status_processing_to_order_confirmed', array( $this, 'trigger' ) );

        // Call parent constructor to load any other defaults not explicity defined here
        parent::__construct();

        // this sets the recipient to the settings defined below in init_form_fields()
        $this->recipient = $this->get_option( 'recipient' );

        // if none was entered, just use the WP admin email as a fallback
        if ( ! $this->recipient )
            $this->recipient = get_option( 'admin_email' );
    }


    /**
     * Determine if the email should actually be sent and setup email merge variables
     *
     * @since 0.1
     * @param int $order_id
     */
    public function trigger( $order_id ) {
            // bail if no order ID is present
        if ( ! $order_id )
            return;

        if ( $order_id ) {
            $this->object       = wc_get_order( $order_id );
            $this->recipient    = $this->object->billing_email;

            $this->find['order-date']      = '{order_date}';
            $this->find['order-number']    = '{order_number}';

            $this->replace['order-date']   = date_i18n( wc_date_format(), strtotime( $this->object->order_date ) );
            $this->replace['order-number'] = $this->object->get_order_number();
        }


        if ( ! $this->is_enabled() || ! $this->get_recipient() )
            return;

        // woohoo, send the email!
        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }


    /**
     * get_content_html function.
     *
     * @since 0.1
     * @return string
     */
    public function get_content_html() {
        ob_start();
        wc_get_template( $this->template_html, array(
            'order'         => $this->object,
            'email_heading' => $this->get_heading()
        ) );
        return ob_get_clean();
    }


    /**
     * get_content_plain function.
     *
     * @since 0.1
     * @return string
     */
    public function get_content_plain() {
        ob_start();
        wc_get_template( $this->template_plain, array(
            'order'         => $this->object,
            'email_heading' => $this->get_heading()
        ) );
        return ob_get_clean();
    }


    /**
     * Initialize Settings Form Fields
     *
     * @since 2.0
     */
    public function init_form_fields() {

        $this->form_fields = array(
            'enabled'    => array(
                'title'   => 'Enable/Disable',
                'type'    => 'checkbox',
                'label'   => 'Enable this email notification',
                'default' => 'yes'
            ),
            'recipient'  => array(
                'title'       => 'Recipient(s)',
                'type'        => 'text',
                'description' => sprintf( 'Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', esc_attr( get_option( 'admin_email' ) ) ),
                'placeholder' => '',
                'default'     => ''
            ),
            'subject'    => array(
                'title'       => 'Subject',
                'type'        => 'text',
                'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ),
                'placeholder' => '',
                'default'     => ''
            ),
            'heading'    => array(
                'title'       => 'Email Heading',
                'type'        => 'text',
                'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ),
                'placeholder' => '',
                'default'     => ''
            ),
            'email_type' => array(
                'title'       => 'Email type',
                'type'        => 'select',
                'description' => 'Choose which format of email to send.',
                'default'     => 'html',
                'class'       => 'email_type',
                'options'     => array(
                    'plain'     => __( 'Plain text', 'woocommerce' ),
                    'html'      => __( 'HTML', 'woocommerce' ),
                    'multipart' => __( 'Multipart', 'woocommerce' ),
                )
            )
        );
    }


} // end \WC_confirmed_Order_Email class

我可以在电子邮件设置中查看电子邮件,并在订单状态下拉列表中查看状态。现在,每当订单状态更改为时,我都需要发送新电子邮件wc-order-confirmed。过渡钩子似乎永远不会触发。

我也尝试过:

/**
 * Register the "woocommerce_order_status_pending_to_quote" hook which is necessary to
 * allow automatic email notifications when the order is changed to refunded.
 *
 * @modified from http://stackoverflow.com/a/26413223/2078474 to remove anonymous function
 */
add_action( 'woocommerce_init', 'so_25353766_register_email' );
function so_25353766_register_email(){
    add_action( 'woocommerce_order_status_pending_to_order_confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );
    add_action( 'woocommerce_order_status_processing_to_order_confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );
}

这似乎也根本不起作用......请问有什么想法吗?


您需要的钩子是:

woocommerce_order_status_changed

add_action("woocommerce_order_status_changed", "my_awesome_publication_notification");

function my_awesome_publication_notification($order_id, $checkout=null) {
   global $woocommerce;
   $order = new WC_Order( $order_id );
   if($order->status === 'completed' ) {
      // Create a mailer
      $mailer = $woocommerce->mailer();

      $message_body = __( 'Hello world!!!' );

      $message = $mailer->wrap_message(
        // Message head and message body.
        sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );


      // Cliente email, email subject and message.
     $mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message );
     }

   }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WooCommerce - 发送有关自定义订单状态更改的自定义电子邮件 的相关文章

随机推荐

  • 连接整数变量最惯用的方法是什么?

    编译器似乎没有推断出整数变量作为字符串文字传递到concat 宏 所以我找到了stringify 将这些整数变量转换为字符串文字的宏 但这看起来很难看 fn date year u8 month u8 day u8 gt String co
  • 加载我的包时 Symfony 容器没有扩展

    我有一个捆绑包 在一段时间内运行良好 但是 我必须向其中添加一些自定义配置参数 因此我在包的 config yml 中编写了一些行 如下所示 acme my bundle special params param 1 param 2 配置在
  • 带有模块的 Ruby 类命名空间:为什么我会收到带有双冒号的 NameError 而不是模块块?

    我正在处理许多预先存在的文件 类和模块 并尝试为框架的不同组件提供更好的命名空间 我一直使用模块作为命名空间的方式 主要是因为这似乎是标准约定 并且能够 包含 框架的不同部分可能很有用 问题在于 全局命名空间下有大量本应存在于模块下的类 例
  • 什么是编程中的“序列化”对象? [复制]

    这个问题在这里已经有答案了 我到处都看到过 序列化 这个词 但从未解释过 请解释一下这是什么意思 序列化通常是指将抽象数据类型转换为字节流的过程 有时也序列化为文本 XML 或 CSV 或其他格式 重要的是它是一种简单的格式 无需理解即可读
  • 使用 ui 路由器实例化作用域和控制器

    我对控制器何时实例化感到困惑 另外 在嵌套状态时控制器如何实例化 我可能会感到困惑范围如何附加到视图和控制器 也就是说 如果每个视图都有自己的控制器和范围 或者它们共享相同的范围 有人可以解释一下控制器何时被实例化吗 在嵌套路由下 所有视图
  • 获取 Gallery Intent 选择的图像路径时出错(Android 6 - 某些设备)

    当用户从图库中选择时 有意 我试图获取图像的路径 它一直工作正常 因为一些用户注意到 Android 6 0 无法做到这一点 我尝试过不同的方法 有些解决方案可以在 Android 6 0 的模拟器中运行 但不能在我的 Android 6
  • 如何退出 Android 应用程序?

    我刚刚读到 您只需调用以下命令即可退出 Android 应用程序 finish 然而 这种情况并非如此 当我这样做时 我收到以下错误 PackageInstallationReciever Remove data local tmp com
  • 为 SSL 配置 MAMP

    好吧 各位编码员 我正在尝试在我的 mac 上使用 SSL 配置 MAMP 以用于开发目的 我已阅读并尝试了以下说明 http www emersonlackey com article mamp with ssl https http w
  • Groovy 执行“cp *”shell 命令

    我想复制文本文件并且仅复制来自src to dst groovy 000 gt cp src txt dst execute text gt groovy 000 gt 您可以看到命令执行时没有错误 但文件src test txt不会被复制
  • 隐藏 webBrowser 控件中的滚动条

    我正在研究 Windows 窗体的 HTML 显示控件 我使用 webBrowser 控件作为控件的基础 我需要隐藏 webBrowser 滚动条 因为它看起来很糟糕 永远不会被使用 并且使控件看起来像网页 从而破坏了布局 目前 滚动条在控
  • .Net core 3:手动添加框架依赖项

    自从3 0版本发布以来 现在可以在 net core中编写WPF应用程序 这真是太棒了 另一方面 在 net core 上 依赖系统现在依赖于完整的框架 不再有多个 nuget 依赖项 除非您想要在同一个应用程序中混合使用 WPF 和 AS
  • Java,BorderLayout.CENTER,获取JPanel的宽度和高度

    我正在使用 Swing 和 AWT 针对听众 制作一个小程序 我在获取 JPanel 名为 Chess 的类 的大小时遇到 问题 我的布局 public class Main extends JFrame implements MouseL
  • 在 Typo3 中实现 HTML 模板,内容不起作用或者是我的错误

    我尝试在typo3中实现html模板 通过本教程 http wiki typo3 org Templated Tutorial Basics http wiki typo3 org Templating Tutorial Basics 所有
  • 使用 xsi:nil="true" C# 序列化删除 xml 元素

    我有一个 XML 其中包含一些值 有时可能存在空值 如下所示 我根本不希望在 XML 中列出带有 null 的节点 元素已设置IsNullable true在课堂里 任何建议 因为我在谷歌中尝试了很多东西 没有任何帮助
  • 更改 pandas 中的默认选项

    我想知道是否有任何方法可以更改 pandas 的默认显示选项 我想在每次运行 python 时更改显示格式和显示宽度 例如 pandas options display width 150 我看到默认值是硬编码的pandas core co
  • 部署.NET Web应用程序时如何获取预编译的razor文件?

    我的任务是改进服务器上应用程序的 IIS 预加载和初始化 我已经在IIS上实现了应用程序初始化和应用程序预加载 但回收 重新启动应用程序池时仍然有很长的等待时间 我找到了一些有用的链接 我认为这些链接对我有帮助 但我仍然没有获得预编译的 R
  • 通过引用切片为不可变字符串,而不是复制

    如果你使用string split http docs python org library stdtypes html str split对于 Python 字符串 它返回字符串列表 这些已拆分的子字符串是其父字符串部分的副本 是否有可能
  • Spring Boot 中的代理设置

    我的应用程序需要从 Web 获取 XML 文件 如下所示 Bean public HTTPMetadataProvider metadataProvider throws MetadataProviderException String m
  • 未排序数组中的前 5 个元素

    给定一个未排序的数组 我们需要以有效的方式找到前 5 个元素 但我们无法对列表进行排序 我的解决方案 找到数组中的最大元素 在 处理 使用此最大元素后删除它 重复步骤 1 和 2 k 次 本例中为 5 次 时间复杂度 O kn O n 空间
  • WooCommerce - 发送有关自定义订单状态更改的自定义电子邮件

    我添加了自定义状态wc order confirmed Register new status function register order confirmed order status register post status wc o