Woocommerce 电子邮件订单详细信息中的其他列和项目值 [英] Additional columns and item values in Woocommerce email order details

查看:30
本文介绍了Woocommerce 电子邮件订单详细信息中的其他列和项目值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑 Woocommerce 电子邮件模板,但有点卡住了 - 我需要在订单汇总表中添加几列以使其包含:

  • 数量
  • 产品
  • 产品代码
  • 价格
  • 增值税

我已将所需的表格标题添加到email-order-details.php"(复制到我的主题的电子邮件文件夹)中,但不确定如何将实际内容放入表格中.文件中的代码如下:

<h2><?php如果($sent_to_admin){$before = '<a class="link" href="' .esc_url( $order->get_edit_order_url() ) .'">';$after = '</a>';} 别的 {$before = '';$after = '';}/* 翻译员:%s:订单 ID.*/echo wp_kses_post( $before . sprintf( __( 'Order #%s', 'woocommerce' ) . $after . ' (<time datetime="%s">%s</time>)', $order->get_order_number(), $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) );?><div style="margin-bottom: 40px;"><table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"边框="1"><头><tr><th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e('Quantity', 'woocommerce');?></th><th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Product', 'woocommerce');?></th><th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( '产品代码', 'woocommerce');?></th><th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e('Price', 'woocommerce');?></th><th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e('增值税', 'woocommerce');?></th></tr></thead><?phpecho wc_get_email_order_items( $order, array(//WPCS: XSS ok.'show_sku' =>$sent_to_admin,'show_image' =>错误的,'图像大小' =>数组( 32, 32 ),'纯文本' =>$plain_text,'sent_to_admin' =>$sent_to_admin,) );?></tbody><脚><?php$totals = $order->get_order_item_totals();如果($总计){$i = 0;foreach ( $totals as $total ) {$i++;?><tr><th class="td" scope="row" colspan="4" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 ===$i ) ? 'border-top-width: 4px;': ''; ?>"><?php echo wp_kses_post( $total['label'] );?></th><td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;': ''; ?>"><?php echo wp_kses_post( $total['value'] );?></td></tr><?php}}如果 ( $order->get_customer_note() ) {?><tr><th class="td" scope="row" colspan="4" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( '附加说明:', 'woocommerce' );?></th><td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo wp_kses_post( wptexturize( $order->get_customer_note()) );?></td></tr><?php}?></tfoot>

<?php do_action('woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text, $email );?>

任何帮助将不胜感激.

解决方案

根据您的问题自定义模板代码,默认情况下有 5 列而不是 3 列:

  1. 数量
  2. 产品
  3. 产品代码
  4. 价格
  5. 增值税

您还需要覆盖另一个模板文件:emails/email-order-items.php 如:

 $item ) :$product = $item->get_product();$sku = '';$purchase_note = '';$图像 = '';如果(!apply_filters('woocommerce_order_item_visible',真,$item)){继续;}如果 ( is_object( $product ) ) {$sku = $product->get_sku();$purchase_note = $product->get_purchase_note();$image = $product->get_image( $image_size );}?><tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>"><td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, 无衬线字体;"><?php echo wp_kses_post( apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ));?></td><td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align: middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php//显示标题/图片等如果($show_image){echo wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $image, $item ) );}//产品名称.echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );//SKU.如果 ( $show_sku && $sku ) {echo wp_kses_post( ' (#' . $sku . ')' );}//允许其他插件在此处添加额外的产品信息.do_action('woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );wc_display_item_meta( $item, 数组('label_before' =>'<strong class="wc-item-meta-label" style="float: left; margin-right: .25em; clear: both">',) );//允许其他插件在此处添加额外的产品信息.do_action('woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );?></td><td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, 无衬线字体;"><?php echo wp_kses_post( $sku );?></td><td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, 无衬线字体;"><?php echo wp_kses_post( wc_price( round( $item->get_subtotal(), wc_get_price_decimals() ) ) );?></td><td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, 无衬线字体;"><?php echo wp_kses_post( wc_price( round( $item->get_subtotal_tax(), wc_get_price_decimals() ) );?></td></tr><?php如果 ( $show_purchase_note && $purchase_note ) {?><tr><td colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, 无衬线字体;"><?phpecho wp_kses_post( wpautop( do_shortcode( $purchase_note ) ) );?></td></tr><?php}?><?php endforeach;?>

然后你会得到类似的东西:

注意:对于产品代码,如果它是一些自定义数据,您将不得不找出如何替换它:

<?php echo wp_kses_post( $sku );?>

通过正确的值.

I am editing a Woocommerce email template but am a little stuck - I need to add a few columns to the order summary table so that it contains:

  • Quantity
  • Product
  • Product code
  • Price
  • VAT

