• Resolved sorro445

    (@sorro445)


    Hi,
    I am using your plugin along with Woocommerce and Collapseomatic(an accordion plugin) without any problems. However, therer is one issue I have: Iam adding an accordion into the cart and checkout table, and the collapseomatic author told me to add this function to my child theme

    add_action('wp_footer', 'my_footer_javascript');
    function my_footer_javascript() {
        ?>
            <script>
              jQuery( document ).on( 'updated_wc_div init_checkout updated_checkout', function() {
          collapse_init();
    } );
            </script>
        <?php
    }
    

    so that the accordion on the checkout page starts CLOSED when opening the page, not open. When I now defer js in the settings, the accordion starts OPEN when opening the page though…is there anyway to defer jquery but still have the accordion be closed by default? Or alternatively only not defer this one particular jquery script?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Raul P.

    (@alignak)

    If you do that, you are automatically requiring jQuery to be render blocking on your theme, so there’s no way to defer jQuery as it would load “too late” after that code, and trigger an undefined error.

    Instead of that, you can put that javascript code on an external javascript file and enqueue it as a dependency to your theme js file.
    https://developer.ww.wp.xz.cn/reference/functions/wp_enqueue_script/

    That way, FVM will merge it together in order, thus it won’t need render blocking.

    I suggest hiring a developer, or looking for some tutorials on google on how to add custom js files to wordpress.

    You can also edit the relevant js file on your theme and add the code between <script> to it.

    Thread Starter sorro445

    (@sorro445)

    Hi,
    thanks for your reply. So now I have tried removing the function and instead adding the script part directly to the footer.php.

    Still, the accordion in the checkout starts open by default (strangely the accordion on the cart page starts closed, as it should though, even when adding the script with the function as before).

    Plugin Author Raul P.

    (@alignak)

    You cannot add code directly to the footer, that’s not what I said.
    What I said, is that the javascript code, not the whole thing, should go into an external file properly enqueued (never directly on the footer).

    If you are adding it to the footer, it will show up on the exact same place a with that code.
    Deferring javascript, means it will load after the footer… that’s why, you cannot add it to the html, but rather as an external js file dependent on the theme files.

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

The topic ‘Accordion jquery’ is closed to new replies.