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 - 发送有关自定义订单状态更改的自定义电子邮件 的相关文章

  • 检查 $_POST 数据

    我正在对表单进行一些垃圾邮件检查 下面的代码在我的本地主机上正常工作 如果为 true 则重定向到 google com 但是 当它在生产服务器上时却不起作用 执行脚本的其余部分并且不重定向到 Google com if POST SERV
  • 如何从父类中获取子类名

    我试图在不需要子类上的函数的情况下完成此任务 这可能吗 我有一种感觉 但我真的很想确定
  • 如何在laravel中注册后自动登录

    我在 laravel 中注册用户时遇到问题 user假设是包含所有数组元素的数组 同时自动登录以下代码结果false 数据库中保存的密码是hash make password user id this gt user model gt ad
  • 一种无需 JavaScript 即可在 PHP 中确定浏览器宽度的方法?

    首先有吗 或者我必须使用javascript 我希望能够更改使用的 CSS 因此 frex 我可以为移动设备或其他设备加载较小的字体 不幸的是 仅使用 PHP 无法检测用户分辨率 如果您使用 Javascript 则可以在 cookie 中
  • setcookie with expire=0 浏览器关闭后不会过期

    我使用setcookie来制作一个过期 0的cookie 从 PHP 文档来看 link http php net manual en function setcookie php cookie 过期的时间 这是一个 Unix 时间戳 所以
  • 无法访问 localhost/xampp/index.php

    我刚刚安装了 Windows 7 的 XAMPP 控制面板似乎工作正常 我启动了 MySql 和 Apache 我遇到的问题是 当我在浏览器 Google Chrome 中输入 localhost 时 它会将我发送到 http localh
  • CakePHP 视图包括其他视图

    我有一个 CakePHP 应用程序 在某些时候会显示带有产品媒体 图片或视频 的视图 我想知道是否有某种方式可以包含另一个威胁视频或威胁图片的视图 具体取决于标志 我想将这些 小视图 用于其他几个目的 所以它应该 像 蛋糕组件一样 以便重用
  • Xdebug V3 不会停止 VSCode 中的断点

    我正在尝试使用 VSCode 在 XAMPP 上进行调试 但没有成功 我知道有很多关于这个的问题 我已经尽了一切努力 但仍然行不通 我的 xdebug 扩展确实有一件奇怪的事情 我目前使用 PHP v7 4 12 和 Xdebug 版本 3
  • PDO PHP 连接,致命错误

    我的连接类 firstcode php class DB functions public db function construct try db new PDO mysql localhost dbname xxx charset ut
  • 分页显示所有其他页面上第 1 页的相同帖子

    我最近在创建即将发生的事件列表时得到了很多帮助 请参阅此处显示即将举行的活动 包括今天的活动 https stackoverflow com questions 17343615 showing upcoming events includ
  • 覆盖 FOS 用户包中的“更改密码”模板

    我做了一些研究 遗憾的是找不到任何帮助 因此 我将 FOSUserBundle ChangePasswordAction 渲染到我的模板中 但它显示供应商提供的默认模板 我的渲染控制器的模板 block body h2 Einstellun
  • 使用值填充的 Symfony2 自定义字段类型

    这是先前问题的后续问题Symfony2 自定义表单类型或扩展 https stackoverflow com questions 24079288 symfony2 custom form type or extension 我正在尝试为订
  • Facebook PHP-SDK 页面刷新后似乎丢失了 userID

    我似乎登录工作正常 我可以登录 接受应用程序 第一次 然后显示用户信息 例如姓名 图片 等 然而 当我刷新页面时 userid 又回到 0 我必须再次登录 我不确定问题是什么 我必须在每次页面加载时重新启动它还是什么 我不知道 我会发布一些
  • PHP 编码风格回归;在开关/外壳中

    我们正在尝试为我们的团队实施新的编码风格指南 当未找到 break 时 php codeniffer 会在 switch case 语句上打印警告 如下所示 switch foo case 1 return 1 case 2 return
  • PHP 检查当前日期是在设定日期之前还是之后

    我从数据库中提取一个日期 其格式为 dd mm YYYY 我想做的是检查当前日期 如果当前日期早于数据库中的日期 则需要打印数据库日期 如果是在之后 则需要打印 继续 有人能指出我正确的方向吗 if strtotime database d
  • 使用 php/regex 验证美国电话号码

    EDIT 我混合并修改了下面给出的两个答案 以形成完整的功能 现在它可以完成我想要的功能 然后是一些 所以我想我会将其发布在这里 以防其他人来寻找同样的东西 Function to analyze string against many p
  • 使用会话 php 创建 cookie?

    我使用会话来登录我网站中的用户 问题是 我想让用户remember密码 因此关闭 打开浏览器后他们不需要再次登录 我需要使用 cookie 和 session 来实现它吗 my code user POST user pass POST p
  • 使用 Vue 的多模式组件

    我在 Vue 中实现动态模式组件时遇到问题 A common approach I follow to display a set of data fetched from the db is I dump each of the rows
  • PayPal 网关已拒绝请求。安全标头无效(#10002:安全错误 Magento

    在 magento 中增加 PayPal 预付款 我已填写 magento admin 中的所有凭据 但是当我进入前端并单击 pay pal 按钮时 它给出了 PayPal 网关已拒绝请求 安全标头无效 10002 安全错误 我用谷歌搜索了
  • PHPUnit - 模拟 S3Client 无法正常工作

    库 aws aws sdk php 2 PHP 版本 PHP 5 4 24 cli 作曲家 json require php gt 5 3 1 aws aws sdk php 2 require dev phpunit phpunit 4

随机推荐

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

    编译器似乎没有推断出整数变量作为字符串文字传递到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