Last updated on September 25, 2023

WooCommerce checkout not working

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Fix issues with WooCommerce checkout for seamless transactions.

The WooCommerce checkout process is a critical part of any online store, as it allows customers to complete their purchases. If the checkout is not working properly, it can lead to frustrated customers and lost sales. Here’s a code snippet that can help you troubleshoot and fix common issues with the WooCommerce checkout.

/**
 * Disable AJAX on the WooCommerce checkout page.
 */
function wpsnippets_disable_checkout_ajax() {
    if ( is_checkout() ) {
        wp_dequeue_script( 'wc-checkout' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpsnippets_disable_checkout_ajax' );

This code snippet disables the AJAX functionality on the WooCommerce checkout page. Sometimes, conflicts with other plugins or themes can cause issues with the AJAX calls, resulting in a non-functional checkout. By disabling AJAX, the checkout page will reload fully with each step, which can help identify and resolve any issues.

To use this code snippet, you can add it to your theme’s functions.php file or create a custom plugin. After adding the code, visit the checkout page of your WooCommerce store and see if the issue persists. If the checkout starts working correctly, it indicates that there might be a conflict with AJAX.

Remember to always test your changes on a staging or development site before implementing them on a live site.

Examples

Example #1: Troubleshooting WooCommerce Checkout Issues

This example demonstrates how to troubleshoot and fix common issues with the WooCommerce checkout process.

/**
 * Disable all non-WooCommerce scripts and styles on the checkout page.
 */
function wpsnippets_disable_non_wc_scripts_styles() {
    if ( function_exists( 'is_checkout' ) && is_checkout() ) {
        wp_dequeue_script( 'jquery' );
        wp_dequeue_style( 'wp-block-library' );
        // Add more scripts and styles to dequeue if necessary.
    }
}
add_action( 'wp_enqueue_scripts', 'wpsnippets_disable_non_wc_scripts_styles', 999 );

This code snippet disables all non-WooCommerce scripts and styles on the checkout page, ensuring that only necessary assets are loaded. It uses the wp_dequeue_script() and wp_dequeue_style() functions to remove unwanted scripts and styles. You can add more scripts and styles to dequeue if needed.

Example #2: Clearing WooCommerce Checkout Session

This example demonstrates how to clear the WooCommerce checkout session to resolve issues related to persistent data.

/**
 * Clear WooCommerce checkout session.
 */
function wpsnippets_clear_wc_checkout_session() {
    if ( function_exists( 'WC' ) && WC()->session ) {
        WC()->session->set( 'cart', array() );
        WC()->session->set( 'order_awaiting_payment', false );
        WC()->session->set( 'order_created', false );
        WC()->session->set( 'order_id', false );
        WC()->session->set( 'order_awaiting_payment', false );
        WC()->session->set( 'order_key', false );
    }
}
add_action( 'woocommerce_before_checkout_process', 'wpsnippets_clear_wc_checkout_session' );

This code snippet clears the WooCommerce checkout session when the checkout process begins. It uses the WC()->session->set() function to reset various session variables related to the cart and order. This can help resolve issues where incorrect or stale data is causing problems during checkout.

Example #3: Disabling WooCommerce Checkout Fields

This example demonstrates how to disable specific fields on the WooCommerce checkout page.

/**
 * Disable specific fields on the WooCommerce checkout page.
 */
function wpsnippets_disable_wc_checkout_fields( $fields ) {
    unset( $fields['billing']['billing_company'] );
    unset( $fields['shipping']['shipping_company'] );
    // Add more fields to disable if necessary.
    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'wpsnippets_disable_wc_checkout_fields' );

This code snippet disables the “Company” fields on both the billing and shipping sections of the WooCommerce checkout page. It uses the woocommerce_checkout_fields filter to modify the $fields array and remove the unwanted fields. You can add more fields to disable by specifying their keys in the $fields array.

Last updated on September 25, 2023. Originally posted on October 2, 2023.

Leave a Reply

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