自定义WooCommerce admin-new-order.php模板以在POS打印机中打印

时间:2017-11-08 12:44:17

标签: php wordpress templates woocommerce email-notifications

我正在使用Wordpress和woocommerce建立一个商店,我需要重写admin-new-order.php文件,以便在POS收据打印机中打印订单。实际上我需要的是发送的电子邮件格式为左对齐,特别是产品附加内容列在不同的行中,以便不打印打印的订单。

图像风箱显示纯文本新订单电子邮件:

email new order plain text

我正在使用Extra Variation Plugin能够为产品添加额外内容,但我认为它不会影响这个问题的答案,因为woocommerce email-order-items.php允许其他插件添加其他产品信息

我尝试使用admin-new-order.php中的纯文本模板,通过编辑admin-new-order.phpemail-order-details.php来解决所有问题,但我遇到了email-order-items.php因为我不能强迫它列出不同行中的额外内容。

使用HTML电子邮件,我当然可以格式化此表格,但这会影响所有其他电子邮件模板。出于这个原因,我尝试创建一个admin-new-order.php HTML模板,其中包含所涉及的所有三个模板的代码。如下所示,我得到一个“为foreach()提供的无效参数”错误,无法列出产品。

new order email HTML

以下是我对admin-new-order.php模板的编码方式:

<?php
/**
 * Admin new order email hacked by me
 */

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



//Output the email header
  do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

 <p><?php printf( __( 'You have received an order from %s. The order is as follows:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); ?></p>

 <?php

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

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

<?php if ( ! $sent_to_admin ) : ?>
    <h2><?php printf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ); ?> (<?php printf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ); ?>)</h2>
<?php else : ?>
    <h2><a class="link" href="<?php echo esc_url( admin_url( 'post.php?post=' . $order->get_id() . '&action=edit' ) ); ?>"><?php printf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ); ?></a> (<?php printf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ); ?>)</h2>
<?php endif; ?>

<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 $text_align; ?>;"><?php _e( 'Product', 'woocommerce' ); ?></th>
                <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
                <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Price', 'woocommerce' ); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php/** echo wc_get_email_order_items( $order, array(
                'show_sku'      => $sent_to_admin,
                'show_image'    => false,
                'image_size'    => array( 32, 32 ),
                'plain_text'    => $plain_text,
                'sent_to_admin' => $sent_to_admin,
            ) ); */?>

            <?php
            //aqui vai entrar o ciclo que lista em cada linha da tabela cada um dos produtos. ISTO ESTÁ A DAR ERRO E NÃO É MOSTRADO. Experiência radical com este do_action aqui em baixo. Radicalmente inventado :D

            do_action( 'woocommerce_email_order_items', $order, $sent_to_admin, $plain_text, $email );


            foreach ( $items as $item_id => $item ) :
                $product = $item->get_product();
                if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
                    ?>
                    <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
                        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php


                            // Product name
                            echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) . "\n";
                            echo ' X ' . apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item );
                            echo ' = ' . $order->get_formatted_line_subtotal( $item ) . "\n";

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

                            // 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 $text_align; ?>; vertical-align:middle; border: 0px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">nada</td>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">nada</td>
                    </tr>
                    <?php
                }

                if ( $show_purchase_note && is_object( $product ) && ( $purchase_note = $product->get_purchase_note() ) ) : ?>
                    <tr>
                        <td colspan="3" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
                    </tr>
                <?php endif; ?>

            <?php endforeach; ?>
            ?>
        </tbody>
        <tfoot>
            <?php
                if ( $totals = $order->get_order_item_totals() ) {
                    $i = 0;
                    foreach ( $totals as $total ) {
                        $i++;
                        ?><tr>
                            <th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['label']; ?></th>
                            <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['value']; ?></td>
                        </tr><?php
                    }
                }
                if ( $order->get_customer_note() ) {
                    ?><tr>
                        <th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Note:', 'woocommerce' ); ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>;"><?php echo 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 ); 

// fim de order details?>
<?php
// linhas 40 até ao fim do ficheiro original
/**
  * @hooked WC_Emails::order_meta() Shows order meta data.
  */
 do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

 /**
  * @hooked WC_Emails::customer_details() Shows customer details
  * @hooked WC_Emails::email_address() Shows email address
  */
 do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

 /**
  * @hooked WC_Emails::email_footer() Output the email footer
  */
 do_action( 'woocommerce_email_footer', $email );

如何扭转此问题?

商店下周一上线,我需要弄明白这一点。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在foreach循环中有一些错误,例如未定义的$items,已被$order->get_items()和其他一些小东西取代。这次它应该有效。

以下是自定义模板的工作和测试代码:

<?php
/**
 * Admin new order email hacked by me
 */

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



//Output the email header
  do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

 <p><?php printf( __( 'You have received an order from %s. The order is as follows:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); ?></p>

 <?php

$show_purchase_note = true; // ADDED

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

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

<?php if ( ! $sent_to_admin ) : ?>
    <h2><?php printf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ); ?> (<?php printf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ); ?>)</h2>
<?php else : ?>
    <h2><a class="link" href="<?php echo esc_url( admin_url( 'post.php?post=' . $order->get_id() . '&action=edit' ) ); ?>"><?php printf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ); ?></a> (<?php printf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ); ?>)</h2>
