如何使用“稍后付款”(待处理状态)网关发送“客户发票”电子邮件

时间:2018-01-13 18:31:00

标签: woocommerce email-notifications

我创建了一个新的支付网关插件,允许客户稍后付款。我将使用它与woocommerce POS系统。

我无法将“客户发票”电子邮件自动发送 - 我认为这与订单的“待处理”状态有关(为了让客户以后付款,它必须具有此状态)。我已经分析了许多其他答案,但没有一个对我有用。

我已经轻轻修改了此链接中的代码(仅更改名称并设置为插件):https://github.com/creativelittledots/woocommerce-pay-later

它使用一系列功能来触发我不完全理解的电子邮件 - 但它似乎不起作用:默认状态设置为“暂停”以允许电子邮件触发然后命令设置为“待定”以允许客户支付订单。

以下是相关摘录:

    add_filter( 'woocommerce_default_order_status', array($this, 'default_order_status') );
    add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
    add_filter( 'woocommerce_email_format_string_find', array($this, 'order_status_format_string_find') );
    add_filter( 'woocommerce_email_format_string_replace', array($this, 'order_status_format_string_replace'), 10, 2 );
    add_action( 'woocommerce_order_status_pending', array($this, 'send_pending_order_emails') );
    add_filter( 'woocommerce_valid_order_statuses_for_payment', array($this, 'valid_order_statuses_for_payment' ), 10, 2 );
    add_action( 'wp', array($this, 'change_order_to_pending_on_order_received'), 8 );`

public function valid_order_statuses_for_payment($statuses, $order) {

    if( $order->is_pay_later ) {

        $statuses[] = 'on-hold';

    }

    return $statuses;

}

/**
 * Change the default order status to on-hold so that pending order emails can be triggered
 */
public function default_order_status($default) {

    if( ! is_admin() && WC()->session->set( 'chosen_payment_method') == $this->id ) {

        $default = 'on-hold';

    }

    return $default;

}

/**
 * Allow Order status to be accessible from emails
 */
public function order_status_format_string_find( $find ) {

    $find['order-status'] = '{order_status}';

    return $find;

}

/**
 * Replace Order status in emails
 */
public function order_status_format_string_replace( $replace, $email ) {

    if( $email->object ) {

        $replace['order-status'] = wc_get_order_status_name( $email->object->get_status() );

    }

    return $replace;

}

/**
 * Trigger pending order emails and invoice email
 */
public function send_pending_order_emails( $order_id ) {

    $emails = new WC_Emails();

    $order = wc_get_order( $order_id );

    $emails->customer_invoice( $order_id );

    $emails->emails['WC_Email_New_Order']->trigger( $order_id );

    $order->set_payment_method( $this );

}

/**
 * WC Shop As Customer support on Order Received, because the default status is on hold we need to change these orders to pending
 */
public function change_order_to_pending_on_order_received() {

    if( class_exists('WC_Shop_As_Customer') && ! empty( $_GET['order_on_behalf'] ) && ! empty( $_GET['key'] ) && ! empty( $_GET['send_invoice'] ) ) {

        global $wp;

        if ( ! isset( $wp->query_vars['order-received'] ) )
            return;

        // Bail if we're not shopping-as - don't display the special interface.
        if ( ! WC_Shop_As_Customer::get_original_user() )
            return;

        $order_id = $wp->query_vars['order-received'];
        if ( ! empty( $order_id ) ) {

            $order = new WC_Order( absint( $order_id) );

            $order->update_status( 'pending' );

        }

        unset( $_GET['send_invoice'] );

    }

}

我还读到woocommerce_order_status_pending可能不会触发电子邮件,所以我尝试添加此代码(没有成功):

add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
    $actions[] = 'woocommerce_order_status_pending';
    return $actions;
}

我怀疑:

  1. 将订单“暂停”然后触发然后返回“待定”的代码未正确设置
  2. woocommerce_order_status_pending hook不会触发电子邮件
  3. 我有一点工作来触发functions.php中的“woocommerce_thankyou”钩子,但我相信这应该在网关中完成(加上我不想依赖woocommerce_thankyou - 这封电子邮件应该基于订单创建)。

    提前致谢!!!!!!

0 个答案:

没有答案