izawa
1
写真のページを構築中です。カートに追加するというボタンが動かないのですが、動かすにはどうすればよいでしょうか?
また、検証ツールで見ると以下の様になるのですが、action="/cart/add"は、どこにあるのでしょうか?cart.jsでしょうか?
実際に記述しているコードは以下です。
{%- form 'product', product, id: product_form_id, class: 'form', novalidate: 'novalidate', data-type: 'add-to-cart-form' -%}
{%- if block.settings.show_dynamic_checkout -%}
{{ form | payment_button }}
{%- endif -%}
{%- endform -%}
izawa
2
product-form.jsファイルの.onSubmitHandler(evt)にあるthis.cartNotification.setActiveElement(document.activeElement);という記述がエラーの原因でした。
これはnullのプロパティを読み取ることができないみたいです。
ここからどこをどう変更すればいいかはまだ分かりません。
ちなみに、これはDawnテーマを元にしています。
if (!customElements.get('product-form')) {
customElements.define('product-form', class ProductForm extends HTMLElement {
constructor() {
super();
this.form = this.querySelector('form');
this.form.addEventListener('submit', this.onSubmitHandler.bind(this));
this.cartNotification = document.querySelector('cart-notification');
}
onSubmitHandler(evt) {
evt.preventDefault();
const submitButton = this.querySelector('[type="submit"]');
if (submitButton.classList.contains('loading')) return;
this.handleErrorMessage();
this.cartNotification.setActiveElement(document.activeElement);
submitButton.setAttribute('aria-disabled', true);
submitButton.classList.add('loading');
const config = fetchConfig('javascript');
config.headers['X-Requested-With'] = 'XMLHttpRequest';
config.body = JSON.stringify({
...JSON.parse(serializeForm(this.form)),
sections: this.cartNotification.getSectionsToRender().map((section) => section.id),
sections_url: window.location.pathname
});
fetch(`${routes.cart_add_url}`, config)
.then((response) => response.json())
.then((response) => {
if (response.status) {
this.handleErrorMessage(response.description);
return;
}
this.cartNotification.renderContents(response);
})
.catch((e) => {
console.error(e);
})
.finally(() => {
submitButton.classList.remove('loading');
submitButton.removeAttribute('aria-disabled');
});
}
handleErrorMessage(errorMessage = false) {
this.errorMessageWrapper = this.errorMessageWrapper || this.querySelector('.product-form__error-message-wrapper');
this.errorMessage = this.errorMessage || this.errorMessageWrapper.querySelector('.product-form__error-message');
this.errorMessageWrapper.toggleAttribute('hidden', !errorMessage);
if (errorMessage) {
this.errorMessage.textContent = errorMessage;
}
}
});
}
izawa
3
this.cartNotification.setActiveElement(document.activeElement);
この行の this.cartNotification が null なので、setActiveElement というプロパティが読めない。
this.cartNotification = document.querySelector('cart-notification');
ここで document.querySelector() が null を返していた。
()内の記述が間違っているのか、と思って色々調べて試してみましたが問題なかったようでした…。
()内が反応していないことは確かなので、とりあえずこのproduct-form.jsを読み込ませるのをやめたらボタンは機能しました。
なぜnullが返ってくるのかは未だに分からないが、とりあえずの応急処置で読み込ませるのをやめました。
izawa
4
teratailで質問したら、教えていただけました。
https://teratail.com/questions/365988#
原因は、setActiveを定義しているcartNotification.jsのファイルの読み込む順でした。
cartNotification.jsを読み込んでからproduct-form.jsを読み込ませないといけないようです。
すいません。このCDNの読み込みの順番を変更するのはどうやるのでしょうか?