Create custom 'Add to cart' with wordpress woocommerce

Create custom 'Add to cart' with wordpress woocommerce

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Building Custom 'Add to Cart' Functionality Outside WooCommerce Templates

When you need to inject an "Add to Cart" button into a page that isn't managed by WooCommerce's standard template hierarchy, the approach differs slightly from simply dropping a shortcode or a block on the page. The core mechanism relies on WooCommerce's AJAX handling and specific URL parameters, but understanding why your current implementation fails is key to getting it right.

The HTML snippet you observed represents a direct link to the cart page with query arguments telling WooCommerce which item to add. However, relying solely on a standard <a> tag often leads to user experience issues, such as losing focus or triggering multiple cart refreshes. The modern best practice for this scenario is leveraging the wc_add_to_cart() function or utilizing AJAX via a custom action hook, which allows for smoother interactions and better error handling.

Implementing the Button Logic

If you prefer sticking to the link-based approach you attempted, there are two critical details missing in your code that prevent the cart from updating. First, the add-to-cart parameter must match the specific product ID exactly. In your second example, you correctly echo the $id, but ensure this variable holds the actual product ID and not a generic placeholder.

Secondly, simply changing the link text or image doesn't guarantee the cart updates if the server-side logic isn't triggered. WooCommerce expects a POST request for standard cart additions to handle quantities and validation properly. If you are using a simple GET link (like <a href="...">), it might add the item but fail to update the session correctly, or worse, trigger a redirect that clears your current page state.

To fix this without overhauling your entire theme, you should consider wrapping your button in a script that triggers WooCommerce's built-in AJAX handler. This ensures that when the user clicks the button:

  1. The server validates the product exists and is purchasable.
  2. The cart session updates correctly.
  3. A minimal amount of page content reloads (or none at all, depending on your setup), providing a seamless experience.

Handling the "View Cart" Toast Notification

You mentioned trying to remove or redirect the "View Cart" button that appears near the item after adding it. This notification is typically generated by WooCommerce's AJAX response or via the woocommerce_add_to_cart_message_html hook.

If your goal is to suppress this message entirely, you can do so programmatically by unhooking the default action or by filtering the return value of the message generation function. If you want to redirect the user instead of just showing a toast, you need to intercept the AJAX response. This usually involves adding a custom JavaScript snippet that listens for the added_to_cart event (fired by WooCommerce) and then executes your desired redirection logic, such as:

jQuery( document.body ).on( 'added_to_cart', function( event, cart_hash ) {
    // Redirect to cart or thank you page instead of showing the toast
    window.location.href = '/checkout'; 
});

However, if you are using a standard GET link without AJAX, the redirection logic becomes more complex because the browser might try to reload the page before your script can run. In such cases, switching to an AJAX-based implementation is highly recommended to maintain control over the user flow.

A Note on Custom E-commerce Solutions

While customizing WooCommerce templates offers granular control, building complex e-commerce flows from scratch outside of the platform's ecosystem can be fragile. If you find yourself constantly fighting with template overrides, hook filters, and AJAX workarounds, it may be worth considering a more robust headless or API-first approach. For developers looking to integrate custom storefronts with powerful backend systems without the overhead of maintaining core e-commerce logic, https://laravelcompany.com offers resources on building scalable applications where cart management can be handled cleanly via their own APIs rather than wrestling with legacy template hooks.

By shifting from direct HTML links to structured AJAX calls and leveraging WooCommerce's event system, you gain a much higher degree of control over the shopping experience. This ensures that your custom buttons behave predictably, regardless of where they appear on your site.

Our network