WooCommerce: How to Add Scripts to the Checkout Page

A client needed to add her SSL Logo Seal to the checkout page. The problem is that the code she was given had also a JS part, together with a bunch of HTML.

Unfortunately you can’t just copy and paste JavaScript in the checkout page… you need a workaround!

In this article, we will learn about wp_footer, a handy WordPress hook to print anything inside the footer, a bit of conditional logic to target the WooCommerce checkout page only (and therefore excluding the Thank You Page and Order Pay Page), and how to print HTML inside a PHP function. Enjoy!

WooCommerce: how to add HTML/JS scripts to the checkout page

PHP Snippet: Add Script @ Checkout Page  – WooCommerce

/**
* @snippet       Print Script @ Checkout Footer - WooCommerce
* @how-to        Get CustomizeWoo.com FREE
* @author        Rodolfo Melogli
* @testedwith    WooCommerce 5
* @community     https://businessbloomer.com/club/
*/

add_action( 'wp_footer', 'bbloomer_add_jscript_checkout', 9999 );

function bbloomer_add_jscript_checkout() {
   global $wp;
	if ( is_checkout() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ) {
      echo '<script>paste your script here!</script>';
   }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]
  • WooCommerce: Disable Payment Gateway by Country
    You might want to disable PayPal for non-local customers or enable a specific gateway for only one country… Either way, this is a very common requirement for all of those who trade internationally. Here’s a simple snippet you can further customize to achieve your objective. Simply pick the payment gateway “slug” you want to disable/enable […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

6 thoughts on “WooCommerce: How to Add Scripts to the Checkout Page

  1. How much trouble is it to have your web designer add “code” (and have a number or word or numbers and letters)
    to your check out page. I need the businesses working with me to have a code so that when a customer buys from
    them, it will tell them that the customer came to them through my web page.

    1. Hello Ellie, thanks for your comment! I recommend you take a look at this tutorial: https://businessbloomer.com/woocommerce-add-custom-checkout-field-php/

  2. Thanks alot for this snippet . However, should this be added in the functions.php?

    1. Vishay, yes, positive ๐Ÿ™‚ Place it in your current theme’s functions.php file. Tutorial here:

      how-to-edit-woocommerce-with-php-snippets

  3. Thanks this was really helpful. I have my own user-registration form added to this checkout (by copying and overriding the form-checkout.php WC template. I needed to copy over the email address from one field to another, this was perfect for that, thanks!

    1. Thanks for your feedback Gerry!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *