php - WooCommerce 添加基于付款方式和送货方式的自定义电子邮件内容

标签 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";
 }} 

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

知道哪里出了问题吗?我该如何让它发挥作用?

谢谢

最佳答案

有很多小东西需要改变(例如后元支付方法是一个数组):

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

关于php - WooCommerce 添加基于付款方式和送货方式的自定义电子邮件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44885575/

相关文章:

Javascript - 在滚动上添加类不起作用 WordPress

excel - 使用自定义元动态定价规则导入 WooCommerce 产品

php - Woocommerce WP - 从 post_type = shop_order | 获取 _sku 值SQL查询

php - 在 WooCommerce 电子邮件通知上显示自定义产品图像

php - 如何更新倒数第二行?

php - PHP 中 or 运算符的行为

php - XAMPP 管理器无法在 OS X 上启动服务器,但可以在终端上运行

javascript - 如果使用 Select2 YII2 选择第一个字段,则自动填充下面的字段

php - ACF 的 WordPress 查询帖子

Wordpress 虚拟 robots.txt 覆盖手动创建的 robots.txt