gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:16:54 25 4
gpt4 key购买 nike

在 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>";
}
}

代码进入事件子主题(或事件主题)的 function.php 文件。测试和工作。

关于php - 在 Woocommerce 订单详细信息中显示产品类别名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49481924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com