// Добавление колонки в Заказы Woo
function child_theme_mod_order($columns) {
$new_columns = array();
foreach ($columns as $column_name => $column_info) {
$new_columns[$column_name] = $column_info;
if ($column_name === 'order_total') {
$new_columns['custom_column'] = __('Custom:', 'child-theme');
}
}
return $new_columns;
}
add_filter('manage_edit-shop_order_columns', 'child_theme_mod_order');
// Вывод
function child_theme_mod_order_content($column) {
global $post;
$order_id = $post->ID;
if ($column === 'custom_column') {
$order = wc_get_order($order_id);
$order_meta = get_metadata('post', $order_id, 'custom_column_meta', true);
if ($order_meta == 'true') {
echo 'TRUE';
} else {
if ($order_meta == '') {
add_metadata('post', $order_id, 'custom_column_meta', 'false', true);
}
update_metadata('post', $order_id, 'custom_column_meta', 'false');
echo 'FALSE';
}
}
}
add_action('manage_shop_order_posts_custom_column', 'child_theme_mod_order_content');
// Сортировка!!!
function child_theme_mod_order_sortable($columns) {
$custom = array(
'custom_column' => 'custom_column_meta',
);
return wp_parse_args($custom, $columns);
}
add_filter('manage_edit-shop_order_sortable_columns', 'child_theme_mod_order_sortable');
function child_theme_mod_order_sortable_query($query) {
if (!is_admin()) {
return;
}
$orderby = $query->get('orderby');
if ('custom_column_meta' == $orderby) {
$query->set('meta_key','custom_column_meta');
$query->set('orderby','meta_value');
$query->set('order','ASC');
}
}
add_action('pre_get_posts', 'child_theme_mod_order_sortable_query');
// Обновление при действии из внешнего плагина
function child_theme_modify_shop_orders_action($order_id) {
$order = wc_get_order($order_id);
$order->update_meta_data('custom_column_meta', 'true');
$order->save();
}
add_action('child_theme_custom_action_order_single_action', 'child_theme_modify_shop_orders_action');
add_action('child_theme_custom_action_order_bulk_action', 'child_theme_modify_shop_orders_action');