John Davidson

php - Determine if an WooCommerce order contains order notes while using "woe_order_export_started" hook

0 comments
Message:


The woe_order_export_started filter hook is related to a plugin


This code is used for exporting orders with total > 10


add_filter( 'woe_order_export_started',  function ( $order_id ) { 
$order = new WC_Order($order_id);
return ($order->get_total() > 10.00) ? $order_id: false;
});



I am trying to create a php code to find out whether the order has order note or not, and to ignore the orders which has no order note.


I found out the code to get the order notes


$args = array(
'post_id' => $order->id,
'approve' => 'approve',
'type' => 'order_note',
'search' => 'Order status changed from Pending Payment to Processing.',
);
// woocommerce hides such records by default
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10 );
$notes = get_comments( $args );
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );

How to I implement this with the woe_order_export_started filter hook to determine if the order has order note or not.


Basically I want to avoid exporting the orders which has no order note. So it should return false if there is no order note.



My Answer:
To determine if an WooCommerce order contains order notes, you can use the following code snippet:

php
$order_id = 123; // Replace 123 with the actual order ID

$order = wc_get_order( $order_id );

if ( $order ) {
$order_notes = $order->get_customer_order_notes();

if ( ! empty( $order_notes ) ) {
echo 'This order contains order notes.';
} else {
echo 'This order does not contain any order notes.';
}
} else {
echo 'Invalid order ID.';
}


This code snippet first retrieves the WooCommerce order object using the `wc_get_order()` function. Then, it checks if the order object exists and gets the order notes using the `get_customer_order_notes()` method. Finally, it checks if the order notes array is not empty and displays a message accordingly.

Rate this post

3 of 5 based on 1798 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us