Wordpress - WooCommerce - 在桌面外显示"Add Order Note"表单



我正在建立一个在线商店,我有一个私人页面,管理员可以在其中扫描二维码,它显示分配给该二维码的订单的订单详细信息。管理员还可以在该页面上设置订单的状态。我也设法在该页面中显示订单注释。我还想添加一个表单(textarea + 提交按钮(来为订单添加订单备注,因为我们的在线商店是关于服务的,我希望能够在订单部分兑换时添加备注,所以我们不必去桌面 ->订单 ->订单并添加注释。有人可以帮助我吗?我需要一段代码来直接将其添加到我想要的地方,我的意思是,不是在 functions.php 中,是页面自定义模板的代码 (page-scanqr.php(。任何帮助将不胜感激!

编辑:我看到了这个,我想也许它可以以某种方式帮助我,但我不知道如何应用它,所以它在按下提交按钮后运行并将文本区域文本作为新订单发送:https://stackoverflow.com/a/49508808/12459095

正如我所说,我想以某种方式应用此功能。但是我想在按下"提交"按钮后运行它,我希望它从文本区域中选择文本并将其作为新的订单单发送到 wordpress。

function add_redeeming_notes( $order_id ) {
$order = new WC_Order( $order_id ); 
// The text for the note
$note = __("I WANT THIS TO PICK THE TEXT FROM THE TEXTAREA");
// Add the note
$order->add_order_note( $note );

编辑2:我还向您展示了允许我显示订单注释的代码,以防万一。

<?php 
$order_notes = get_private_order_notes( $order_id );
foreach($order_notes as $note){
$note_id = $note['note_id'];
$note_date = $note['note_date'];
$note_author = $note['note_author'];
$note_content = $note['note_content'];
// Outputting each note content for the order
echo '<p>'.$note_date.' - '.$note_content.'</p>';
} ?>

在自定义页面模板中添加以下代码段。 或者,您可以替换模板表单结构中的以下代码 -

<?php 
if ( isset( $_POST['order_note_nonce_field'] ) && wp_verify_nonce( $_POST['order_note_nonce_field'], 'order_note_action' ) ) {
$order_id = ( isset( $_POST['order_id'] ) && $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : '';
$order_note = ( isset( $_POST['order_note'] ) && $_POST['order_note'] ) ? sanitize_textarea_field( $_POST['order_note'] ) : '';
$order = wc_get_order( $order_id );
if( $order && $order_note ) {
// Add the note
$order->add_order_note( $order_note );
}
}
?>
<form action="" method="post">
<?php wp_nonce_field( 'order_note_action', 'order_note_nonce_field' ); ?>
<input type="hidden" name="order_id" value="21" /> <!-- dont forgot to replace the value with your current order id -->
<div class="form-field">
<textarea name="order_note"></textarea>
</div>
<div class="form-field">
<input type="submit" value="Add Note" />
</div>
</form>

这是我上面评论中的一些伪代码:

if (isset($_POST['your_submit_action)) {
update_post_meta($id_of_product, 'your_text_key', $_POST['your_text_field'];
}

就在那里会存储它。 要获取它,请执行以下操作:

$text = get_post_meta($id_of_product, 'your_text_key', TRUE);

希望有帮助。

编辑:*

WC将订单备注存储在"注释"表中。 订单本身是"shop_order"的帖子类型。 只需根据需要使用设置为"注释"表中"comment_post_ID"字段的订单 ID 将shop_order注释插入注释表即可。 完了,完了。

最新更新