I need to search for customers using their phone details when creating a manual order in WooCommerce. I’ve tried:
add_filter( 'woocommerce_shop_order_search_fields', 'custom_billing_phone_search_fields', 10, 1 );
function custom_billing_phone_search_fields( $meta_keys ){
$meta_keys[] = '_billing_phone';
return $meta_keys;
}
jQuery(document).ready(function($) {
// Enhance customer search to include phone numbers
$(document).on('select2:open', '.wc-customer-search', function() {
// Add additional search criteria for phone numbers
$('.select2-search__field').attr('placeholder', 'Search by name or phone number');
});
});
jQuery(document).ready(function($) {
// Modify the customer search box to include phone numbers
$(document).on('select2:open', '.wc-customer-search', function() {
// Set a custom template for the select2 dropdown
$('.select2-dropdown').html('<input type="text" class="select2-search__field" placeholder="Search by name or phone number">');
// Retrieve all customers
var customers = $('.select2-results__option');
// Filter the customers to include phone numbers in the search
$('.select2-search__field').on('keyup', function() {
var searchKeyword = $(this).val().toLowerCase().trim();
customers.each(function() {
var customerName = $(this).text().toLowerCase();
var customerPhone = $(this).data('phone').toLowerCase();
if (customerName.includes(searchKeyword) || customerPhone.includes(searchKeyword)) {
$(this).show();
} else {
$(this).hide();
}
});
});
});
});
And
/**
* Customize customer search in WooCommerce manual order creation.
*/
function custom_woocommerce_customer_search_by_phone( $search_fields ) {
// Add 'billing_phone' field to the search fields array.
$search_fields[] = 'billing_phone';
$search_fields[] = 'shipping_phone';
return $search_fields;
}
add_filter( 'woocommerce_admin_order_data_after_billing_address', 'custom_woocommerce_customer_search_by_phone' );
But none of these allow me to search for the phone number , just the usernanme, email or name pops up. Please assist
Tried all of the above code snippets but all returned no result
New contributor
user24640143 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.