C
C
Cyril2018-04-24 01:41:10
WordPress
Cyril, 2018-04-24 01:41:10

Woocommerce how to sort by meta field with true and false values?

There is some code that adds a sort column to woo orders, but the sort doesn't work. in theory, the value of the order is true or false, and when you click on the title, sorting should work, but at the moment it sorts by date instead of the meta field. Where did I go wrong?!

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');

All code
// Добавление колонки в Заказы 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');

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question