如何在Woocommerce中按”Ship To”列对订单表进行排序?

huangapple go评论66阅读模式
英文:

How to Sort the Orders Table by Ship To Column in Woocommerce?

问题

我试图使WooCommerce订单表中的“Ship to”列按“Ship To”地址排序。

我明白“Ship to”值是从多个表单字段汇总而来的,但我无法按照Shipping Address 1进行排序。

如何使WooCommerce订单表中的“Ship to”地址可排序?

谢谢你的帮助。

目前我最好的尝试是使shipping_address_1(美国的街道地址)可排序:

// 使列可排序
add_action('pre_get_posts', 'custom_street_address_orderby');
function custom_street_address_orderby($query)
{
    if (!is_admin()) return;
    
    $orderby = $query->get('orderby');
    if ('_shipping_address_1' == $orderby) {
        $query->set('meta_key', '_shipping_address_1');
        $query->set('orderby', 'meta_value_num');
    }
}
英文:

I am attempting to make the "Ship to" column in the Woocommerce Orders table sortable by "Ship To" address.

I understand that the the Ship to values are aggregated from multiple form fields, but I cannot get them to sort by Shipping Address 1.

How does one go about making the "Ship to" address sortable in the Orders Table in WooCommerce?

Thanks in advance for your help.

Best I have currently is an attempt to make the shipping_address_1 (the street address in the USA) sortable:

// Make sorting by column
add_action('pre_get_posts', 'custom_street_address_orderby');
function custom_street_address_orderby($query)
{
    if (!is_admin()) return;
    
    $orderby = $query->get('orderby');
    if ('_shipping_address_1' == $orderby) {
        $query->set('meta_key', '_shipping_address_1');
        $query->set('orderby', 'meta_value_num');
    }
}

答案1

得分: 1

在WooCommerce管理订单中,您可以使“Ship to”列可排序(基于'_shipping_address_1' meta_key),使用以下代码:

// 让现有的“Ship to”列在管理订单列表中可排序
add_filter("manage_edit-shop_order_sortable_columns", 'shop_order_column_meta_field_sortable');
function shop_order_column_meta_field_sortable( $columns )
{
    $meta_key = '_shipping_address_1';
    return wp_parse_args( array('shipping_address' => $meta_key), $columns );
}

// 使“Ship to”可以正确排序(按数值值)
add_action('pre_get_posts', 'shop_order_column_meta_field_sortable_orderby' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
    global $pagenow;

    // 仅定位到WooCommerce管理订单列表
    if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ){

        $orderby  = $query->get( 'orderby');
        $meta_key = '_shipping_address_1';

        if ('_shipping_address_1' === $orderby){
          $query->set('meta_key', $meta_key);
          $query->set('orderby', 'meta_value_num'); // 或使用 'meta_value'
        }
    }
}

将此代码放入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

英文:

In WooCommerce admin orders, you can make the "Ship to" column sortable (based on '_shipping_address_1' meta_key) using the following:

// Make existing "Ship to" column sortable on admin orders list
add_filter( "manage_edit-shop_order_sortable_columns", 'shop_order_column_meta_field_sortable' );
function shop_order_column_meta_field_sortable( $columns )
{
    $meta_key = '_shipping_address_1';
    return wp_parse_args( array('shipping_address' => $meta_key), $columns );
}

// Make "Ship to" sorting properly (by numerical values)
add_action('pre_get_posts', 'shop_order_column_meta_field_sortable_orderby' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
    global $pagenow;

    // Targeting WooCommerce admin orders list only
    if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ){

        $orderby  = $query->get( 'orderby');
        $meta_key = '_shipping_address_1';

        if ('_shipping_address_1' === $orderby){
          $query->set('meta_key', $meta_key);
          $query->set('orderby', 'meta_value_num'); // Or using 'meta_value'
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

huangapple
  • 本文由 发表于 2023年6月8日 01:31:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定