Woocommerce - 如何根据付款类型发送自定义电子邮件

Ada*_*dam 6 php wordpress woocommerce payment-method hook-woocommerce

这是问题所在.我的woocommerce网站有3种不同的付款方式 -

  • 支票付款
  • 西联汇款
  • 货到付款

如果我的买家以"支票付款"结帐,我想向他发送一封自动发送的电子邮件,其中概述了支票付款的步骤.如果他与"西联汇款"签出,我想通过电子邮件将他的西联汇款信息发送给他.应发送另一封自动电子邮件以进行货到付款.

通常在Woocommerce中,您有一封电子邮件发送给客户完成所有已完成的订单,在我的情况下,根据付款选项,我需要3封不同的电子邮件.

所以我开始使用本教程制作自定义电子邮件 - https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/

上面的教程用于制作自定义电子邮件以加快运输.这是教程中使用的代码行 -

// bail if shipping method is not expedited
if ( ! in_array( $this->object->get_shipping_method(), array( 'Three Day Shipping', 'Next Day Shipping' ) ) )
    return;
Run Code Online (Sandbox Code Playgroud)

如果我想检查付款方式是什么,那么代码行是什么?我想检查付款方式是否为"支票付款",以便我可以向他发送自定义电子邮件.

如果您有任何想法,请告诉我.

Loi*_*tec 7

您可以使用thank_you hook通过此自定义函数为每种付款方式发送不同的自定义电子邮件.您可以设置许多选项,为此参考wp_mail()函数代码参考.

这是代码:

add_action( 'woocommerce_thankyou', 'wc_cheque_payment_method_email_notification', 10, 1 );
function wc_cheque_payment_method_email_notification( $order_id ) {
    if ( ! $order_id ) return;

    $order = wc_get_order( $order_id );

    $user_complete_name_and_email = $order->billing_first_name . ' ' . $order->billing_last_name . ' <' . $order->billing_email . '>';
    $to = $user_complete_name_and_email;

    // ==> Complete here with the Shop name and email <==
    $headers = 'From: Shop Name <name@email.com>' . "\r\n";

    // Sending a custom email when 'cheque' is the payment method.
    if ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) {
        $subject = 'your subject';
        $message = 'your message goes in here';
    }
    // Sending a custom email when 'Cash on delivery' is the payment method.
    elseif ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) {
        $subject = 'your subject';
        $message = 'your message goes in here';
    }
    // Sending a custom email when 'Western Union' is the payment method.
    else {
        $subject = 'your subject';
        $message = 'your message goes in here';
    }
    if( $subject & $message) {
        wp_mail($to, $subject, $message, $headers );
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码位于活动子主题(或主题)的functions.php文件中,或者也存储在任何插件文件中.

这是经过测试的,并且有效.


- 更新 -与您的评论相关.

获取可用的付款方式slug (临时,只是为了获得所有slug).这将在商店页面或产品页面中显示您可用的付款方式.使用后,只需将其删除即可.

这是功能代码:

function the_available_payment_gateways(){
    foreach(WC()->payment_gateways->get_available_payment_gateways() as $payment_gateway)
        echo '<div style="border:solid 1px #999">Method Title: "'.$payment_gateway->title .'" / Method slug: "'.$payment_gateway->id .'"</div>';
}
add_action( 'woocommerce_before_main_content', 'the_available_payment_gateways', 1 );
Run Code Online (Sandbox Code Playgroud)

此代码位于活动子主题(或主题)的function.php文件中.使用后将其取下.