WooCommerce根据付款方式和送货方式添加自定义电子邮件内容

时间:2017-07-03 12:26:06

标签: php wordpress woocommerce orders email-notifications

我试图根据付款方式和送货方式的组合,为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";
 }} 

支付部分有效(我得到了正确的#34;某事1和#34;对于"某事3和#34;内容)但是如果我添加&&&运输条件,我得到" something3"每种付款方式。

知道什么是错的,我怎么能让它发挥作用?

由于

1 个答案:

答案 0 :(得分:4)

有许多小事要改变(例如,后元付款方式是一个数组):

// (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 +