<?php endif; ?>

<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 $text_align; ?>;"><?php _e( 'Product', 'woocommerce' ); ?></th>
                <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
                <th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Price', 'woocommerce' ); ?></th>
            </tr>
        </thead>
        <tbody>

            <?php
            /*
                echo wc_get_email_order_items( $order, array(
                'show_sku'      => $sent_to_admin,
                'show_image'    => false,
                'image_size'    => array( 32, 32 ),
                'plain_text'    => $plain_text,
                'sent_to_admin' => $sent_to_admin,
            ) );
            */

            do_action( 'woocommerce_email_order_items', $order, $sent_to_admin, $plain_text, $email );


            foreach ( $order->get_items() as $item_id => $item ) : // CHANGED from "$items" to "$order->get_items()"
                $product = $item->get_product();
                if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
                    ?>
                    <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
                        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php


                            // Product name
                            echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) . "\n";
                            echo ' X ' . apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item );
                            echo ' = ' . $order->get_formatted_line_subtotal( $item ) . "\n";

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

                            // 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 $text_align; ?>; vertical-align:middle; border: 0px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">nada</td>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">nada</td>
                    </tr>
                    <?php
                }

                if ( $show_purchase_note && is_object( $product ) && ( $purchase_note = $product->get_purchase_note() ) ) : ?>
                    <tr>
                        <td colspan="3" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
                    </tr>
                <?php // CHANGES Below (removed a closing php tag)
                    endif;
                endforeach;
            ?>
        </tbody>
        <tfoot>
            <?php
                if ( $totals = $order->get_order_item_totals() ) {
                    $i = 0;
                    foreach ( $totals as $total ) {
                        $i++;
                        ?><tr>
                            <th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['label']; ?></th>
                            <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['value']; ?></td>
                        </tr><?php
                    }
                }
                if ( $order->get_customer_note() ) {
                    ?><tr>
                        <th class="td" scope="row" colspan="2" style="text-align:<?php echo $text_align; ?>;"><?php _e( 'Note:', 'woocommerce' ); ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>;"><?php echo 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 ); 

// fim de order details?>
<?php
// linhas 40 até ao fim do ficheiro original
/**
  * @hooked WC_Emails::order_meta() Shows order meta data.
  */
 do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

 /**
  * @hooked WC_Emails::customer_details() Shows customer details
  * @hooked WC_Emails::email_address() Shows email address
  */
 do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

 /**
  * @hooked WC_Emails::email_footer() Output the email footer
  */
 do_action( 'woocommerce_email_footer', $email );