2

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>';
} ?>
Eva Marrez
  • 45
  • 5
  • 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 – Eva Marrez Dec 06 '19 at 05:18
  • It's hard to tinker a live shop being without knowledge. Even though this site can help in a lot of ways, your question is off topic. Please visit [help center.](https://stackoverflow.com/help/on-topic) – Reigel Gallarde Dec 06 '19 at 05:26
  • you want to order notes in admin am I right? – Jinesh Dec 06 '19 at 05:45
  • @Reigel I don't understand your comment. I was pretty specific on what I wanted. I have an online store with WooCommerce and I want to display an add order note form in a private page, only accessible by admins. I already have that page created and everything placed, the only thing missing is the add order note form. I even replied with a comment of the code that could help me but I don't know how to adapt it to my situation. – Eva Marrez Dec 06 '19 at 06:14
  • @Jinesh I want a "Add note to order" form, in a custom page I have. That page scans a QR code and it displays the order status, data, and the order notes. I want a "add note to order" form so when they mark the order as partially redeemed, they can type "Redemeed this service, this other service still available to redeem", so next time they scan the code, they know what service can be redemeed and what services where already redemeed. That's because the order can be multiple services. – Eva Marrez Dec 06 '19 at 06:17
  • Please add step how you add that page. if possible put html – Jinesh Dec 06 '19 at 06:27
  • @Jinesh, it doesn't matter how she creates that admin page. Eva Marrez, let me explain WC and WP a bit. WC uses the post type 'product'. Post types are stored in the 'posts' table in the DB. Data for products is stored in the 'post_meta' table. When you have your form/page submit the text box, just put it into your own custom 'post_meta' record for the 'product' post ID. Really simple. Then, when an admin pulls this page back up later, simply populate that textarea by first fetching the value of this 'post_meta'. – mrwpress Dec 06 '19 at 06:31
  • @mrwpress yes it is matter because How can you know which method used for fetch order status? – Jinesh Dec 06 '19 at 06:34
  • It doesn't matter. We are past that point. She has stated she already gets the data. So, she has the Object ID (post ID). If she has that the World is her oyster. The only thing she needs to do is insert this into the 'post_meta' table per my answer below. – mrwpress Dec 06 '19 at 06:37

2 Answers2

1

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>
itzmekhokan
  • 2,597
  • 2
  • 9
  • 9
0

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.

mrwpress
  • 311
  • 1
  • 8
  • The thing is I don't know if this is what I need. I want to be able to fill a order note in frontend, and end up as another order note like when you change the status of that order, screenshot: https://imgur.com/DqSYBHV // Also thank you in advance for your help ♥ – Eva Marrez Dec 06 '19 at 06:48
  • @EvaMarrez - Check out this page: https://docs.woocommerce.com/document/managing-orders/ – mrwpress Dec 06 '19 at 06:49
  • So, based on my answer above, the 'Order' is the post here. The "notes" are post_meta. I don't know how exactly WC stores the notes in the 'post_meta' for the post type 'shop_order' but I'll bet it is a JSON string in that table. I know that is post type because if you go to the "Orders" page in the admin, you can see it in the URL: /wp-admin/edit.php?post_type=shop_order – mrwpress Dec 06 '19 at 06:51
  • Okay, I'll try to use it and if it works I'll post the code here so it can be used by anyone who needs it. Thank you!! – Eva Marrez Dec 06 '19 at 06:54
  • @EvaMarrez It will work for sure. But, you will need to know where it is stored ;). It is NOT in the 'post_meta'. WC stores these notes as comments in the 'comments' table: https://snipboard.io/u4Q5Iv.jpg – mrwpress Dec 06 '19 at 06:59
  • I still can't apply it, I tried several ways and nothing worked. I edited the original question adding the code that displays the order notes, also I attach here the code of the order notes box of the backend in case is helpful so you can see how woocommerce stores the order notes: https://justpaste.it/1qd9m – Eva Marrez Dec 06 '19 at 07:55
  • @EvaMarrez your code there is adding a custom meta box. That is going to be added in the order page in the admin. That will not be added to your custom admin page. You said you had a custom admin page, correct? The code you posted is adding a metabox to the 'shop_order' post edit page in the admin. That is why you can't save this value I presume. From your custom page, write a query using $wpdb to insert your custom note into the comments table. – mrwpress Dec 06 '19 at 08:02
  • Better than $wpdb, use the wp_insert_comment(): https://developer.wordpress.org/reference/functions/wp_insert_comment/ – mrwpress Dec 06 '19 at 08:04
  • 1. Create a custom admin page. 2. You need to have that page be a form and put all the values into the form pre-populated. 3. You stored the update on submission. You have to be able to intercept your form data. So, do you have a form and are . you able to get values from it? If so, insert the comment via the wp_insert_comment() function from WP core. – mrwpress Dec 06 '19 at 08:11