为 WooCommerce 订单搜索创建过滤器
Make a filter for WooCommerce Order Search
在 WP 中,我有一个包含大约 35k 个订单的 WooCommerce 订单数据库。 wp_postmeta table 大约有 200 万条记录,因为所有订单数据都存储为元数据。因此,订单搜索非常慢。
我将默认的 wc 订单搜索更改为仅使用此代码段搜索特定的元数据字段:
function custom_woocommerce_shop_order_search_fields( $search_fields ) {
// error_log( 'currently searching in these order fields: ' . print_r($search_fields, true) );
unset( $search_fields );
$search_fields[] = '_order_key';
$search_fields[] = '_billing_company';
$search_fields[] = 'serialnumbers';
$search_fields[] = 'information';
// error_log( 'now only searching in these order fields: ' . print_r($search_fields, true) );
return $search_fields;
}
add_filter( 'woocommerce_shop_order_search_fields', 'custom_woocommerce_shop_order_search_fields' );
所以wc顺序搜索现在只搜索这4种类型的元数据,但由于其中一些比较大,速度还是很慢。
我想使用下拉过滤器 'orderkey, company, serials, info' 扩展 wc 订单搜索,以便搜索仅搜索下拉列表中 selected 的元数据键。因此,如果我在下拉列表中 select 'serialnumbers',wc 订单搜索将仅在元数据序列号中查找我的搜索项,而不会在其他序列号中查找。不仅是速度,还有可用性。
我不是真正的程序员,所以非常感谢帮助!
感谢您的帮助!在 Filter orders by specific meta fields in WooCommerce admin orders list 中解决了类似的问题。制作了自定义过滤器以按元键过滤订单。最后(可选)部分是将过滤器中的所有元键添加到搜索查询中。
我将最后一部分更改为下面的代码片段。我现在寻找选定的值,如果已设置,我取消设置顺序搜索,然后仅从下拉列表中添加选定的元键。在下面的情况下,搜索仅搜索 1 个选定的元键。如果未选择任何内容,它将使用默认搜索。
// Make only the selected 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 ){
$filter_id = 'filter_shop_order_by_meta';
if ( isset( $_GET[$filter_id] ) && ! empty($_GET[$filter_id]) ) {
unset( $meta_keys );
$meta_keys[] = $_GET[$filter_id];
}
return $meta_keys;
}
注意:上面的代码片段只能与其他主题中的其余代码结合使用,以制作此代码片段中使用的过滤器。
在 WP 中,我有一个包含大约 35k 个订单的 WooCommerce 订单数据库。 wp_postmeta table 大约有 200 万条记录,因为所有订单数据都存储为元数据。因此,订单搜索非常慢。 我将默认的 wc 订单搜索更改为仅使用此代码段搜索特定的元数据字段:
function custom_woocommerce_shop_order_search_fields( $search_fields ) {
// error_log( 'currently searching in these order fields: ' . print_r($search_fields, true) );
unset( $search_fields );
$search_fields[] = '_order_key';
$search_fields[] = '_billing_company';
$search_fields[] = 'serialnumbers';
$search_fields[] = 'information';
// error_log( 'now only searching in these order fields: ' . print_r($search_fields, true) );
return $search_fields;
}
add_filter( 'woocommerce_shop_order_search_fields', 'custom_woocommerce_shop_order_search_fields' );
所以wc顺序搜索现在只搜索这4种类型的元数据,但由于其中一些比较大,速度还是很慢。
我想使用下拉过滤器 'orderkey, company, serials, info' 扩展 wc 订单搜索,以便搜索仅搜索下拉列表中 selected 的元数据键。因此,如果我在下拉列表中 select 'serialnumbers',wc 订单搜索将仅在元数据序列号中查找我的搜索项,而不会在其他序列号中查找。不仅是速度,还有可用性。
我不是真正的程序员,所以非常感谢帮助!
感谢您的帮助!在 Filter orders by specific meta fields in WooCommerce admin orders list 中解决了类似的问题。制作了自定义过滤器以按元键过滤订单。最后(可选)部分是将过滤器中的所有元键添加到搜索查询中。 我将最后一部分更改为下面的代码片段。我现在寻找选定的值,如果已设置,我取消设置顺序搜索,然后仅从下拉列表中添加选定的元键。在下面的情况下,搜索仅搜索 1 个选定的元键。如果未选择任何内容,它将使用默认搜索。
// Make only the selected 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 ){
$filter_id = 'filter_shop_order_by_meta';
if ( isset( $_GET[$filter_id] ) && ! empty($_GET[$filter_id]) ) {
unset( $meta_keys );
$meta_keys[] = $_GET[$filter_id];
}
return $meta_keys;
}
注意:上面的代码片段只能与其他主题中的其余代码结合使用,以制作此代码片段中使用的过滤器。