Site icon Rashed Hossain

How to Add a Sticky “Proceed to Checkout” Button in WooCommerce

How to Add a Sticky “Proceed to Checkout” Button in WooCommerce

One of the best ways to enhance the user experience on WooCommerce websites is by providing easy access to important actions. In this tutorial, we’ll walk you through adding a sticky button on your WooCommerce cart and checkout pages. This button will follow users as they scroll, offering them quick access to the next step in their shopping journey—whether it’s proceeding to checkout or placing the order.

Step 1: Adding the Sticky Button to the Footer

The core functionality of this feature is achieved by using the wp_footer action hook, which ensures that the sticky button is added at the bottom of the page.

add_action('wp_footer', 'scb_add_sticky_button');

function scb_add_sticky_button() {
    if (!is_cart() && !is_checkout()) return;

    ?>
    <style>
        .scb-sticky-btn {
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            background: #2c7be5;
            color: #fff;
            text-align: center;
            padding: 15px;
            font-size: 18px;
            z-index: 9999;
            box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.15);
        }
        .scb-sticky-btn:hover {
            background: #1a68d1;
        }
        .scb-sticky-btn a,
        .scb-sticky-btn button {
            color: #fff;
            text-decoration: none;
            display: block;
        }
    </style>
    <?php
}

Explanation:

Step 2: Display the Button on the Cart Page

On the cart page, we want to provide the user with a link to proceed to the checkout. We achieve this by adding a simple anchor tag inside the sticky button.

<div class="scb-sticky-btn">
    <?php if (is_cart()) : ?>
        <a href="<?php echo esc_url(wc_get_checkout_url()); ?>">Proceed to Checkout</a>
    <?php endif; ?>
</div>

Explanation:

Step 3: Display the Button on the Checkout Page

For the checkout page, we want to add a sticky “Place Order” button that’s styled similarly to the existing “Place Order” button on the page. To achieve this, we use JavaScript to clone the existing button, adjust its styling, and append it to the sticky container.

<?php elseif (is_checkout()) : ?>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const placeOrderBtn = document.querySelector('#place_order');
            if (placeOrderBtn) {
                const clonedBtn = placeOrderBtn.cloneNode(true);
                clonedBtn.style.width = '100%';
                clonedBtn.style.background = 'none';
                clonedBtn.style.border = 'none';
                clonedBtn.style.fontSize = '18px';
                clonedBtn.style.padding = '0';
                clonedBtn.id = 'sticky_place_order';

                clonedBtn.addEventListener('click', function (e) {
                    e.preventDefault();
                    placeOrderBtn.click();
                });

                document.querySelector('.scb-sticky-btn').appendChild(clonedBtn);
            }
        });
    </script>
<?php endif; ?>

Explanation:

Step 4: Testing and Implementation

Once the code is in place, you should see a sticky button appear at the bottom of your cart and checkout pages:

Conclusion

This sticky button feature is a great way to ensure that your customers always have quick access to critical actions, such as proceeding to checkout or placing an order. It improves the shopping experience by making navigation seamless, especially on mobile devices, where screen real estate is limited.

Exit mobile version