douhao2026 2015-01-15 10:21
浏览 173

在woocommerce新订单电子邮件中包含产品类别

I run an e-commerce store set-up using Wordpress and WooCommerce and am having a little difficulty including product category in my new order email. Basically when a customer makes a purchase, I receive an email with details of the new order however the product category is not included in the email.

Here is the code in my email-order-items.php file

<?php

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

foreach ( $items as $item_id => $item ) :
$_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
$item_meta    = new WC_Order_Item_Meta( $item['item_meta'], $_product );
?>
<tr>
    <td style="text-align:left; vertical-align:middle; border: 1px solid #eee; word-wrap:break-word;"><?php


        // Show title/image etc
        if ( $show_image ) {
            echo apply_filters( 'woocommerce_order_item_thumbnail', '<img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . __( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" />', $item );
        }

        // Product name
        echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item );


        // SKU
        if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
            echo ' (#' . $_product->get_sku() . ')';
        }

        // File URLs
        if ( $show_download_links && is_object( $_product ) && $_product->exists() && $_product->is_downloadable() ) {

            $download_files = $order->get_item_downloads( $item );
            $i              = 0;

            foreach ( $download_files as $download_id => $file ) {
                $i++;

                if ( count( $download_files ) > 1 ) {
                    $prefix = sprintf( __( 'Download %d', 'woocommerce' ), $i );
                } elseif ( $i == 1 ) {
                    $prefix = __( 'Download', 'woocommerce' );
                }

                echo '<br/><small>' . $prefix . ': <a href="' . esc_url( $file['download_url'] ) . '" target="_blank">' . esc_html( $file['name'] ) . '</a></small>';
            }
        }

        // Variation
        if ( $item_meta->meta ) {
            echo '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>';
        }

    ?></td>
    <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?></td>
    <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>

<?php if ( $show_purchase_note && is_object( $_product ) && $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) : ?>
    <tr>
        <td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
    </tr>
<?php endif; ?>

Any help would be much appreciated :)

  • 写回答

2条回答 默认 最新

  • dqd3690 2015-01-15 10:57
    关注

    Try this code:

    <?php
        /**
         * Email Order Items
         *
         * @author      WooThemes
         * @package     WooCommerce/Templates/Emails
         * @version     2.1.2
         */
    
        if ( ! defined( 'ABSPATH' ) ) {
            exit; // Exit if accessed directly
        }
    
        foreach ( $items as $item_id => $item ) :
            $_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
            $item_meta    = new WC_Order_Item_Meta( $item['item_meta'], $_product );
            $terms = get_the_term_list( $_product->id, 'product_cat' );
            ?>
            <tr>
                <td style="text-align:left; vertical-align:middle; border: 1px solid #eee; word-wrap:break-word;"><?php
                    echo ''.$terms.': ';
                    // Show title/image etc
                    if ( $show_image ) {
                        echo apply_filters( 'woocommerce_order_item_thumbnail', '<img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . __( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" />', $item );
                    }
    
                    // Product name
                    echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item )."something";
    
                    // SKU
                    if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
                        echo ' (#' . $_product->get_sku() . ')';
                    }
    
                    // File URLs
                    if ( $show_download_links && is_object( $_product ) && $_product->exists() && $_product->is_downloadable() ) {
    
                        $download_files = $order->get_item_downloads( $item );
                        $i              = 0;
    
                        foreach ( $download_files as $download_id => $file ) {
                            $i++;
    
                            if ( count( $download_files ) > 1 ) {
                                $prefix = sprintf( __( 'Download %d', 'woocommerce' ), $i );
                            } elseif ( $i == 1 ) {
                                $prefix = __( 'Download', 'woocommerce' );
                            }
    
                            echo '<br/><small>' . $prefix . ': <a href="' . esc_url( $file['download_url'] ) . '" target="_blank">' . esc_html( $file['name'] ) . '</a></small>';
                        }
                    }
    
                    // Variation
                    if ( $item_meta->meta ) {
                        echo '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>';
                    }
    
                ?></td>
                <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?></td>
                <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
            </tr>
    
            <?php if ( $show_purchase_note && is_object( $_product ) && $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) : ?>
                <tr>
                    <td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
                </tr>
            <?php endif; ?>
    
        <?php endforeach; ?>
    

    NOTE: Untested.

    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来