• Resolved Paul Vek

    (@paulvek)


    Here is my WooCommerce general setting page

    I choose only Iran country. Therefore, the prefix code should no longer be shown…

    Image1 (please right-click and open new tab)

    in the checkout field, there is a problem. WooCommerce automatically adds country code prefixes sometimes +98 and sometimes 98! however, In both cases, this is wrong, and should not be added to the beginning of the number.

    Image2 (please right-click and open new tab)

    Image3 (please right-click and open new tab)

    No one in his own country uses the country code to call! This only makes sense when a buyer wants to buy from another country, or when you want to contact someone in another country!

    Most people set up their stores to sell their products only in their country or region. And not everyone has a global store like Amazon;! And since it is set and selected in the settings of a specific country for the store, sales to other countries cannot be done; In all cases, the prefix of the country code should not be added.

    It just confuses the buyer and makes the field ugly and crowded. In many countries, even people do not know what their country code prefix is! Because they only deal with the mobile number itself.

    Even in our country, operators made +98 or 98 hidden from the beginning of the number, and no international prefix is known during incoming and outgoing calls. Some mobile phone manufacturers in Asia, such as Samsung, hide the international prefix for certain countries so that it is not even displayed.

    I know that this problem can be solved by JavaScript and jQuery, but not everyone knows that! Also, adding additional JavaScript and jQuery codes can cause page loading to be slower, page loading speed to decrease, and also some other problems. And it is better to solve this problem in WooCommerce itself. so that there is no need for any additional work and other uncertain methods.

    Thank you for your time. Please solve this problem soon.

Viewing 8 replies - 1 through 8 (of 8 total)
  • ChatGPT:

    Temporary JavaScript/JQuery Solution: While waiting for an official fix, you can consider using JavaScript or jQuery to remove the country code prefix on the client side. You can add this code to your theme’s footer or use a custom JavaScript plugin:

    jQuery(document).ready(function($) {
        // Replace 'billing_phone' with the actual ID or class of the phone input field
        $('#billing_phone').on('input', function() {
            var phoneNumber = $(this).val();
            // Remove country code prefix (e.g., +98 or 98)
            phoneNumber = phoneNumber.replace(/^\+98|^98/, '');
            // Update the input field with the modified number
            $(this).val(phoneNumber);
        });
    });
    

    Human – me:

    References for more knowledge:

    Similar problems:

    • This reply was modified 10 months, 1 week ago by Rok Megli?.
    Thread Starter Paul Vek

    (@paulvek)

    @rokmeglic Thank you very much for your reply. But this is not the right way to do it.
    If the internet speed is low, or if the page loads slowly for any reason, the prefix will be removed and reappeared in front of the visitor’s eyes!

    Even if none of the things I said are true, but still, the PHP method might still be something like a hook, or even better, meaning that this problem is solved internally, and in WooCommerce itself, it will perform better than all other methods. had And no additional action or code is required. And the problem of non-professionals will be solved.

    • This reply was modified 10 months, 1 week ago by Paul Vek. Reason: place an image
    • This reply was modified 10 months, 1 week ago by Paul Vek.

    are you using plugins for editing checkout page fields? like “Flexible Checkout Page” or “Checkout fields editor”

    Thread Starter Paul Vek

    (@paulvek)

    @daneshh No, I do not use any plugins. These items have been fully tested by a WordPress and WooCommerce raw, the problem is from WooCommerce. It is not a matter of interference or using another plugin.

    I encountered a particular issue which was resolved by removing the checkout editor plugin. Additionally, I made modifications to the fields in the functions.php file of the child-theme.

    Also It appears that the images are either not being displayed or have not been uploaded. Therefore, I kindly request you to edit your post or reply by including the images. This will assist others in providing more effective assistance.

    Thread Starter Paul Vek

    (@paulvek)

    @daneshh As I said I do not use any plugins. These items have been fully tested by WordPress and WooCommerce raw, the problem is from WooCommerce. It is not a matter of interference or using another plugin.

    All the images are uploaded correctly and load well. You must use a VPN (if the Imgur site is filtered in your country) then you can see the images easily.

    ChatGPT.

    Certainly! If you prefer a PHP-based solution using hooks in WordPress, you can create a custom function in your theme’s functions.php file to modify the behavior of the billing phone field. Here’s an example:

    function remove_country_code_prefix( $fields ) {
        // Replace 'billing_phone' with the actual field name of the phone input in your WooCommerce checkout
        $billing_phone_field = 'billing_phone';
    
        // Check if the billing phone field is present and has a value
        if ( isset( $fields[$billing_phone_field] ) && ! empty( $fields[$billing_phone_field]['value'] ) ) {
            $phone_number = $fields[$billing_phone_field]['value'];
    
            // Remove country code prefix (e.g., +98 or 98)
            $phone_number = preg_replace( '/^\+?98/', '', $phone_number );
    
            // Update the billing phone field value
            $fields[$billing_phone_field]['value'] = $phone_number;
        }
    
        return $fields;
    }
    
    add_filter( 'woocommerce_checkout_fields', 'remove_country_code_prefix' );
    

    Explanation:

    1. Replace 'billing_phone' with the actual field name of the phone input in your WooCommerce checkout. You can inspect the HTML source of your checkout page to find the correct field name.
    2. The remove_country_code_prefix function hooks into the woocommerce_checkout_fields filter. This filter allows you to modify the array of checkout fields.
    3. It checks if the billing phone field is present and has a value. If so, it removes the country code prefix from the phone number.
    4. The add_filter function is used to add the custom function to the woocommerce_checkout_fields filter.

    After adding this code to your theme’s functions.php file, the country code prefix should be removed from the billing phone field during the checkout process. Make sure to test it thoroughly to ensure it works as expected with your specific setup.

    Thread Starter Paul Vek

    (@paulvek)

    Thank you sir for your kindness @rokmeglic

    but this number not just only billing phone feild

    It exists in other places like billing, order review, order edit, users, factors (i mean invoices), emails and more

    That’s why I say it should be solved by the programmers and developers of WooCommerce itself. Because this is being produced and called from the source. It is better to stop there (change).

    Here, we have only changed the billing phone on the payment page and written a code for it. This can also affect the performance of the site, even if it is PHP. Because these terms cancel the default and previous terms of WooCommerce. A conflict is taking place to win our code!

    But as I gave an example. It still remains in many places. Maybe many places remain hidden from our view and they also need to change. If we want to write a code for each of those places, it requires expertise and a lot of time. It is not suitable for people who are not experts.

    Thank you very much for your help. Your codes worked (of course the first code didn’t work) although I was able to fix it myself.

    But AI codes are often not good enough either. They have flaws. Or they are not optimal and should be changed. You must be a programmer and check because sometimes they may even damage the database. And it requires caution and knowledge. Otherwise, anyone can generate an answer with artificial intelligence and use it.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘I found a big problem in the billing phone field (auto-add country code prefix)’ is closed to new replies.