首页 文章

仅为WooCommerce管理员电子邮件通知自定义订单商品元

提问于
浏览
2

我需要添加自定义分类以管理新订单电子邮件,但不能添加到客户电子邮件 . 我当前的代码显示了订单中每个项目的自定义分类,但它显示在管理员和客户电子邮件中,这是我不想要的 .

通过 email-order-items.php 我看不到在我正在使用的钩子中使用 $sent_to_admin 的方法 . 我错过了什么吗?

如何仅使用挂钩和过滤器将自定义分类仅添加到管理员电子邮件中?

add_action( 'woocommerce_order_item_meta_end', 'custom_woocommerce_order_item_meta_end', 10, 3 );

function custom_woocommerce_order_item_meta_end( $item_id, $item, $order ) {
     $product = $item->get_product();

     $locations = get_the_terms( $product->get_id(), 'my_custom_taxonomy' );
     echo '
'; echo '<div style="margin-top: 20px;">'; foreach( $locations as $location ) { echo 'Location: <b>' . $location->name . '</b>'; echo '
'; } echo '</div> }

1 回答

  • 1

    这可以使用 $GLOBAL 变量完成 . 我也重新审视了一下你的代码 . 试试这个:

    // Setting the "sent_to_admin" as a global variable
    add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);
    function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
        $GLOBALS['email_data'] = array(
            'sent_to_admin' => $sent_to_admin, // <== HERE we set "$sent_to_admin" value
            'email_id' => $email->id, // The email ID (to target specific email notification)
        );
    }
    
    // Conditionally customizing footer email text
    add_action( 'woocommerce_order_item_meta_end', 'custom_email_order_item_meta_end', 10, 3 );
    function custom_email_order_item_meta_end( $item_id, $item, $order ){
    
        // Getting the custom 'email_data' global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_data = $refNameGlobalsVar['email_data'];
    
        // Only for admin email notifications
        if( ! ( is_array( $email_data ) && $email_data['sent_to_admin'] ) ) return;
    
        ## -------------------------- Your Code below -------------------------- ##
    
        $taxonomy = 'my_custom_taxonomy'; // <= Your custom taxonomy
    
        echo '
    <div style="margin-top: 20px;">'; foreach( get_the_terms( $item->get_product_id(), $taxonomy ) as $term ) echo 'Location: <b>' . $term->name . '</b>
    '; echo '</div>'; }

    代码位于活动子主题(或活动主题)的function.php文件中 .

    经过测试和工作 .

相关问题