Woocommerce-如何根据付款类型发送自定义电子邮件 [英] Woocommerce - How to send custom emails based on payment type

查看:153
本文介绍了Woocommerce-如何根据付款类型发送自定义电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is the problem. My woocommerce website has 3 different payment options -

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

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

If my buyer checkout with "Check Payment" I want to send him an automated email that outlines the steps to make a check payment. If he checkout with "Western Union" I want to email him my Western Union information as an automated email. Another automated email should be send for Cash On Delivery.

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

Usually in Woocommerce you have a single email sent to customer for all completed orders, in my case I need 3 different emails depending on the payment option.

因此,我开始使用本教程制作自定义电子邮件-

So I started using this tutorial to make a custom email - https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/

以上教程用于制作自定义电子邮件,以加快运输速度.这是与本教程相同的代码行-

The tutorial above is used to make custom emails for expedited shipping. This is the line of code used for the same from the tutorial -

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

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

What will the line of code be if I want to check what the payment method is? I want to check if the payment method is "Check Payment" so that I can send him a custom email.

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

Please let me know if you have any idea.

推荐答案

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

You can send a different customized email for each payment method with this custom function using thank_you hook. There is many options that you can set, for this refer to 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文件中,也可能出现在任何插件文件中.

这已经过测试,并且可以正常工作.

This is tested and it works.

—更新— 与您的评论有关.

— Update — Related to your comments.

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

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.

这是功能代码:

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

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

这篇关于Woocommerce-如何根据付款类型发送自定义电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