有谁能告诉我,我怎么能在woo-commerce订单页面中添加按公司名称设置过滤器。
并请分享功能或显示我的错误,所以我可以解决它。
我试过了,但不工作。你的帮助是非常感激的。
add_action( 'restrict_manage_posts', 'admin_shop_order_by_product_type_filter' );
function admin_shop_order_by_product_type_filter(){
global $pagenow, $post_type;
if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {
$domain = 'woocommerce';
$filter_id = 'filter_billing_company';
$current = isset($_GET[$filter_id])? $_GET[$filter_id] : '';
$query_args = ['fields' => '_billing_company', 'orderby' => 'order'];
echo "<pre>";print_r(get_terms($query_args)); echo "</pre>";
echo '<select name="'.$filter_id.'">
<option value="">' . __('Filter by Company', $domain) . '</option>';
foreach ( get_terms($query_args) as $term_name ) {
printf( '<option value="%s"%s>%s</option>', $term_name,
$term_name === $current ? '" selected="selected"' : '', ucfirst($term_name) );
}
echo '</select>';
}
}
你的帮助,非常感谢。
要在管理订单列表中通过元字段过滤订单,你将使用以下方法: 1.(您将在下面第一个函数中定义metakey标签对,以过滤订单):
// Custom function where metakeys / labels pairs are defined
function get_filter_shop_order_meta( $domain = 'woocommerce' ){
// Add bellow the metakey / label pairs to filter orders
return [
'_billing_company' => __('Billing company', $domain),
'_order_total' => __('Gran total', $domain),
];
}
// Add a dropdown to filter orders by meta
add_action( 'restrict_manage_posts', 'display_admin_shop_order_by_meta_filter' );
function display_admin_shop_order_by_meta_filter(){
global $pagenow, $typenow;
if( 'shop_order' === $typenow && 'edit.php' === $pagenow ) {
$domain = 'woocommerce';
$filter_id = 'filter_shop_order_by_meta';
$current = isset($_GET[$filter_id])? $_GET[$filter_id] : '';
echo '<select name="'.$filter_id.'">
<option value="">' . __('Filter by meta…', $domain) . '</option>';
$options = get_filter_shop_order_meta( $domain );
foreach ( $options as $key => $label ) {
printf( '<option value="%s"%s>%s</option>', $key,
$key === $current ? '" selected="selected"' : '', $label );
}
echo '</select>';
}
}
// Process the filter dropdown for orders by Marketing optin
add_filter( 'request', 'process_admin_shop_order_marketing_by_meta', 99 );
function process_admin_shop_order_marketing_by_meta( $vars ) {
global $pagenow, $typenow;
$filter_id = 'filter_shop_order_by_meta';
if ( $pagenow == 'edit.php' && 'shop_order' === $typenow
&& isset( $_GET[$filter_id] ) && ! empty($_GET[$filter_id]) ) {
$vars['meta_key'] = $_GET[$filter_id];
$vars['orderby'] = 'meta_value';
}
return $vars;
}
// (Optional) Make a custom meta field searchable from the admin order list search field
add_filter( 'woocommerce_shop_order_search_fields', 'shop_order_meta_search_fields', 10, 1 );
function shop_order_meta_search_fields( $meta_keys ){
foreach ( get_filter_shop_order_meta() as $meta_key => $label ) {
$meta_keys[] = $meta_key;
}
return $meta_keys;
}
代码在您的活动子主题(或活动主题)的function.php文件中。经过测试,工作。