如何仅在管理员订单详细信息的电子邮件中显示产品类别

时间:2020-04-30 08:45:03

标签: php wordpress woocommerce hook-woocommerce

我想在新订单电子邮件中显示产品类别

  • 将其发送给管理员。
  • 客户无需查看此内容,也无需在结帐页面上显示该内容。

我已将以下内容添加到email-order-details.php

最初我以为它可以正常工作,但是后端却出现了一个重新声明的错误。如果我在functions.php中使用相同的代码,则会收到所有类别的电子邮件。

function woocommerce_before_order_add_cat( $name, $item ){

    $product_id = $item['product_id'];
    $tax = 'product_cat';

    $terms = wp_get_post_terms( $product_id, $tax, array( 'fields' => 'names' ) );

    if( $terms && ! is_wp_error( $terms )) {
        $taxonomy = get_taxonomy($tax);
        $name .= '<label>' . $taxonomy->label . ': </label>' . implode( ', ', $terms );
    }

    return $name;
}
add_filter( 'woocommerce_order_item_name', 'woocommerce_before_order_add_cat', 10, 2 );

1 个答案:

答案 0 :(得分:0)

要确定only for WooCommerce admin email notification信用,请执行以下操作:@Loictheaztec

// Setting the "sent_to_admin" as a global variable
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)
    );
}
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);

function woocommerce_before_order_add_cat( $name, $item ) {
    // On email notifications
    if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {

        // 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'] ) {

            // Get product id
            $product_id = $item['product_id'];

            // Tax
            $tax = 'product_cat';

            // Get post terms
            $terms = wp_get_post_terms( $product_id, $tax, ['fields' => 'names'] );

            if( $terms && ! is_wp_error( $terms )) {
                $taxonomy = get_taxonomy($tax);
                $name .= '<label>' . $taxonomy->label . ': </label>' . implode( ', ', $terms );
            }
        }
    }

    return $name;
}
add_filter( 'woocommerce_order_item_name', 'woocommerce_before_order_add_cat', 10, 2 );

您也可以使用

  • woocommerce_order_item_meta_start
  • 或者如果需要,您也可以使用woocommerce_order_item_meta_end

代替woocommerce_order_item_name

function add_product_categories_to_mail( $item_id, $item, $order, $plain_text ) {
    // On email notifications
    if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {

        // 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'] ) {

            // Get product id
            $product_id = $item['product_id'];

            // Tax
            $tax = 'product_cat';

            // Get post terms
            $terms = wp_get_post_terms( $product_id, $tax, ['fields' => 'names'] );

            // Set variable
            $name = '';

            if( $terms && ! is_wp_error( $terms )) {
                $taxonomy = get_taxonomy($tax);
                $name = '<label>' . $taxonomy->label . ': </label>' . implode( ', ', $terms );
            }

            echo $name;
        }
    }
}
add_action( 'woocommerce_order_item_meta_start', 'add_product_categories_to_mail', 10, 4 );
//add_action( 'woocommerce_order_item_meta_end', 'add_product_categories_to_mail', 10, 4 );