Hello Developer,
I like this approach for deleting all orders, it’s a good start to clean out an entire shop. But there are a few improvements to be made, to make it more secure and accessible for everyone.
1. In your code you have put on line 41 – 43 “DELETE from wp_”, however if your database doesn’t start with wp_ this won’t work. And most advanced wordpress users won’t name their database wp_.
2. This is more a security concern, I noticed that the sql queries are running through the entire database on the server. I have had once it deleted all orders from 2 other databases who share the same server and started with the same wp_. This needs to be improved, I think you can tackle this by adding database info related to the current store, like:
$host = ‘localhost’;
$username = ‘user’;
$password = ‘password’;
$db = ‘dbname’;
I am not a specialist in plugin coding, otherwise I could be of better help, but I hope you will consider these 2 flaws.
Thanks and keep up the good work.
]]>Hi,
The plugin does′nt delete correctly, not only because database prefix should be added but also because the query is not concatenated, so only executes the last query.
$queries = $results = array();
$queries[] = “DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta;”;
$queries[] = “DELETE FROM {$wpdb->prefix}woocommerce_order_items;”;
$queries[] = “DELETE FROM {$wpdb->prefix}posts WHERE post_type = ‘shop_order’;”;
$queries[] = “DELETE FROM {$wpdb->prefix}posts WHERE post_type = ‘mail-debug’;”;
foreach( $queries as $query ) {
$results[] = $wpdb->query( $query );
}
Error treatment should be implemented later.
Regards
]]>Just a heads up you should update your wc_delete_orders
function to account for different database prefixes.
public function wc_delete_orders() {
global $wpdb;
$ran = true;
$sql = "DELETE FROM " . $wpdb->prefix . "woocommerce_order_itemmeta";
$sql = "DELETE FROM " . $wpdb->prefix . "woocommerce_order_items";
$sql = "DELETE FROM " . $wpdb->prefix . "posts WHERE post_type = 'shop_order'";
$rows = $wpdb->query( $sql );
if( false !== $rows ) {
$this->deleted = $rows;
//add_action( 'admin_notices', array( $this, 'admin_notice_success' ) );
}
}
]]>