Ajax Cart Drawer

Topic summary

A user is experiencing an issue where clicking “Add to Cart” on a custom product filter section redirects to the cart page instead of opening the cart drawer, despite having the drawer setting enabled in their Dawn theme. The cart drawer works correctly on standard product pages.

Proposed Solution:

Another user suggests modifying the cart behavior by:

  • Locating the product-form component in assets/product-form.js
  • Finding the onSubmitHandler function that handles the cart add action
  • After a successful product addition (in the .then() response), calling a method to open the cart drawer/notification instead of redirecting
  • Using document.querySelector('cart-notification') or document.querySelector('cart-drawer') to trigger the drawer

Key Technical Details:

The code snippets show the custom filter section uses fetch requests to load products, but the add-to-cart functionality likely needs to be updated to match Dawn’s native drawer behavior by rendering cart contents and opening the drawer component after successful addition.

Summarized with AI on November 11. AI used: claude-sonnet-4-5-20250929.

I have a custom section on the main page with a product filter.The problem is that when I click on add to cart, I am redirected to the cart page, although the site settings indicate that the cart is Drawer and on the general product page it is added to the cart using Cart Drawer. I’m using the Dawn theme. How can I solve this problem?

document.addEventListener("DOMContentLoaded", () => {
  const buttons = document.querySelectorAll(".home-filter__buttons button");
  const productsContainer = document.querySelector(".home-filter__products");

  buttons.forEach((button) => {
    button.addEventListener("click", function () {
      buttons.forEach((btn) => {
        btn.classList.remove("active");
      });
      this.classList.add("active");

      const url = this.getAttribute("data-url");

      fetch(url, {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
        },
      })
        .then((response) => {
          if (!response.ok) {
            throw new Error("Network response was not ok");
          }
          return response.text();
        })
        .then((data) => {
          const tempDiv = document.createElement("div");
          tempDiv.innerHTML = data;
          const productGrid = tempDiv.querySelector("#product-grid");
          productsContainer.innerHTML = "";
          productsContainer.appendChild(productGrid);
        })
        .catch((error) => console.log("Error:", error));
    });
  });
});
<div class="home-filter__wrapper">
  <div class="page-width">
    <div class="home-filter__content">
      <div class="home-filter__buttons">
        {% assign unique_tags = collections.all.products | map: 'tags' | join: ',' | split: ',' | uniq %}
        {% for tag in unique_tags %}
          <button
            data-url="/collections/all/{{ tag | handle }}"
            {% if forloop.first %}
              class="active"
            {% endif %}
          >
            {{ tag }}
          </button>
        {% endfor %}
      </div>

      <div class="home-filter__products"></div>
    </div>
  </div>
</div>

Hello, in the Dawn theme, the “Add to Cart” button is located in the main-product.liquid file.

Locate render ‘buy-buttons’.

The location of buy-buttons is in snippets/buy-buttons.liquid.

Find the product-form component.

assets/product-form.js

Find the onSubmitHandler function.

cart_add_url = '/cart/add'

onSubmitHandler(){
...

 fetch(`${routes.cart_add_url}`, config)
   .then((response) => response.json())
    .then((response) => {
      // After the product is successfully added, call the following method to open the shopping cart popup.

 this.cart.renderContents(response)

  })

...

}
this.cart =
  document.querySelector('cart-notification') ||
  document.querySelector('cart-drawer');

Following the code above, you should be able to add products to the cart and open the cart drawer.

I hope my answer is helpful to you.