编程语言
首页 > 编程语言> > php – Woocommerce – 如何根据付款方式发送自定义电子邮件

php – 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;

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

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

解决方法:

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

这是代码:

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

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

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

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

Getting your available payment methods slugs (temporary, just to get all slugs). This will display your available payment methods slugs on shop page or in product pages too. After usage, just remove it.

Here is that functional code:

06001

This code goes in function.php file of your active child theme (or theme). Remove it after usage.

标签:php,wordpress,woocommerce,hook-woocommerce,payment-method
来源: https://codeday.me/bug/20191006/1862748.html