gpt4 book ai didi

php - 在 Woocommerce Admin Orders 列表中添加可排序的自定义列

转载 作者:行者123 更新时间:2023-12-04 16:06:14 25 4
gpt4 key购买 nike

我在 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 );
}

最佳答案

2020 年更新:
你也可以试试这个,让它可以排序和搜索:

// Add a custom column
add_filter( 'manage_edit-shop_order_columns', 'add_custom_shop_order_column' );
function add_custom_shop_order_column( $columns ) {
$order_actions = $columns['order_actions'];
unset($columns[ 'order_actions' ]);

// add custom column
$columns['postcode'] = __('Zip Code', 'woocommerce');

// Insert back 'order_actions' column
$columns['order_actions'] = $order_actions;

return $columns;
}

// Custom column content
add_action( 'manage_shop_order_posts_custom_column', 'shop_order_column_meta_field_value' );
function shop_order_column_meta_field_value( $column ) {
global $post, $the_order;

if ( ! is_a( $the_order, 'WC_Order' ) ) {
$the_order = wc_get_order( $post->ID );
}

if ( $column == 'postcode' ) {
echo $the_order->get_shipping_postcode();
}
}

// Make custom column sortable
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_postcode';
return wp_parse_args( array('postcode' => $meta_key), $columns );
}

// Make sorting work 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;

if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ){

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

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

// Make metakey searchable in the shop orders list
add_filter( 'woocommerce_shop_order_search_fields', 'shipping_postcode_searchable_field' );
function shipping_postcode_searchable_field( $meta_keys ){
$meta_keys[] = '_shipping_postcode';
return $meta_keys;
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。

关于php - 在 Woocommerce Admin Orders 列表中添加可排序的自定义列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48908754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com