diff --git a/blocks/pdp/add-to-cart.js b/blocks/pdp/add-to-cart.js index d3afa4fa..9ce3705c 100644 --- a/blocks/pdp/add-to-cart.js +++ b/blocks/pdp/add-to-cart.js @@ -1,5 +1,5 @@ import { getMetadata } from '../../scripts/aem.js'; -import { checkOutOfStock } from '../../scripts/scripts.js'; +import { checkOutOfStock, getLocaleAndLanguage } from '../../scripts/scripts.js'; /** * Renders "Find Locally" button container. @@ -209,13 +209,14 @@ export default function renderAddToCart(block, parent) { // add product to cart with selected options and quantity await cartApi.addToCart(sku, selectedOptions, quantity); + const { locale, language } = await getLocaleAndLanguage(); // redirect to cart page after successful addition - window.location.href = '/us/en_us/checkout/cart/'; + window.location.href = `/${locale}/${language}/checkout/cart/`; } catch (error) { // eslint-disable-next-line no-console console.error('Failed to add item to cart', error); - } finally { - // update button state to show ATC + + // update button state to re-enable atc addToCartButton.textContent = 'Add to Cart'; addToCartButton.removeAttribute('aria-disabled'); } @@ -229,3 +230,15 @@ export default function renderAddToCart(block, parent) { return addToCartContainer; } + +// reset button state when page is restored from bfcache +window.addEventListener('pageshow', (event) => { + if (event.persisted) { + // Page was restored from bfcache + const addToCartButton = document.querySelector('.add-to-cart button'); + if (addToCartButton) { + addToCartButton.textContent = 'Add to Cart'; + addToCartButton.removeAttribute('aria-disabled'); + } + } +}); diff --git a/tests/pdp/integration.spec.js b/tests/pdp/integration.spec.js index 777f9c66..9defc1ea 100644 --- a/tests/pdp/integration.spec.js +++ b/tests/pdp/integration.spec.js @@ -190,7 +190,8 @@ test.describe('PDP Integration Tests', () => { await page.route('**/us/en_us/checkout/cart/add/**', async (route) => { // check that the correct uenc path segment exists const url = new URL(route.request().url()); - expect(url.pathname).toContain('/us/en_us/checkout/cart/add/uenc/aHR0cHM6Ly9hdGMtZXJyb3JzLS12aXRhbWl4LS1hZW1zaXRlcy5hZW0ubmV0d29yay91cy9lbl91cy9wcm9kdWN0cy9hc2NlbnQteDM=_Q09VUE9OPXRlc3QmbWFydGVjaD1vZmY=/product/3641/'); + expect(url.pathname).toContain('/us/en_us/checkout/cart/add/uenc/'); + expect(url.pathname).toContain('/product/3641/'); const requestBody = route.request().postData(); // requestBody is a multipart form data string @@ -368,7 +369,9 @@ test.describe('PDP Integration Tests', () => { await page.route('**/us/en_us/checkout/cart/add/**', async (route) => { // check that the correct uenc path segment exists const url = new URL(route.request().url()); - expect(url.pathname).toEqual('/us/en_us/checkout/cart/add/uenc/aHR0cHM6Ly9hdGMtZXJyb3JzLS12aXRhbWl4LS1hZW1zaXRlcy5hZW0ubmV0d29yay91cy9lbl91cy9wcm9kdWN0cy81MjAwLWxlZ2FjeS1idW5kbGU=_bWFydGVjaD1vZmY=/product/3701/'); + + expect(url.pathname).toContain('/us/en_us/checkout/cart/add/uenc/'); + expect(url.pathname).toContain('/product/3701/'); const requestBody = route.request().postData(); // requestBody is a multipart form data string