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

时间:2018-05-28 13:51:16

标签: php wordpress woocommerce custom-taxonomy email-notifications

我试图展示一些"条件" "订单完成"上的自定义文字电子邮件,在订单标记为已完成时发送给客户。我使用过the following code

现在,我想让这段代码成为有条件的,所以这段代码只应该对于#34; Gift"分类产品。我无法找到适合此特定功能的Woocommerce功能。

任何帮助将不胜感激。

这就是我现在所拥有的:

new_lines = ['one \n', 'two \n']

with open('test.txt', 'r+') as f:
    pos, text = 0, ''
    while True:
        # save last line value and cursor position
        prev_pos, pos = pos, f.tell()
        prev_text, text = text, f.readline()  
        if text == '':
            break

    f.seek(prev_pos, 0) # replace cursor to the last line

    for line in new_lines: # write new lines
        f.write(line)

    f.write(prev_text) # write old last line

1 个答案:

答案 0 :(得分:3)

尝试以下方法:

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文件中。经过测试和工作。