簡體   English   中英

在 Woocommerce 管理訂單編輯頁面中以編程方式添加自定義訂單備注

[英]Add a custom order note programmatically in Woocommerce admin order edit pages

在woocommerce中,我試圖通過php(以編程方式)在管理訂單編輯頁面中添加自定義訂單注釋。 我還沒有找到路。

任何幫助將不勝感激。

訂單管理頁面中的 WooCommerce 訂單說明

從動態訂單 ID,您可以這樣使用WC_Order add_order_note()方法:

// If you don't have the WC_Order object (from a dynamic $order_id)
$order = wc_get_order(  $order_id );

// The text for the note
$note = __("This is my note's text…");

// Add the note
$order->add_order_note( $note );

測試和工作。

謝謝你們,我正試圖找到一種將注釋添加到新訂單的方法。 我正在使用@LoicTheAztec 發布的解決方案尋找正確的鈎子。 這是對我有用的解決方案,希望它可以幫助其他人。

將此添加到 Functions.php 文件中

add_action( 'woocommerce_new_order', 'add_engraving_notes',  1, 1  );

function add_engraving_notes( $order_id ) {
 //note this line is different 
 //because I already have the ID from the hook I am using.
 $order = new WC_Order( $order_id ); 

 // The text for the note
 $note = __("Custom Order Note Here");

 // Add the note
 $order->add_order_note( $note );

}

這段代碼會幫你在functions.php中添加代碼

<?php

add_action('woocommerce_after_order_notes', 'customise_checkout_field');

function customise_checkout_field($checkout)
{
    echo '<div id="customise_checkout_field"><h2>' . __('Heading') . '</h2>';
    woocommerce_form_field('customised_field_name', array(
        'type' => 'text',
        'class' => array(
        'my-field-class form-row-wide'
    ) ,
    'label' => __('Customise Additional Field') ,
    'placeholder' => __('Guidence') ,
    'required' => true,
    ) , $checkout->get_value('customised_field_name'));
    echo '</div>';
}

對於自定義字段的數據驗證,請使用下面給出的代碼:

add_action('woocommerce_checkout_process', 'customise_checkout_field_process');
 
function customise_checkout_field_process()
{
    // if the field is set, if not then show an error message.
    if (!$_POST['customised_field_name']) wc_add_notice(__('Please enter value.') , 'error');
}

更新字段值

add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
                     
function customise_checkout_field_update_order_meta($order_id)
{
    if (!empty($_POST['customised_field_name'])) {
        update_post_meta($order_id, 'Some Field', sanitize_text_field($_POST['customised_field_name']));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM