我正在使用 Woocommerce 插件智能优惠券。在尝试自定义通过邮件收到的礼券时,我无法显示订单中包含的产品类别。
如何在订单邮件中显示产品的类别?
最佳答案
由于 $order
(WC_Order
对象的实例) 包含在大多数电子邮件模板和 Hook 中,因此您可以获得订单中包含的产品类别。请记住,一个订单可以包含许多商品,并且每个商品可以有多个类别。您将使用以下代码获取这些产品类别:
$product_categories = array();
// Loop through order items
foreach( $order->get_items() as $items ){
// Get an array of the WP_Terms of the product categories
$terms = wp_get_post_terms( $items->get_product_id(), 'product_cat' );
// Loop through the product categories WP_Term objects
foreach( $terms as $wp_term ){
// Get the product category ID
$term_id = $wp_term->term_id;
// Get the product category Nmae
$term_name = $wp_term->name;
// Get the product category Slug
$term_slug = $wp_term->slug;
// Get the product category Parent ID
$term_parent_id = $wp_term->parent;
// Set each product category WP_Term object in an array (avoiding duplicates)
$product_categories[$wp_term->term_id] = $wp_term;
}
}
// Output the raw data of all product categories in the order (Testing)
var_dump($product_categories);
此代码经过测试并且可以工作。
关于php - 在 WooCommerce 电子邮件通知中显示订单商品的产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928354/