我想覆盖位于search_orders()
的Woocommerce WC_Order_Data_Store_CPT
Class中的includes/data-stores/class-wc-order-data-store-cpt.php
函数
这是我需要覆盖的函数源代码:
/**
* Search order data for a term and return ids.
*
* @param string $term
* @return array of ids
*/
public function search_orders( $term ) {
global $wpdb;
/**
* Searches on meta data can be slow - this lets you choose what fields to search.
* 3.0.0 added _billing_address and _shipping_address meta which contains all address data to make this faster.
* This however won't work on older orders unless updated, so search a few others (expand this using the filter if needed).
* @var array
*/
$term = preg_replace('/[^0-9.]+/', '', $term); // <=== <=== <=== HERE
$search_fields = array_map( 'wc_clean', apply_filters( 'woocommerce_shop_order_search_fields', array(
'_billing_address_index',
'_shipping_address_index',
'_billing_last_name',
'_billing_email',
) ) );
$order_ids = array();
if ( is_numeric( $term ) ) {
$order_ids[] = absint( $term );
}
if ( ! empty( $search_fields ) ) {
$order_ids = array_unique( array_merge(
$order_ids,
$wpdb->get_col(
$wpdb->prepare( "SELECT DISTINCT p1.post_id FROM {$wpdb->postmeta} p1 WHERE p1.meta_value LIKE '%%%s%%'", $wpdb->esc_like( wc_clean( $term ) ) ) . " AND p1.meta_key IN ('" . implode( "','", array_map( 'esc_sql', $search_fields ) ) . "')"
),
$wpdb->get_col(
$wpdb->prepare( "
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
WHERE order_item_name LIKE '%%%s%%'
",
$wpdb->esc_like( wc_clean( $term ) )
)
)
) );
}
return apply_filters( 'woocommerce_shop_order_search_results', $order_ids, $term, $search_fields );
}
我只想添加这个添加函数的开头
$term = preg_replace('/[^0-9.]+/', '', $term);
欢迎任何帮助。
您可以使用位于woocommerce_shop_order_search_results
的可用end of the function过滤器钩子,它将允许覆盖该方法:
add_filter( 'woocommerce_shop_order_search_results', 'custom_shop_order_search_results_filter', 10, 3 );
function custom_shop_order_search_results_filter( $order_ids, $term, $search_fields ) {
global $wpdb;
$term = preg_replace('/[^0-9.]+/', '', $term); // <=== <=== <=== Your change
$order_ids = array();
if ( is_numeric( $term ) ) {
$order_ids[] = absint( $term );
}
if ( ! empty( $search_fields ) ) {
$order_ids = array_unique(
array_merge(
$order_ids,
$wpdb->get_col(
$wpdb->prepare(
"SELECT DISTINCT p1.post_id FROM {$wpdb->postmeta} p1 WHERE p1.meta_value LIKE %s AND p1.meta_key IN ('" . implode( "','", array_map( 'esc_sql', $search_fields ) ) . "')", // @codingStandardsIgnoreLine
'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
)
),
$wpdb->get_col(
$wpdb->prepare(
"SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
WHERE order_item_name LIKE %s",
'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
)
)
)
);
}
return $order_ids
}
代码位于活动子主题(或活动主题)的function.php文件中。经过测试,应该可行。