• Resolved nicetig

    (@nicetig)


    I’ve created only 1 shipping method with 1 flat rate option. how do i make it auto checked on my checkout page as it won’t allow me to select / click on the radio button.
    screenshot at https://imgur.com/a/2ZqratB

    Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @nicetig

    add_action( 'woocommerce_before_checkout_form', 'ahir_select_free_shipping_by_default' );
    function ahir_select_free_shipping_by_default() {
        if ( isset(WC()->session) && ! WC()->session->has_session() )
            WC()->session->set_customer_session_cookie( true );
    
        // Check if "free shipping" is already set
        if ( strpos( WC()->session->get('chosen_shipping_methods')[0], 'free_shipping' ) !== false )
            return;
    
        // Loop through shipping methods
        foreach( WC()->session->get('shipping_for_package_0')['rates'] as $key => $rate ){
            if( $rate->method_id === 'free_shipping' ){
                // Set "Free shipping" method
                WC()->session->set( 'chosen_shipping_methods', array($rate->id) );
                return;
            }
        }
    }

    Code goes in function.php file of your active child theme (or active theme). tested and works.

    Please change code based on your shipping method name.

    Thanks
    Ahir Hemant

    Thread Starter nicetig

    (@nicetig)

    thanks Ahir,

    I’ve changed ‘free_shipping’ to ‘flat_rate1’ but still not able to get it to work?

    add_action( 'woocommerce_before_checkout_form', 'ahir_select_free_shipping_by_default' );
    function ahir_select_free_shipping_by_default() {
        if ( isset(WC()->session) && ! WC()->session->has_session() )
            WC()->session->set_customer_session_cookie( true );
    
        // Check if "free shipping" is already set
        if ( strpos( WC()->session->get('chosen_shipping_methods')[0], 'flat_rate1' ) !== false )
            return;
    
        // Loop through shipping methods
        foreach( WC()->session->get('shipping_for_package_0')['rates'] as $key => $rate ){
            if( $rate->method_id === 'flat_rate1' ){
                // Set "Free shipping" method
                WC()->session->set( 'chosen_shipping_methods', array($rate->id) );
                return;
            }
        }
    }

    `

    Hello there,

    Can you please share with us the website URL when the issue is present?

    Also, please provide screenshots of shipping zones, their methods, and shipping rates so that we can check this further for you much better.

    For screenshots you can use https://snipboard.io – please follow the instructions on the page, then paste the URL in this chat. It works with Chrome, Firefox, Safari, and Edge.

    Thread Starter nicetig

    (@nicetig)

    Hello @nicetig ,

    Thanks for the link to your site.

    I can see that the “Flat-rate” shipping method can’t be selected.

    Have you tried with a default theme like Storefront to see if the problem is with your current theme?

    You can try after enabling troubleshoot mode using the Health Check: https://wordpress.org/plugins/health-check/ plugin to safely enable troubleshooting mode and deactivate plugins, themes without affecting how your site is available to the visitors. See Troubleshooting using Health Check: https://docs.woocommerce.com/document/troubleshooting-using-health-check/ for more details.

    For more detailed instructions on conflict test, check out How to test for plugin and theme conflicts: https://docs.woocommerce.com/document/how-to-test-for-conflicts/

    Let us know how the test goes.

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    It’s been a while since we last heard back from you. I’m going to mark this thread as resolved.

    Hopefully, you were able to find a way forward with the issue – we’ll be here if and/or when you are ready to continue.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Default shipping method checked’ is closed to new replies.