Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions blocks/pdp/add-to-cart.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
}
}
});
7 changes: 5 additions & 2 deletions tests/pdp/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down