Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Ajax Cart Drawer

Ajax Cart Drawer

Rostislav333
Shopify Partner
10 0 0

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>

 

Reply 1 (1)

eason_feng
Shopify Partner
34 4 7

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.

If this is helpful, please Like and Accept the solution.
Want to modify or custom changes on store? Let me help.
- Feel free to contact me on (fengzemin@shinetechchina.com)