g3a*_*3ar 2 php wordpress orders custom-taxonomy woocommerce
在 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>";
}
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。
| 归档时间: |
|
| 查看次数: |
2976 次 |
| 最近记录: |