Загрузка массовых заказов на ПК и отображение сообщения администратора в Woocommerce admin

В Woocommerce я реализовал код ниже из этой ветки ответов на один из моих вопросов Это позволяет пользователю-администратору загружать стандартизированную информацию о заказах оптом из бэкэнда «Список заказов магазина». Чтобы достичь этого, пользователь либо выбирает период между двумя датами, либо выбирает желаемые заказы один за другим; после чего он запускает новое действие «загрузить в файл» для обработки своего запроса.

Мой код разделен на 4 подключенных функции:

  • первый отображает html-контент, в том числе ввод даты и новое массовое действие, разрешающее действие загрузки;

  • во втором случае основное действие происходит, когда пользователь нажимает кнопку «применить действие»: собирает всю информацию о woocommerce и записывает ее в файл на сервере;

  • третий просит пользователя загрузить файл на его компьютер (мне пришлось сегрегировать этот файл из второй части, потому что единственный способ для моего загруженного файла, в котором не было много смешанного мусора HTML, это добавление функции «выход» в конце ; а также

  • четвертый просто отображает сообщение пользователю, информирующее, сколько заказов было обработано и загружено.

// 1st SUB-SNIPPET: Adding to admin order list bulk dropdown a custom action 'custom_downloads'
add_filter( 'bulk_actions-edit-shop_order', 'downloads_bulk_actions_edit_product', 100, 1 );
function downloads_bulk_actions_edit_product( $actions ) {

?><div class="alignleft actions custom">

<span>From: </span>
<input type="date" id="download_date_initial" name="download_date_initial"  value="<?php echo date('Y-m-d'); ?>" class="input-date" />
<span> To: </span>
<input type="date" id="download_date_final" name="download_date_final"  value="<?php echo date('Y-m-d'); ?>" class="input-date" />

</div><?php

$actions['write_downloads'] = __( 'Download orders', 'woocommerce' );
return $actions;
}

// 2nd SUB-SNIPPET: Make the action from selected orders
add_filter( 'handle_bulk_actions-edit-shop_order', 'downloads_handle_bulk_action_edit_shop_order', 10, 3 );
function downloads_handle_bulk_action_edit_shop_order( $redirect_to, $action, $post_ids ) {
global $attach_download_dir, $attach_download_file;
global $countries_list, $countries_obj;

if ( $action !== 'write_downloads' ) return $redirect_to; // Exit

$processed_ids = array();

// Opens file
$myfile = fopen($attach_download_dir . '/' . $attach_download_file, "w") or die("Unable to open file!");

$date_initial = $_REQUEST['download_date_initial'];
$date_final = $_REQUEST['download_date_final'];
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_ = [];

if ( isset($post_ids) && empty($args_)) $array = $post_ids; else {
$orders = wc_get_orders( $args_ );
$array=[]; if (isset($orders)) foreach ($orders as $order) array_push($array, $order->get_id());
}

// Goes through each selected order
foreach ( $array as $key => $post_id ) {
$order = wc_get_order( $post_id );
$order_data = $order->get_data();

// Fills in woocommerce orders info as required ...
fwrite($myfile, XXX);

$processed_ids[] = $post_id;
}
// Closes file
fclose($myfile);

// Returns info to be used elsewhere, namely displaying message
return $redirect_to = add_query_arg( array( 'write_downloads' => '1', 'processed_count' => count( $processed_ids ), ), $redirect_to );

}

// 3rd SUB-SNIPPET: requests admin user to download file to his PC
add_filter( 'handle_bulk_actions-edit-shop_order', 'downloads_handle_bulk_action_edit_shop_order_2', 20, 3 );
function downloads_handle_bulk_action_edit_shop_order_2( $redirect_to, $action, $post_ids ) {
global $attach_download_dir, $attach_download_file;
global $root_page;

// Saves in pc
$file_url = $root_page . 'wp-admin/' . $attach_download_dir . '/' . $attach_download_file;
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename=' . basename($file_url));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($file_url);
exit;
}

// 4th SUB-SNIPPET: The results notice from bulk action on orders
add_action( 'admin_notices', 'downloads_bulk_action_admin_notice', 10 );
function downloads_bulk_action_admin_notice() {

if ( empty( $_REQUEST['write_downloads'] ) ) return; // Exit
$count = intval( $_REQUEST['processed_count'] );
printf( '<div id="message" class="updated fade"><p>' . _n( 'Processed %s order for downloads.', 'Processed %s orders for downloads.', $count, 'write_downloads' ) . '</p></div>', $count );

}

Однако у меня есть 3 проблемы здесь:

  1. Если у меня есть «часть для загрузки на ПК», то есть третий фрагмент кода, сообщение перестает отображаться в серверной части. Если я достану его, я смогу увидеть сообщение, но пользователю больше не требуется загружать файл на свой компьютер. Я попробовал несколько альтернатив, но все безуспешно.

  2. Если кто-то хочет загружать заказы, выбранные один за другим, проблем нет. Но если кто-то хочет выбрать все заказы между двумя датами, пользователь-администратор также должен выбрать хотя бы один заказ, чтобы выполнить массовое действие. В противном случае он не обработает обработчик handle_bulk_actions-edit-shop_order. Это раздражает и не имеет смысла для пользователя, так как этот заказ выбирается только для того, чтобы система прошла через этот крючок, даже если в этом случае только две даты имеют отношение к определению, какие заказы должны быть загружены.

  3. Я хотел бы, чтобы действие «скачать» было выбрано по умолчанию в кнопке действия, без необходимости выбора пользователем. Я пытался сделать это именно с Javascript, но безрезультатно …

Есть идеи?

1

Решение

Для проблемного пункта 2 (два), Массовые действия работают только на выбранные заказы и не обрабатывать выбор диапазона дат… поэтому я частично удалил ваш код в вашей 1-й подключенной функции (ниже) …

Ваша функция загрузки диапазона дат, должна быть добавлена ​​отдельно где-то еще.

Для проблемного пункта 3 (три), это решается в первой подключенной функции ниже (проверено и работает).

Для проблемного пункта 1 (один) Я удалил ловушку из вашей третьей функции и вместо этого вызвал ее из вашей последней подключенной функции, которая отображает пользовательское уведомление … Я надеюсь, что это будет работать (не проверено).

Я перевернул (переупорядочил) ваши 3-ю и последнюю функции:

// 1. Adding to admin order list bulk dropdown a custom action 'custom_downloads'
add_filter( 'bulk_actions-edit-shop_order', 'downloads_bulk_actions_edit_product', 100, 1 );
function downloads_bulk_actions_edit_product( $actions ) {
$reordered_actions = array( 'write_downloads' => __( 'Download orders', 'woocommerce' ) );
foreach( $actions as $key => $action ){
// Reinserting reordered actions
$reordered_actions[$key] = $action;
}
?>
<script type="text/javascript">
jQuery( function($){
$('#bulk-action-selector-top').find('option').eq(0).remove(); // Remove defaut option
$('#bulk-action-selector-top').val('write_downloads'); // To be sure select "downloads"…
});
</script>
<?php
return $reordered_actions;
}

// 2. Make the action from selected orders
add_filter( 'handle_bulk_actions-edit-shop_order', 'downloads_handle_bulk_action_edit_shop_order', 10, 3 );
function downloads_handle_bulk_action_edit_shop_order( $redirect_to, $action, $post_ids ) {
global $attach_download_dir, $attach_download_file, $countries_list, $countries_obj;

if ( $action !== 'write_downloads' ) return $redirect_to; // Exit

$processed_ids = array();

// Opens file
$myfile = fopen($attach_download_dir . '/' . $attach_download_file, "w") or die("Unable to open file!");

$date_initial = $_REQUEST['download_date_initial'];
$date_final = $_REQUEST['download_date_final'];
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_ = [];

if ( isset($post_ids) && empty($args_)) $array = $post_ids; else {
$orders = wc_get_orders( $args_ );
$array=[]; if (isset($orders)) foreach ($orders as $order) array_push($array, $order->get_id());
}

// Goes through each selected order
foreach ( $array as $key => $post_id ) {
$order = wc_get_order( $post_id );
$order_data = $order->get_data();

// Fills in woocommerce orders info as required ...
fwrite($myfile, XXX);

$processed_ids[] = $post_id;
}
// Closes file
fclose($myfile);

// Returns info to be used elsewhere, namely displaying message
return $redirect_to = add_query_arg( array( 'write_downloads' => '1', 'processed_count' => count( $processed_ids ), ), $redirect_to );

}

// 3. Display a results notice from this bulk action on orders and trigger download to PC
add_action( 'admin_notices', 'downloads_bulk_action_admin_notice', 10 );
function downloads_bulk_action_admin_notice() {
if ( empty( $_REQUEST['write_downloads'] ) ) return; // Exit

$count = intval( $_REQUEST['processed_count'] );
printf( '<div id="message" class="updated fade"><p>' . _n( 'Processed %s order for downloads.', 'Processed %s orders for downloads.', $count, 'write_downloads' ) . '</p></div>', $count );

// Execute the request to download file to  PC
downloads_to_pc();
}

// 4. Simple function that request user to download file to his PC
function downloads_to_pc() {
global $attach_download_dir, $attach_download_file, $root_page;

// Saves in pc
$file_url = $root_page . 'wp-admin/' . $attach_download_dir . '/' . $attach_download_file;
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename=' . basename($file_url));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($file_url);
exit;
}

Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Не проверенный, это могло работать.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector