質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Shopify

Shopifyとは、ECサイト向けのプラットフォームを提供している企業。さらに、その企業が提供するオンラインストアやPOSシステムを指します。高いデザイン性とカスタマイズ性が評価され、世界各国のネットショップで使用されています。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

1回答

1813閲覧

Shopify:カートページで、商品を増減させた時に金額を反映させたい。

zzziiii

総合スコア16

Shopify

Shopifyとは、ECサイト向けのプラットフォームを提供している企業。さらに、その企業が提供するオンラインストアやPOSシステムを指します。高いデザイン性とカスタマイズ性が評価され、世界各国のネットショップで使用されています。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2021/10/25 09:05

カートページで、商品を増減させた時に金額を反映させたいです。
shopifyのDawnというテーマを元にカスタマイズしています。

ページをリロードすると金額は商品の個数に合わせた額になるのですが、ボタンを押してすぐには反映されません。(キャッシュとかではないと思います。)

毎回カートエラーのメッセージが出てしまいます。
カートエラーが出るという処理より上に書いている部分がいけないのでしょうか?

class CartRemoveButton extends HTMLElement { constructor() { super(); this.addEventListener('click', (event) => { event.preventDefault(); this.closest('cart-items').updateQuantity(this.dataset.index, 0); }); } } customElements.define('cart-remove-button', CartRemoveButton); class CartItems extends HTMLElement { constructor() { super(); this.lineItemStatusElement = document.getElementById('shopping-cart-line-item-status'); this.currentItemCount = Array.from(this.querySelectorAll('[name="updates[]"]')) .reduce((total, quantityInput) => total + parseInt(quantityInput.value), 0); this.debouncedOnChange = debounce((event) => { this.onChange(event); }, 300); this.addEventListener('change', this.debouncedOnChange.bind(this)); } onChange(event) { this.updateQuantity(event.target.dataset.index, event.target.value, document.activeElement.getAttribute('name')); } getSectionsToRender() { return [ { id: 'main-cart-items', section: document.getElementById('main-cart-items').dataset.id, selector: '.js-contents', }, { id: 'cart-icon-bubble', section: 'cart-icon-bubble', selector: '.shopify-section' }, { id: 'cart-live-region-text', section: 'cart-live-region-text', selector: '.shopify-section' }, { id: 'main-cart-footer', section: document.getElementById('main-cart-footer').dataset.id, selector: '.js-contents', } ]; } updateQuantity(line, quantity, name) { this.enableLoading(line); const body = JSON.stringify({ line, quantity, sections: this.getSectionsToRender().map((section) => section.section), sections_url: window.location.pathname }); fetch(`${routes.cart_change_url}`, {...fetchConfig(), ...{ body }}) .then((response) => { return response.text(); }) .then((state) => { const parsedState = JSON.parse(state); this.classList.toggle('is-empty', parsedState.item_count === 0); const cartFooter = document.getElementById('main-cart-footer'); if (cartFooter) cartFooter.classList.toggle('is-empty', parsedState.item_count === 0); this.getSectionsToRender().forEach((section => { const elementToReplace = document.getElementById(section.id).querySelector(section.selector) || document.getElementById(section.id); elementToReplace.innerHTML = this.getSectionInnerHTML(parsedState.sections[section.section], section.selector); })); this.updateLiveRegions(line, parsedState.item_count); const lineItem = document.getElementById(`CartItem-${line}`); if (lineItem) lineItem.querySelector(`[name="${name}"]`).focus(); this.disableLoading(); }).catch(() => { this.querySelectorAll('.loading-overlay').forEach((overlay) => overlay.classList.add('hidden')); document.getElementById('cart-errors').textContent = window.cartStrings.error; this.disableLoading(); }); } updateLiveRegions(line, itemCount) { if (this.currentItemCount === itemCount) { document.getElementById(`Line-item-error-${line}`) .querySelector('.cart-item__error-text') .innerHTML = window.cartStrings.quantityError.replace( '[quantity]', document.getElementById(`Quantity-${line}`).value ); } this.currentItemCount = itemCount; this.lineItemStatusElement.setAttribute('aria-hidden', true); const cartStatus = document.getElementById('cart-live-region-text'); cartStatus.setAttribute('aria-hidden', false); setTimeout(() => { cartStatus.setAttribute('aria-hidden', true); }, 1000); } getSectionInnerHTML(html, selector) { return new DOMParser() .parseFromString(html, 'text/html') .querySelector(selector).innerHTML; } enableLoading(line) { document.getElementById('main-cart-items').classList.add('cart__items--disabled'); this.querySelectorAll(`#CartItem-${line} .loading-overlay`).forEach((overlay) => overlay.classList.remove('hidden')); document.activeElement.blur(); this.lineItemStatusElement.setAttribute('aria-hidden', false); } disableLoading() { document.getElementById('main-cart-items').classList.remove('cart__items--disabled'); } } customElements.define('cart-items', CartItems);

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

fetchでサーバにリクエストしていると思いますので、通信のタイムラグでは?

投稿2021/10/25 09:14

Lhankor_Mhy

総合スコア36158

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zzziiii

2021/10/25 09:28 編集

本番環境で見たら、商品別で金額の部分は都度変わるのですが、小計の部分はすぐ反映されません。これも、通信のタイムラグでしょうか...?
Lhankor_Mhy

2021/10/25 13:26

ああ、「すぐには反映されません」というのは、リロードしないと反映しない、という意味ですか? だとすると、そのエラーが関係してるかもしれないですね。
zzziiii

2021/10/26 04:23

そういう意味です! { id: 'main-cart-footer', section: document.getElementById('main-cart-footer').dataset.id, selector: '.js-contents', } が、拾えてないかfetch以降がエラーなんだと思ってます。。。 もう少し探してみます。ありがとうございます。
Lhankor_Mhy

2021/10/26 04:26

そうですか、がんばってください。 エラー行とエラーメッセージなどがわかれば、なにかわかることがあるかもしれません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問