【发布时间】:2022-01-09 16:42:21
【问题描述】:
我要求在 WooCommerce 中购买的产品的类别和子类别在 WooCommerce 订单页面上作为数据属性单独回显。以下 $terms 会返回产品购买的类别和子类别列表。
有没有办法分别列出通过 WooCommerce 购买的产品的主类别和子类别
// Display order items product categories and its id
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' ) );
$term_ids = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'ids' ) );
echo "<div data-category=" . implode(', ', $terms) . "></div>";
echo "<div data-catid=" . implode(', ', $term_ids) . "></div>";
}
【问题讨论】:
标签: wordpress woocommerce hook-woocommerce