I have added the table headers that I need into 'email-order-details.php' (copied to my theme's email folder) to but not sure how to get the actual content into the table. The code from the file is below:

<?php
/**
 * Order details table shown in emails.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-details.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates/Emails
 * @version 3.3.1
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$text_align = is_rtl() ? 'right' : 'left';

do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>

<h2>
    <?php
    if ( $sent_to_admin ) {
        $before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
        $after  = '</a>';
    } else {
        $before = '';
        $after  = '';
    }
    /* translators: %s: Order ID. */
    echo wp_kses_post( $before . sprintf( __( 'Order #%s', 'woocommerce' ) . $after . ' (<time datetime="%s">%s</time>)', $order->get_order_number(), $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ) );
    ?>
</h2>

<div style="margin-bottom: 40px;">
    <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
        <thead>
            <tr>
                <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></th>
                <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
                <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Product code', 'woocommerce' ); ?></th>
                <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
                <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'VAT', 'woocommerce' ); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            echo wc_get_email_order_items( $order, array( // WPCS: XSS ok.
                'show_sku'      => $sent_to_admin,
                'show_image'    => false,
                'image_size'    => array( 32, 32 ),
                'plain_text'    => $plain_text,
                'sent_to_admin' => $sent_to_admin,
            ) );
            ?>
        </tbody>
        <tfoot>
            <?php
            $totals = $order->get_order_item_totals();

            if ( $totals ) {
                $i = 0;
                foreach ( $totals as $total ) {
                    $i++;
                    ?>
                    <tr>
                        <th class="td" scope="row" colspan="4" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['label'] ); ?></th>
                        <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['value'] ); ?></td>
                    </tr>
                    <?php
                }
            }
            if ( $order->get_customer_note() ) {
                ?>
                <tr>
                    <th class="td" scope="row" colspan="4" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Additional notes:', 'woocommerce' ); ?></th>
                    <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo wp_kses_post( wptexturize( $order->get_customer_note() ) ); ?></td>
                </tr>
                <?php
            }
            ?>
        </tfoot>
    </table>
</div>

<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text, $email ); ?>

Any help would be greatly appreciated.

解决方案

Based on your question customized template code which have 5 columns instead of 3 by default:

  1. Quantity
  2. Product
  3. Product code
  4. Price
  5. VAT

You need to override another template file too: emails/email-order-items.php like:

<?php
/**
 * Email Order Items
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates/Emails
 * @version 3.5.0
 */

defined( 'ABSPATH' ) || exit;

$text_align = is_rtl() ? 'right' : 'left';

foreach ( $items as $item_id => $item ) :
    $product       = $item->get_product();
    $sku           = '';
    $purchase_note = '';
    $image         = '';

    if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
        continue;
    }

    if ( is_object( $product ) ) {
        $sku           = $product->get_sku();
        $purchase_note = $product->get_purchase_note();
        $image         = $product->get_image( $image_size );
    }

    ?>
    <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
        <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
            <?php echo wp_kses_post( apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ) ); ?>
        </td>
        <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align: middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;">
        <?php

        // Show title/image etc.
        if ( $show_image ) {
            echo wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $image, $item ) );
        }

        // Product name.
        echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );

        // SKU.
        if ( $show_sku && $sku ) {
            echo wp_kses_post( ' (#' . $sku . ')' );
        }

        // allow other plugins to add additional product information here.
        do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );

        wc_display_item_meta( $item, array(
            'label_before' => '<strong class="wc-item-meta-label" style="float: left; margin-right: .25em; clear: both">',
        ) );

        // allow other plugins to add additional product information here.
        do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );

        ?>
        </td>
        <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
            <?php echo wp_kses_post( $sku ); ?>
        </td>
        <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
            <?php echo wp_kses_post( wc_price( round( $item->get_subtotal(), wc_get_price_decimals() ) ) ); ?>
        </td>
        <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
            <?php echo wp_kses_post( wc_price( round( $item->get_subtotal_tax(), wc_get_price_decimals() ) ) ); ?>
        </td>
    </tr>
    <?php

    if ( $show_purchase_note && $purchase_note ) {
        ?>
        <tr>
            <td colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
                <?php
                echo wp_kses_post( wpautop( do_shortcode( $purchase_note ) ) );
                ?>
            </td>
        </tr>
        <?php
    }
    ?>

<?php endforeach; ?>

Then you will get something like:

Note: For the product code if it's some custom data, you will have to findout how to get it replacing:

<?php echo wp_kses_post( $sku ); ?>

by the correct value.

这篇关于Woocommerce 电子邮件订单详细信息中的其他列和项目值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