首页 > 解决方案 > 将订单客户备注添加到 YITH Woocommerce PDF 发票

问题描述

在 Woocommerce 中,我使用了一个名为YITH WooCommerce PDF Invoice and Shipping List的插件,我想在 PDF 发票中添加客户备注。

我想在下面代码的第一行之后添加它:

        <span class="notes-title"><?php _e( "Notes", "yith-woocommerce-pdf-invoice" ); ?></span>    
        <div class="notes">
            <span><?php echo nl2br( $notes ); ?></span>
            <?php do_action( 'yith_ywpi_after_document_notes', $document );?>
        </div>
    </div>
    <?php

但我不知道如何从$document变量中获取客户注释。

我曾尝试使用此答案线程:“在 Woocommerce 中显示客户订单评论(客户备注) ”,这看起来很像同样的问题,但仍然无法弄清楚它$document->order->customer_message;不起作用。

任何帮助表示赞赏。

标签: phpwordpresspdfwoocommerceinvoice

解决方案


自 Woocommerce 3 以来,您无法再从WC_Order对象访问属性。您需要使用WC_Order方法 [ get_customer_note()][1]。

因此,$document您将使用(YITH 全局对象):

$document->order->get_customer_note();

要将客户备注添加到 YITH 发票,您可以选择以下两种方式:

1)使用可用的yith_ywpi_after_document_notes 动作钩子

add_action( 'yith_ywpi_invoice_template_products_list', 'add_customer_notes_after_document_notes', 5 );
function add_customer_notes_after_document_notes( $document ) {
    ?><span><?php echo $document->order->get_customer_note(); ?></span><?php
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。未经测试(因为我没有插件的高级版本),但它应该可以正常工作(取决于插件设置)

2)覆盖模板(在您提供的代码中):

    <span class="notes-title"><?php _e( "Notes", "yith-woocommerce-pdf-invoice" ); ?></span>    
    <div class="notes">
        <span><?php echo nl2br( $notes ); ?></span>
        <span><?php echo $document->order->get_customer_note(); ?></span>
        <?php do_action( 'yith_ywpi_after_document_notes', $document );?>
    </div>
</div>
<?php

它应该工作。


对于免费插件版本

  • 没有可用的钩子(就像在提供的代码中一样)......</li>
  • 需要调用YITH PDF 全局对象,而不是$document.

因此,您将能够在templates/invoice/invoice-footer.php模板中使用以下代码:

 <?php global $ywpi_document; echo $ywpi_document->order->get_customer_note(); ?>

推荐阅读