当前位置: 首页 > 知识库问答 >
问题:

WooCommerce-在自定义订单状态更改时发送自定义电子邮件

葛桐
2023-03-14

我添加了自定义状态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 );
}

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

共有3个答案

仲孙阳
2023-03-14

正如您在这里所看到的:https://github.com/woothemes/woocommerce/blob/f8a161c40673cb019eb96b04c04a774ca040a15a/includes/abstracts/abstract-wc-order.php#l2097您可以使用这个挂钩:

do_action('woocommerce_order_status_'.$new_status,$this->id);

您的自定义状态应该给出:

add_action('woocommerce_order_status_wc-order-confirmed',数组($this,'trigger'));

我想您也将自定义电子邮件添加到邮件程序中,如果没有:

只要加上:

add_filter( 'woocommerce_email_classes', array($this,'edit_woocommerce_email_classes' ));
function edit_woocommerce_email_classes( $email_classes ) {
    require_once( 'your-email-class.php' );
    $email_classes[ 'WC_Confirmed_Order_Email' ] = new WC_Confirmed_Order_Email();
    return $email_classes;
 }

编辑:

null

add_action( 'init' , 'initiate_woocommerce_email' );

function initiate_woocommerce_email(){
   // Just when you update the order_status on backoffice
   if( isset($_POST['order_status']) ) {
        WC()->mailer();
    }
}
黄正浩
2023-03-14

你需要的钩子是:

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 );
     }

   }
}
贺俊材
2023-03-14

正如XCID的答案所指出的,您需要注册电子邮件。

在WC2.2+中,我相信您可以通过以下方法来实现这一点:

add_action( 'woocommerce_order_status_wc-order-confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );

我在WooCommerce 2.3中添加了一个过滤器,所以当它出现时,自定义电子邮件将能够添加到WooCommerce注册的电子邮件操作列表中:

// As of WooCommerce 2.3
function so_27112461_woocommerce_email_actions( $actions ){
    $actions[] = 'woocommerce_order_status_wc-order-confirmed';
    return $actions;
}
add_filter( 'woocommerce_email_actions', 'so_27112461_woocommerce_email_actions' );
 类似资料:
  • 我在我的WooCommerce安装中创建了一个自定义订单状态,称为Quote。 现在我想收到一封电子邮件,每当收到一个订单,已经给出了状态栏。我根据这篇有用的文章创建了一个插件:http://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/ 我的插件基本上是从文章复制的,我只是更改了电子邮件的内容。我想改变的是什么触发了电子邮

  • 我已经在我的WooCommerce回拨订单()中创建了自定义订单状态: 现在我想收到一封电子邮件,每当收到一个订单,已经给出了状态栏。我根据这篇有用的文章创建了一个插件:如何添加自定义的WooCommerce电子邮件 这个链接包含我的插件源代码和functions.php代码。 我在function.php中添加了钩子: 当订单更改为“延迟订单”状态时,不会发生任何情况。 有什么想法吗? 我已经尝

  • 我创建了一个自定义电子邮件类并将其添加到Woocommerce中。当我转到WooCommerce中的电子邮件设置时,我可以在那里看到我的模板,当我手动触发它时,电子邮件就会到达目标电子邮件帐户。现在我遇到的问题是,我在我的类中添加了一个动作,该动作应该检测订单状态更改,并在订单被设置为我的自定义状态时执行我的触发器功能: add_action('woocommerce_order_status_w

  • 我已成功更改了Woocommerce处理订单的电子邮件主题(使用此线程): 但我想发送处理订单电子邮件与新的主题后,订单状态改变,所以我遵循这一步调整主题等。 但只接受第一次电子邮件主题更改。有办法让它一起工作吗? 是否有权使用?

  • 我正试图阻止woo-commerce发送邮件时,订单状态更改。这些订单是亚马逊的,我的插件从亚马逊同步到Woo-Commerce。在这样做的时候,亚马逊和woo-commerce的邮件都收到了,这激怒了客户。因此,我想停止电子邮件功能停止时,状态从我的插件更改。要更改状态的代码是 有没有可以设置的标志来避免发送邮件? 任何种类的帮助都是非常感谢的。

  • 我在这个相关的答案中找到了对我的问题的部分和可行的答案: send-an-email-notification-when-order-status-changhe-fron-pendig-to-cancelled 我正在考虑使用提供的解决方案,但想看看我是否可以更改电子邮件通知,明确地说,“未决付款订单现在取消”,所以它不同于常规取消的订单。 我该怎么做?