代码之家  ›  专栏  ›  技术社区  ›  user24640143

添加在WooCommerce手动订单页面中搜索电话号码的功能

  •  -1
  • user24640143  · 技术社区  · 1 年前

    在WooCommerce中创建手动订单时,我需要使用他们的电话详细信息来搜索客户。我尝试过:

    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();
                    }
                });
            });
        });
    });
    
    

    /**
     * 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' );
    
    

    但这些都不允许我搜索电话号码,只会弹出用户名、电子邮件或姓名。请协助

    尝试了上述所有代码段,但均未返回结果

    0 回复  |  直到 1 年前