【问题标题】:Display the product category of a an order item in the WooCommerce email notifications在 WooCommerce 电子邮件通知中显示订单项目的产品类别
【发布时间】:2018-06-04 07:56:53
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress woocommerce categories email-notifications


    【解决方案1】:

    由于$orderWC_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);
    

    此代码已经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2019-08-27
      • 2017-09-20
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 2019-04-09
      • 2018-04-10
      • 2020-10-03
      相关资源
      最近更新 更多