skip to Main Content

I’m building an online store and I have a private page where admins can scan a QR code and it displays the order details of the order assigned to that QR code. Admins can also set the status of the order on that page. I managed to display order notes in that page too. I also would like to add a form (textarea + submit button) to add a order note to the order, because our online store is about services, and I want to be able to put a note when the order is partially redeemed, so we don’t have to go to the desktop -> orders -> the order and add the note. Could somebody help me? I would need a code to directly add it where I want it, I mean, not in functions.php, a code for the custom template of the page (page-scanqr.php). Any help would be appreciated!

EDIT: I saw this and I thought that maybe it could help me somehow, but I don’t know how to apply it so it runs after pressing the submit button and sends the textarea text as a new order note: https://stackoverflow.com/a/49508808/12459095

As I say, I want to apply somehow this function. But I want to run it after pressing the button “submit” and I want it to pick the text from the text area and send it to wordpress as a new order note.

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 );

EDIT 2: I also show you the code that allows me to display the orders note, in case it could be helpful.

        <?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>';
} ?>

2

Answers


  1. Here is some psuedo code from my comment above:

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

    That right there will store it. To get it, do this:

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

    Hope that helps.

    EDIT:*

    WC stores the order notes in the ‘comments’ table. The orders are post types themselves of ‘shop_order’. Simply insert shop_order notes into the comments table as necessary using the order ID set to the ‘comment_post_ID’ field in the ‘comments’ table. Done and done.

    Login or Signup to reply.
  2. Add the follows codes snippet in your custom page template. or you can replace follows code in your template form structure –

    <?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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search