首页 > 解决方案 > Woocommerce 自定义电子邮件

问题描述

我需要有关 woocommerce 的自定义电子邮件挂钩的帮助。

每当产品完成时,我都会尝试根据产品 ID 发送不同的电子邮件。

我的代码不起作用,如下所示:

/**************
DIFFERENT MESSAGES FOR DIFFERENT PRODUCTS
****************/

//hook our function to the new order email
add_action('woocommerce_email_order_details',     'uiwc_email_order_details_products', 1, 4);

function uiwc_email_order_details_products($order, $admin, $plain, $email) {
 $status = $order->get_status();

 // checking if it's the order status we want
  if ( $status == "completed" ) {
   $items = $order->get_items();

    if ( $item['product_id'] == "3181") {
      echo __( '<strong>IMPORTANT - NEXT STEP:</strong><br>To get started, please follow <a href="https:XXXXX">this link</a> to complete the Policies form.<br><br>This is a really important first step, and only takes about 5 minutes.  After completeing the Policies form, you will receive additional instructions on next steps.<br><br>Congratulations! Let your journey begin.<br><br>', 'uiwc' );
  }

   elseif ( $item['product_id'] == "3223") {
      echo __( '<strong>IMPORTANT - NEXT STEP:</strong><br>Differnet product so differenct email....<br><br>', 'uiwc' );
  }
}  
}

非常感谢任何建议

标签: phpwordpresswoocommerceordersemail-notifications

解决方案


您的代码中存在一些错误,请尝试以下操作

//hook our function to the new order email
add_action( 'woocommerce_email_order_details', 'custom_email_order_details', 4, 4 );
function custom_email_order_details( $order, $admin, $plain, $email ) {
    $domain = 'woocommerce';

    // checking if it's the order status we want
    if ( $order->has_status('completed') ) {
        foreach( $order->get_items() as $item ){
            if ( $item->get_product_id() == '3181' ) {
                echo __( '<strong>IMPORTANT - NEXT STEP:</strong><br>To get started, please follow <a href="https://xxxxxx">this link</a> to complete the Policies form.<br><br>This is a really important first step, and only takes about 5 minutes. After completeing the Policies form, you will receive additional instructions on next steps.<br><br>Congratulations! Let your journey begin.<br><br>', $domain );
                break;
            }
            elseif ( $item->get_product_id() == '3223' ) {
                echo __( '<strong>IMPORTANT - NEXT STEP:</strong><br>Differnet product so differenct email....<br><br>', $domain );
                break;
            }
        } 
    }  
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。


推荐阅读