在Woocommerce管理订单列表中添加可排序的自定义列

时间:2018-02-21 14:45:17

标签: php wordpress woocommerce hook-woocommerce orders

我已经在"订单"中添加了自定义列。 WooCommerce的部分邮寄邮政编码。列及其值正确显示。

我无法弄清楚如何对此字段进行排序(单击列标题)。其他代码示例我可以使用hook" manage_edit-shop_order_sortable_columns"来提及,但这似乎并不适用于此字段。

注意:我已经看到其他StackOverflow问题,但似乎没有任何排序工作。

/**
 * ADD ZIP CODE TO WOOCOMMERCE ORDERS LIST
 */

// Add column (working)
add_filter( 'manage_edit-shop_order_columns', 'custom_woo_columns_function' );
function custom_woo_columns_function( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns[ 'order_actions' ] );

    // all of your columns will be added before the actions column
    $new_columns['zipcode'] = 'Zip Code';

    //stop editing
    $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
    return $new_columns;
}

// Change order of columns (working)
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
    global $post;
    $zip_value = get_post_meta($post->ID, '_shipping_postcode', true);
    if ( $column == 'zipcode' ) {
        echo ( isset( $zip_value ) ? $zip_value : '' );
    }
}

// Sort by custom column (NOT WORKING)
add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
    $custom = array(
        'zipcode'    => '_shipping_postcode',
    );
    return wp_parse_args( $custom, $columns );
}

3 个答案:

答案 0 :(得分:2)

已更新:您也可以尝试使其可以排序和搜索:

// Add column (working)
add_filter( 'manage_edit-shop_order_columns', 'custom_woo_columns_function' );
function custom_woo_columns_function( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns[ 'order_actions' ] );

    // all of your columns will be added before the actions column
    $new_columns['zipcode'] = 'Zip Code';

    //stop editing
    $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];


    return $new_columns;
}

// Change order of columns ==> changed (working)
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
    global $post, $the_order;

    if ( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
        $the_order = wc_get_order( $post->ID );
    }

    if ( $column == 'zipcode' ) {
        $shipping_postcode = $the_order->get_shipping_postcode();
        echo empty($shipping_postcode) ? '' : $shipping_postcode;
    }
}

// Sort by custom column ==> changed (working)
add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
    $custom = array(
        'zipcode'    => '_shipping_postcode',
    );
    return wp_parse_args( $custom, $columns );
}

// Make '_shipping_postcode' metakey searchable in the shop orders list
add_filter( 'woocommerce_shop_order_search_fields', 'shipping_postcode_searchable_field', 10, 1 );
function shipping_postcode_searchable_field( $meta_keys ){
    $meta_keys[] = '_shipping_postcode';
    return $meta_keys;
}

代码进入活动子主题(或活动主题)的function.php文件。

经过测试和工作。

答案 1 :(得分:0)

我明白了。对于试图添加已排序的邮政编码列的其他人,请将此处留在此处。只需添加此附加操作即可。

// Make sorting by custom column work properly
add_action('pre_get_posts', 'custom_zipcode_orderby');
function custom_zipcode_orderby( $query ) {
    if ( !is_admin() ){ return; }

    $orderby = $query->get( 'orderby');
    if ('_shipping_postcode' == $orderby){
      $query->set('meta_key','_shipping_postcode');
      $query->set('orderby','meta_value_num');
    }
}

答案 2 :(得分:0)

列名order_actions不再存在。

代替wc_actions

我正在使用WooCommerce v3.6.2