Add to cart button not working

Add to cart button not working

leechoostore
Excursionist
17 2 2

Hey, my add to cart button is not working properly. Product is being added to the cart but there is no pop up notification. Please help.

URL- https://leechoo.com/products/macrame-hammock-chair-swing-for-terrace

Replies 5 (5)

DitalTek
Shopify Partner
891 103 122

@leechoostore 

I guess you did customize some where in code and it make broken the code related to cart notification, because when i check has a error show in console screen.

 

VIEKIN_0-1726656316466.png

To solved, you need check again your customize code.

- You can please Like and Accepted Solution if my suggestion helpful.
- And you want to hire developer Theme or App Shopify => Feel free to send me a DM or contact to us via Email | WhatsApp
- Website: ditaltek.com
leechoostore
Excursionist
17 2 2

How do I fix it?

DitalTek
Shopify Partner
891 103 122

we can't give you solution detail we need check on directly in code file, and if you familiar with code you can,  you need go to product-form.js file and check line 92 then check code here,

the error come because JS code doing query to an element not defined.

- You can please Like and Accepted Solution if my suggestion helpful.
- And you want to hire developer Theme or App Shopify => Feel free to send me a DM or contact to us via Email | WhatsApp
- Website: ditaltek.com
dws_pvt_ltd
Shopify Partner
369 69 87

Hello @leechoostore 

please share product-form.js 

image (1).png

 

Best Regards,
Dws_pvt_ltd

Helpful? then please Like and Accept the Solution.
For any inquiries, please feel free to contact via WhatsApp and Email: sales@dolphinwebsolution.com.
For more information visit our website Dolphin Web Solution Pvt Ltd.
leechoostore
Excursionist
17 2 2

Hey, here is my code

if (!customElements.get('product-form')) {

  customElements.define(

    'product-form',

    class ProductForm extends HTMLElement {

      constructor() {

        super();

 

        this.form = this.querySelector('form');

        this.form.querySelector('[name=id]').disabled = false;

        this.form.addEventListener('submit', this.onSubmitHandler.bind(this));

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

        this.submitButton = this.querySelector('[type="submit"]');

 

        if (document.querySelector('cart-drawer')) this.submitButton.setAttribute('aria-haspopup', 'dialog');

 

        this.hideErrors = this.dataset.hideErrors === 'true';

      }

 

      onSubmitHandler(evt) {

        evt.preventDefault();

        if (this.submitButton.getAttribute('aria-disabled') === 'true') return;

 

        this.handleErrorMessage();

 

        this.submitButton.setAttribute('aria-disabled', true);

        this.submitButton.classList.add('loading');

        this.querySelector('.loading__spinner').classList.remove('hidden');

 

        const config = fetchConfig('javascript');

        config.headers['X-Requested-With'] = 'XMLHttpRequest';

        delete config.headers['Content-Type'];

 

        const formData = new FormData(this.form);

        if (this.cart) {

          formData.append(

            'sections',

            this.cart.getSectionsToRender().map((section) => section.id)

          );

          formData.append('sections_url', window.location.pathname);

          this.cart.setActiveElement(document.activeElement);

        }

        config.body = formData;

 

        fetch(`${routes.cart_add_url}`, config)

          .then((response) => response.json())

          .then((response) => {

            if (response.status) {

              publish(PUB_SUB_EVENTS.cartError, {

                source: 'product-form',

                productVariantId: formData.get('id'),

                errors: response.errors || response.description,

                message: response.message,

              });

              this.handleErrorMessage(response.description);

 

              const soldOutMessage = this.submitButton.querySelector('.sold-out-message');

              if (!soldOutMessage) return;

              this.submitButton.setAttribute('aria-disabled', true);

              this.submitButton.querySelector('span').classList.add('hidden');

              soldOutMessage.classList.remove('hidden');

              this.error = true;

              return;

            } else if (!this.cart) {

              window.location = window.routes.cart_url;

              return;

            }

 

            if (!this.error)

              publish(PUB_SUB_EVENTS.cartUpdate, {

                source: 'product-form',

                productVariantId: formData.get('id'),

                cartData: response,

              });

            this.error = false;

            const quickAddModal = this.closest('quick-add-modal');

            if (quickAddModal) {

              document.body.addEventListener(

                'modalClosed',

                () => {

                  setTimeout(() => {

                    this.cart.renderContents(response);

                  });

                },

                { once: true }

              );

              quickAddModal.hide(true);

            } else {

              this.cart.renderContents(response);

            }

          })

          .catch((e) => {

            console.error(e);

          })

          .finally(() => {

            this.submitButton.classList.remove('loading');

            if (this.cart && this.cart.classList.contains('is-empty')) this.cart.classList.remove('is-empty');

            if (!this.error) this.submitButton.removeAttribute('aria-disabled');

            this.querySelector('.loading__spinner').classList.add('hidden');

          });

      }

 

      handleErrorMessage(errorMessage = false) {

        if (this.hideErrors) return;

 

        this.errorMessageWrapper =

          this.errorMessageWrapper || this.querySelector('.product-form__error-message-wrapper');

        if (!this.errorMessageWrapper) return;

        this.errorMessage = this.errorMessage || this.errorMessageWrapper.querySelector('.product-form__error-message');

 

        this.errorMessageWrapper.toggleAttribute('hidden', !errorMessage);

 

        if (errorMessage) {

          this.errorMessage.textContent = errorMessage;

        }

      }

    }

  );

}