在WooCommerce电子邮件通知中显示订单商品的产品类别

时间:2017-12-21 15:47:23

标签: php wordpress woocommerce categories email-notifications

我正在使用Woocommerce的插件智能优惠券。在尝试自定义通过邮件收到的礼品券时,我无法显示订单中包含的产品类别。

如何在订单的电子邮件中显示产品类别?

1 个答案:

答案 0 :(得分:0)

由于$order WC_Order对象的一个​​实例)包含在大多数电子邮件模板和摘要中,因此您可以获取该产品的类别。订购。请记住,订单可以包含许多商品,每个商品可以包含多个类别。您将使用以下代码获取这些产品类别:

$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);

此代码经过测试并有效。