将订单客户注释添加到YITH Woocommerce PDF发票

时间:2018-10-20 00:40:57

标签: php wordpress pdf woocommerce invoice

在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变量中获取客户注释。

我尝试使用以下答案线程:“ Display Customer order comments (customer note) in Woocommerce”,看起来很像是同一问题,但由于$document->order->customer_message;不起作用而无法解决。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

自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

应该可以。


  

免费插件版本

  • 没有可用的钩子(如提供的代码中所示)…
  • YITH PDF全局对象需要被调用,它不是$document

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

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