In Woocommerce backend (admin), I have a function that allows the shop-manager to download all orders between two dates with a specific bunch of required data:
function write_to_file($date_initial, $date_final) {
global $attach_download_dir, $attach_download_file;
// Opens/creates file
$myfile = fopen($attach_download_dir . '/' . $attach_download_file, "w") or die("Unable to open file!");
// Populates first line
fwrite($myfile, 'Date; Parent Order ID; Order ID' . PHP_EOL);
// Retrieves orders data
if ( isset($date_initial) && isset($date_final) ) $args = array( 'date_created' => $date_initial . '...' . $date_final );
if ( isset($date_initial) && empty($date_final) ) $args = array( 'date_created' => '>=' . $date_initial );
if ( empty($date_initial) && isset($date_final) ) $args = array( 'date_created' => '<=' . $date_final );
if ( empty($date_initial) && empty($date_final) ) $args = array( );
$orders = wc_get_orders( $args );
// Populates file with orders data
foreach ($orders as $order) {
$order_data = $order->get_data();
fwrite($myfile,
// Date of order creation
$order_data['date_created']->date('d/M/Y') . '; ' .
// Parent Order ID
'#' . ( ( $order->get_type() === 'shop_order' ) ? $order->get_id() : $order->get_parent_id() ) . '; ' .
// Order ID
'#' . $order->get_id()
)
}
}
This function is triggered on a button click…
I would like To enable something similar from admin orders list bulk selection functionality. So the selected orders by shop manager on admin orders list (see the screenshot below) will be sent to a similar custom script and then downloaded.
In that case, the selected orders would override the specified dates, if any, in the orders retrieval.
However, I can't find a variable to access that tells me which orders are selected at that moment by the admin user.
Any help will be appreciated…
Here is the complete way to make your functionality work on bulk order list action selection, instead of date range:
Code goes in function.php file of your active child theme (or active theme). Tested and works.
In the returned url I have something like (for 2 selected / processed orders):
wp-admin/edit.php?post_type=shop_order&paged=1&write_downloads=1&processed_count=2&processed_ids=847%2C846
I can't test your included script but it's the way to do it on Woocommerce Orders Admin list.
VARIABLES:
The available variables are set by
add_query_arg()
function as you will see. When the action is triggered, you get those variables in the URL through GET method…You can set any variable yourself too…
In this example you can use
$_GET
or$_REQUEST
with:$_GET['write_downloads']
(the name action:true
orfalse
)$_GET['processed_count']
(the number of selected orders)$_GET['processed_ids']
(the Order Ids separated by an url-encoded coma%2C
)To remove a specific action from the dropdown orders bulk actions
For example we want to remove "On hold" status change: