57993

Add a custom order note programmatically in Woocommerce admin order edit pages

Question:

In woocommerce I am trying to add a custom order note in the admin order edit pages through php (so programmatically). I haven't find the way yet.

Any help will be appreciated.

<a href="https://i.stack.imgur.com/YgBZf.png" rel="nofollow"><img alt="WooCommerce order note in the order admin page" class="b-lazy" data-src="https://i.stack.imgur.com/YgBZf.png" data-original="https://i.stack.imgur.com/YgBZf.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

From a dynamic Order Id you can use WC_Order <a href="https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html#_add_order_note" rel="nofollow">add_order_note()</a> method this way:

// 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 ); // Save the data $order->save();

Tested and works.

Answer2:

This code will do trick for you add code in functions.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>'; }

For data validation of the custom field use the code given below:

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

Update value of field

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'])); } }

Answer3:

Thank you guys I was trying to find a way to add the notes to a new order. I was looking for the right hook using the solution that @LoicTheAztec posted. This is the solution that worked for me hope it helps someone else out there.

add this to the Functions.php file

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 ); // Save the data $order->save(); }

Recommend

  • Wordpress add_rewrite_rule not working
  • Wordpress - single.php called twice
  • wordpress plugin redirect from page to page permisson error
  • woocommerce in wordpress return always simple as product type
  • Disable add to cart redirection
  • how to sort an arraylist that contains string arrays?
  • Is it possible to specialize on a static lifetime?
  • Quick Question About Get and Set
  • redirect_to root_url and return unless @user.activated
  • Cannot save model when using ember render helper
  • C#: Import/Export Settings into/from a File
  • How can I set a binding to a Combox in a UserControl?
  • AJAX Html Editor Extender upload image appearing blank
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Meteor helpers not available in Angular template
  • How to recover from a Spring Social ExpiredAuthorizationException
  • output of program is not same as passed argument
  • Does CUDA 5 support STL or THRUST inside the device code?
  • ILMerge & Keep Assembly Name
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Large data - storage and query
  • Why winpcap requires both .lib and .dll to run?
  • WOWZA + RTMP + HTML5 Playback?
  • trying to dynamically update Highchart column chart but series undefined
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • embed rChart in Markdown
  • How can I remove ASP.NET Designer.cs files?
  • Django query for large number of relationships
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal