Основываясь на коде ответа "Добавить кнопку поверх списка заказов администратора в woocommerce", я смог добавить пользовательскую кнопку в список заказов администратора woocommerce.
Вот этот код (слегка подгонянный):
add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
global $typenow;
if ( 'shop_order' === $typenow && 'top' === $which ) {
?>
<div class = "alignleft actions custom">
<button type = "submit" name = "custom_" style = "height:32px;" class = "button" value = ""><?php
echo __( 'Import Couriers', 'woocommerce' ); ?></button>
</div>
<?php
}
}
Теперь мне нужно запустить следующую функцию при нажатии этой пользовательской кнопки:
function update_shipping_couriers_meta_field() {
$dir = __DIR__;
$couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$count = count(couriers);
$i = 1;
do {
if ( !empty( $couriers ) ) {
foreach ( $couriers as $a ) {
if ( !empty( $a ) ) {
$rows = explode(';', $a);
$id = $rows[0];
$id = int($id);
$couriers = $rows[1];
update_post_meta( $id, '_shipping_couriers', $couriers );
}
$i++;
}
}
}
while ( $i <= $count );
}
На практике функция обновляет настраиваемое поле «_shipping_couriers» на основе определенного идентификатора заказа. Два значения присутствуют в файле csv.
Я уже протестировал его, и он работает. Я «просто» запускаю его, когда нажимаю кнопку, которую я создал с помощью функции выше.
Как я могу запустить свою функцию при нажатии кнопки?
В вашем коде есть некоторые недостающие вещи и ошибка в вашей последней функции, где вместо count(couriers);
должно быть count($couriers);
.
// Display an action button in admin order list header
add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
global $pagenow, $typenow;
if ( 'shop_order' === $typenow && 'edit.php' === $pagenow && 'top' === $which ) {
?>
<div class = "alignleft actions custom">
<button type = "submit" name = "import_courier" style = "height:32px;" class = "button" value = "yes"><?php
echo __( 'Import Couriers', 'woocommerce' ); ?></button>
</div>
<?php
}
}
// Trigger an action (or run some code) when the button is pressed
add_action( 'restrict_manage_posts', 'display_admin_shop_order_language_filter' );
function display_admin_shop_order_language_filter() {
global $pagenow, $typenow;
if ( 'shop_order' === $typenow && 'edit.php' === $pagenow &&
isset($_GET['import_courier']) && $_GET['import_courier'] === 'yes' ) {
## -------- The code to be trigered -------- ##
update_shipping_couriers_meta_field();
## -------------- End of code -------------- ##
}
}
// Your function that will be triggered on button press
function update_shipping_couriers_meta_field() {
$dir = __DIR__;
$couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$count = count($couriers);
$i = 1;
do {
if ( ! empty( $couriers ) ) {
foreach ( $couriers as $a ) {
if ( ! empty( $a ) ) {
$rows = explode(';', $a);
update_post_meta( intval($rows[0]), '_shipping_couriers', $rows[1] );
}
$i++;
}
}
}
while ( $i <= $count );
}
Код находится в файле functions.php вашей активной дочерней темы (или активной темы). Проверено и работает.
На основе: Добавить кнопку поверх списка заказов администратора в woocommerce