【问题标题】:WooCommerce add custom email content based on payment method and shipping methodWooCommerce 根据付款方式和运输方式添加自定义电子邮件内容
【发布时间】:2017-12-06 17:20:40
【问题描述】:

我正在尝试根据付款方式和运输方式的组合向 woocommerce 完成的订单电子邮件通知添加不同的内容。

到目前为止我的代码:

// completed order email instructions

function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    if (( get_post_meta($order->id, '_payment_method', true) == 'cod' ) && ( get_post_meta($order->id, '_shipping_method', true) == 'local pickup' )){
    echo "something1";
} 
    elseif (( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) && ( get_post_meta($order->id, '_shipping_method', true) == 'local pickup' )){
    echo "something2";
 }
    else {
    echo "something3";
 }} 

付款部分有效(我得到了正确的“something1”到“something3”内容)但如果我添加&&运输条件,我会在每种付款方式中得到“something3”。

知道什么是错的,我怎样才能让它工作?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    有很多小事情需要改变(例如后元支付方式是一个数组):

    // (Added this missing hook in your code)
    add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
    function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    
        // Only for "Customer Completed Order" email notification
        if( 'customer_completed_order' != $email->id ) return;
    
        // Comptibility With WC 3.0+
        if ( method_exists( $order, 'get_id' ) ) {
            $order_id = $order->get_id();
        } else {
            $order_id = $order->id;
        }
        //$order->has_shipping_method('')
        $payment_method = get_post_meta($order_id, '_payment_method', true);
        $shipping_method_arr = get_post_meta($order_id, '_shipping_method', false); // an array
        $method_id = explode( ':', $shipping_method_arr[0][0] );
        $method_id = $method_id[0];  // We get the slug type method
    
    
        if ( 'cod' == $payment_method && 'local_pickup' == $method_id ){
            echo "something1";
        } elseif ( 'bacs' == $payment_method && 'local_pickup' == $method_id ){
            echo "something2";
        } else {
            echo "something3";
        }
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码经过测试,可与 WooCommerce 版本 2.6.x 和 3+ 一起使用

    【讨论】:

    • 感谢并跟进问题:我将代码用作自定义插件的一部分,因为我必须为几乎每种类型的客户电子邮件添加不同的内容。我列出了插件中的功能,并直接在我的子主题中的相应电子邮件模板中调用它们(无论如何我都必须修改模板)。我将您代码的相关部分粘贴到插件中,它可以工作。其他功能仅基于付款方式,我上面的代码似乎工作至今。我的问题是:让它们保持原样是否安全,或者是否存在一些“隐藏的陷阱”,我应该更好地根据您的代码修改它们?
    • @Anna 如果您使用的是 WooCommerce 3.+,您应该使用 $order->get_id() 而不是 get_post_meta() ...如果您的代码的某些部分有效,请按原样使用它们...
    猜你喜欢
    • 2019-02-07
    • 1970-01-01
    • 2014-08-06
    • 2018-02-15
    • 2018-10-28
    • 1970-01-01
    • 2021-06-07
    • 2018-06-26
    • 2016-11-17
    相关资源
    最近更新 更多