在 WooCommerce 电子邮件中显示基于产品类别的自定义文本

Display a custom text based on product category in WooCommerce Email

我正在尝试在 "Order Complete" 电子邮件中显示一些 "conditional" 自定义文本,当订单标记为已完成时发送给客户。我用过the following code

现在,我想让这段代码有条件,所以这段代码应该只对 "Gift" 类别产品有效。我找不到任何适合此特定功能的 Woocommerce 功能。

如有任何帮助,我们将不胜感激。

这是我目前拥有的:

add_action( 'woocommerce_email_before_order_table', 
'bbloomer_add_content_specific_email', 20, 4 );

function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'customer_completed_order' ) {
        echo '<p class="email-text-conditional">Thank you for your order with Adventure Clues, we have sent your recipient the gift card’</p>';
    }
}

尝试以下操作:

add_action( 'woocommerce_email_before_order_table', 'email_before_order_table_conditional_display', 20, 4 );
function email_before_order_table_conditional_display( $order, $sent_to_admin, $plain_text, $email ) {
    // Only for "commpeted" order status notification
    if ( 'customer_completed_order' !== $email->id ) return;

    foreach( $order->get_items() as $item ){
        if( has_term( "Gifts", "product_cat", $item->get_product_id() ) ){
            echo '<p class="email-text-conditional">Thank you for your order with Adventure Clues, we have sent your recipient the gift card.</p>';
            break;
        }
        if( has_term( "Clothes", "product_cat", $item->get_product_id() ) ){
            echo '<p class="email-text-conditional">Thank you for your order with Adventure Clues, we are managing your Clothes.</p>';
            break;
        }
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。