普通网友 2019-03-19 20:41
浏览 171

根据Woocommerce订单中的付款方式显示自定义文本

In Woocommerce I'm trying to display a message based on the payment method that the customer selects when an order is submitted. I have 2 payment methods, BACS and Cheque and I need to display a different message for each one.

I just found that you can put a message on the page of thankyou.php

but I would need this custom message to appear on the order page and also to be added to the pdf invoice (I am using WooCommerce PDF Invoices plugin).

  • 写回答

1条回答 默认 最新

  • dongtao9095 2019-03-19 21:07
    关注

    The following will first save a custom message based on payment gateways as custom order meta data (custom-fields)… This will allow you to set this order custom field in your PDF invoice with much more ease (see the note at the end):

    // Save payment message as order item meta data
    add_filter( 'woocommerce_checkout_create_order', 'save_custom_message_based_on_payment', 10, 2 );
    function save_custom_message_based_on_payment( $order, $data ){
        if ( $payment_method = $order->get_payment_method() ) {
            if ( $payment_method === 'cheque' ) {
                // For Cheque
                $message = __("My custom message for Cheque payment", "woocommerce");
            } elseif ( $payment_method === 'bacs' ) {
                // Bank wire
                $message = __("My custom message for Bank wire payment", "woocommerce");
            }
            // save message as custom order meta data (custom field value)
            if ( isset($message) )
                $order->update_meta_data( '_payment_message', $message );
        }
    }
    

    Then the following will display this custom message on Order received page, view order page and email notifications, using hooks (not changing templates):

    // On "Order received" page (add payment message)
    add_filter( 'woocommerce_thankyou_order_received_text', 'thankyou_custom_payment_message', 10, 2 );
    function thankyou_custom_payment_message( $text, $order ) {
        if ( $message = $order->get_meta( '_payment_message' ) ) {
            $text .= '<br><div class="payment-message"><p>' . $message . '</p></div>' ;
        }
        return $text;
    }
    
    // On "Order view" page (add payment message)
    add_action( 'woocommerce_view_order', 'view_order_custom_payment_message', 5, 1 );
    function view_order_custom_payment_message( $order_id ){
        if ( $message = get_post_meta( $order_id, '_payment_message', true ) ) {
            echo '<div class="payment-message"><p>' . $message . '</p></div>' ;
        }
    }
    
    // Email notifications display (optional)
    add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 10, 4 );
    function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
        if( $sent_to_admin )
            return;
        elseif( $text = $order->get_meta('_payment_message') )
            echo '<div style="border:2px solid #e4e4e4;padding:5px;margin-bottom:12px;"><strong>Note:</span></strong> '.$text.'</div>';
    }
    

    Code goes on function.php file of your active child theme (or theme). Tested and works.


    Note for PDF Invoice

    The rule on stackOverFlow is one question at the time, so for one question one answer, to avoid your question be closed as too broad.

    As there is many different PDF invoices plugins for Woocommerce, you will have to read the developer documentation for the WooCommerce PDF Invoices plugin, to display that custom message in the PDF invoices.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?