I would like to know if its possible to check if a paid order has been modified by admins or shop managers. I have a WooCommerce books website with multiple admins. Some of them are books suppliers and if they couldn't supply any of books in a costumer order they can modify an order and delete some items, calling the costumer and refund the related items.
How can I find those orders and see which books was removed and refunded?
I wrote some code that is supposed to handle that, But I would like to know if it's possible with woocommerce functions in backend.
Any help will be appreciated.
Here is the code i wrote in functions of theme:
// my idea is to add any successful order's id and it's products to a
// table(name it tmp) in database and then compare this table and wp_posts table
// per orders and if products for a order id is not in wp-posts table
// but it is in my table it means this order is modified and the code
// compare products and print out the deleted product.
add_action('woocommerce_payment_complete','add_orders_to_tmp_table');
function add_orders_to_tmp_table($order_id){
// check after successful order if the order number is not in table it adds the order to table
global $wpdb;
$rowcount = $wpdb->get_var("SELECT COUNT(*) FROM order_tmp WHERE order_id = '$order_id'");
if($rowcount < 1):
$order = wc_get_order($order_id);
$order_id = $order->get_id();
$items = $order->get_items();
$products = [];
foreach ($items as $item) {
array_push($products,$item->get_name());
}
$wpdb->insert('order_tmp',array('order_id'=>$order_id,'products'=>serialize($products)));
$wpdb->close();
endif;
// i wrote the below code because some of the orders was not stored
//in the tmp table after successful payment i didn't find the problem
//and because of that wrote the below code i even change the hooked
//function to woocommerce_thankyou but still some orders were not in tmp table
// here we get all order from X date to N date that is successful
// but not in our table and if any, we add it to table
// it will make the list up to date by checking after any
//successful order for any missed successful order and then add it to the tmp table.
$date_from = date("Y-m-d",strtotime("-1 day"));
$date_to = date("Y-m-d");
$post_status = implode("','", array('wc-processing', 'wc-completed') );
$order_ids = [];
$result = $wpdb->get_results( "SELECT * FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_status IN ('{$post_status}')
AND post_date BETWEEN '{$date_from} 00:00:00' AND '{$date_to} 23:59:59'
");
foreach ($result as $i) {
array_push($order_ids,$i->ID);
}
foreach($order_ids as $i){
$rowcount = $wpdb->get_var("SELECT COUNT(*) FROM order_tmp WHERE order_id = '$i'");
if($rowcount == 0):
$order = wc_get_order($i);
$items = $order->get_items();
$products_new = [];
foreach ($items as $item) {
array_push($products_new,$item->get_name());
}
// print_r($products);
// $serialized_products = serialize($products_new);
$serialized_products = serialize($products_new);
// print($serialized_products);
$result = $wpdb->insert('order_tmp',array('order_id'=>$i,'products'=>$serialized_products,));
endif;
}
}
and then here i'm gonna check paid orders and their products for a difference:
global $wpdb;
$date_from = date("Y-m-d",strtotime("-30 day"));;
$date_to = date("Y-m-d");
$post_status = implode("','", array('wc-processing', 'wc-completed') );
$result = $wpdb->get_results( "SELECT * FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_status IN ('{$post_status}')
AND post_date BETWEEN '{$date_from} 00:00:00' AND '{$date_to} 23:59:59'
");
$order_ids = [];
foreach ($result as $i) {
array_push($order_ids,$i->ID);
}
// print_r($order_ids);
$count = 0;
foreach($order_ids as $i){
$rowcount = $wpdb->get_var("SELECT COUNT(*) FROM order_tmp WHERE order_id = '$i'");
if($rowcount >= 1):
$count++;
$products = [];
$order = wc_get_order($i);
$items = $order->get_items();
foreach ($items as $item) {
array_push($products,$item->get_name());
}
// print_r($products);
// Date from database
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT products from order_tmp where order_id=%d",$i
)
);
foreach ($results as $result) {
$array = json_decode( json_encode($result), true);
$order_current_status = unserialize($array["products"]);
// echo "***********************************";
}
$order_diff = array_diff($products,$order_current_status);
if($order_diff):
echo $i;
echo '<br>';
foreach ($order_diff as $key) {
echo '<br>';
echo 'yes' . '-------' . $key;
echo '<br>';
}
else:
echo 'No product deleted from this order' . '---' . $i;
echo '<br>';
echo 'Created date: ' . $order->get_date_paid();
echo '<br>';
echo 'Modified Date: ' . $order->get_date_modified();
endif;
echo '<br>';
echo '------------------------------------------------------------------------------------------';
echo '<br>';
elseif($rowcount == 0):
echo "<h1>was not in db-tmp $i</h1>";
endif;
}