Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在订单详细信息中显示产品类别名称_Php_Wordpress_Woocommerce_Custom Taxonomy_Orders - Fatal编程技术网

Php 在订单详细信息中显示产品类别名称

Php 在订单详细信息中显示产品类别名称,php,wordpress,woocommerce,custom-taxonomy,orders,Php,Wordpress,Woocommerce,Custom Taxonomy,Orders,在Woocommerce中,我如何在订单详细信息(以及Wordpress后端)中显示产品类别名称。也可以使它们出现在电子邮件通知中。这可以通过以下方式完成: // Display order items product categories (Orders on front end and emails) add_action( 'woocommerce_order_item_meta_end', 'display_custom_data_in_emails', 10, 4 ); functio

在Woocommerce中,我如何在订单详细信息(以及Wordpress后端)中显示产品类别名称。也可以使它们出现在电子邮件通知中。

这可以通过以下方式完成:

// Display order items product categories (Orders on front end and emails)
add_action( 'woocommerce_order_item_meta_end', 'display_custom_data_in_emails', 10, 4 );
function display_custom_data_in_emails( $item_id, $item, $order, $bool ) {
    // Get the product categories for this item
    $terms = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );

    // Output a coma separated string of product category names
    echo "<br><small>" . implode(', ', $terms) . "</small>";
}

// Display order items product categories in admin order edit pages
add_action( 'woocommerce_after_order_itemmeta', 'custom_admin_order_itemmeta', 15, 3 );
function custom_admin_order_itemmeta( $item_id, $item, $product ){
    //if( ! is_admin() ) return; // only backend

    // Target order "line items" only to avoid errors
    if( $item->is_type( 'line_item' ) ){
        // Get the product categories for this item
        $terms = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );

        // Output a coma separated string of product category names
        echo "<br><small>" . implode(', ', $terms) . "</small>";
    }
}
//显示订单项目产品类别(前端订单和电子邮件)
添加操作('woocommerce\u order\u item\u meta\u end','display\u custom\u data\u in\u emails',10,4);
功能显示电子邮件中的自定义数据($item\u id、$item、$order、$bool){
//获取此项目的产品类别
$terms=wp\u get\u post\u terms($item->get\u product\u id(),'product\u cat',数组('fields'=>'names');
//输出产品类别名称的分隔字符串
echo“
”。内爆(“,”,$terms)。”; } //在管理订单编辑页面中显示订单项目产品类别 添加动作('woocommerce'在'u order'u itemmeta'之后,'custom'u admin'u order'u itemmeta',15,3); 函数自定义\管理\订单\项目元($item\u id,$item,$product){ //如果(!is_admin())返回;//仅后端 //目标订单“行项目”仅用于避免错误 如果($item->is_类型('line_item')){ //获取此项目的产品类别 $terms=wp\u get\u post\u terms($item->get\u product\u id(),'product\u cat',数组('fields'=>'names'); //输出产品类别名称的分隔字符串 echo“
”。内爆(“,”,$terms)。”; } }

代码进入活动子主题(或活动主题)的function.php文件。已测试并正常工作。

在我的主题中将其添加到functions.php中,其工作状态非常完美:)再次感谢(life saver)通过用我的自定义分类法替换“product_cat”对其进行了修改——谢谢,这非常有帮助。