Fix multilingual routing for virtual product pages
Use site()->visit() to properly set language context for virtual pages. This ensures UI translations and language-specific content work correctly on both /slug (French) and /en/slug (English) routes. Changes: - Add site()->visit($page, $lang) in routes to set page language - Create product controller for language detection - Fix add-to-cart button to update text in .txt div instead of button - Remove broken hooks approach Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
4489e705b8
commit
f4ecdcf947
7 changed files with 41 additions and 20 deletions
|
|
@ -10,7 +10,8 @@
|
||||||
"Bash(grep:*)",
|
"Bash(grep:*)",
|
||||||
"Bash(npm run build:*)",
|
"Bash(npm run build:*)",
|
||||||
"Bash(php test-shopify.php:*)",
|
"Bash(php test-shopify.php:*)",
|
||||||
"WebFetch(domain:getkirby.com)"
|
"WebFetch(domain:getkirby.com)",
|
||||||
|
"WebFetch(domain:forum.getkirby.com)"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.header-cart-btn {
|
.header-cart-btn {
|
||||||
|
font-family: var(--font);
|
||||||
background: none;
|
background: none;
|
||||||
border: none;
|
border: none;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
@ -109,11 +110,11 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
&:not(:empty)::before {
|
&:not(:empty)::before {
|
||||||
content: '(';
|
content: "(";
|
||||||
}
|
}
|
||||||
|
|
||||||
&:not(:empty)::after {
|
&:not(:empty)::after {
|
||||||
content: ')';
|
content: ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -559,6 +559,7 @@ main {
|
||||||
color: var(--color-txt);
|
color: var(--color-txt);
|
||||||
}
|
}
|
||||||
#site-header .header-cart-btn {
|
#site-header .header-cart-btn {
|
||||||
|
font-family: var(--font);
|
||||||
background: none;
|
background: none;
|
||||||
border: none;
|
border: none;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -10,6 +10,13 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const buttonTextDiv = addToCartBtn.querySelector('.txt[data-button-text]');
|
||||||
|
|
||||||
|
if (!buttonTextDiv) {
|
||||||
|
console.error('Button text div not found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const texts = {
|
const texts = {
|
||||||
add: addToCartBtn.dataset.textAdd || 'Add to cart',
|
add: addToCartBtn.dataset.textAdd || 'Add to cart',
|
||||||
adding: addToCartBtn.dataset.textAdding || 'Adding...',
|
adding: addToCartBtn.dataset.textAdding || 'Adding...',
|
||||||
|
|
@ -28,13 +35,13 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
addToCartBtn.disabled = true;
|
addToCartBtn.disabled = true;
|
||||||
const originalText = addToCartBtn.textContent;
|
const originalText = buttonTextDiv.textContent;
|
||||||
addToCartBtn.textContent = texts.adding;
|
buttonTextDiv.textContent = texts.adding;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cartResult = await cart.addToCart(variantId, 1);
|
const cartResult = await cart.addToCart(variantId, 1);
|
||||||
|
|
||||||
addToCartBtn.textContent = texts.added;
|
buttonTextDiv.textContent = texts.added;
|
||||||
addToCartBtn.classList.add('success');
|
addToCartBtn.classList.add('success');
|
||||||
|
|
||||||
document.dispatchEvent(new CustomEvent('cart:updated', {
|
document.dispatchEvent(new CustomEvent('cart:updated', {
|
||||||
|
|
@ -43,19 +50,19 @@
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
addToCartBtn.disabled = false;
|
addToCartBtn.disabled = false;
|
||||||
addToCartBtn.textContent = originalText;
|
buttonTextDiv.textContent = originalText;
|
||||||
addToCartBtn.classList.remove('success');
|
addToCartBtn.classList.remove('success');
|
||||||
}, 1500);
|
}, 1500);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error adding to cart:', error);
|
console.error('Error adding to cart:', error);
|
||||||
|
|
||||||
addToCartBtn.textContent = texts.error;
|
buttonTextDiv.textContent = texts.error;
|
||||||
addToCartBtn.classList.add('error');
|
addToCartBtn.classList.add('error');
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
addToCartBtn.disabled = false;
|
addToCartBtn.disabled = false;
|
||||||
addToCartBtn.textContent = originalText;
|
buttonTextDiv.textContent = originalText;
|
||||||
addToCartBtn.classList.remove('error');
|
addToCartBtn.classList.remove('error');
|
||||||
}, 2000);
|
}, 2000);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,10 @@ return [
|
||||||
],
|
],
|
||||||
|
|
||||||
'routes' => [
|
'routes' => [
|
||||||
// French product pages (default)
|
// French products (default)
|
||||||
[
|
[
|
||||||
'pattern' => '(:any)',
|
'pattern' => '(:any)',
|
||||||
'action' => function($slug) {
|
'action' => function($slug) {
|
||||||
// Skip known pages
|
|
||||||
if (in_array($slug, ['home', 'error', 'thanks'])) {
|
if (in_array($slug, ['home', 'error', 'thanks'])) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -27,28 +26,28 @@ return [
|
||||||
|
|
||||||
foreach ($products as $product) {
|
foreach ($products as $product) {
|
||||||
if ($product['handle'] === $slug) {
|
if ($product['handle'] === $slug) {
|
||||||
return Page::factory([
|
$page = Page::factory([
|
||||||
'slug' => $product['handle'],
|
'slug' => $product['handle'],
|
||||||
'template' => 'product',
|
'template' => 'product',
|
||||||
'parent' => site()->homePage(),
|
|
||||||
'content' => [
|
'content' => [
|
||||||
'title' => $product['title'],
|
'title' => $product['title'],
|
||||||
'shopifyHandle' => $product['handle'],
|
'shopifyHandle' => $product['handle'],
|
||||||
'uuid' => $product['id']
|
'uuid' => $product['id']
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
site()->visit($page, 'fr');
|
||||||
|
return $page;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not a product, let Kirby handle normally
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
// English product pages
|
// English products
|
||||||
[
|
[
|
||||||
'pattern' => 'en/(:any)',
|
'pattern' => 'en/(:any)',
|
||||||
'action' => function($slug) {
|
'action' => function($slug) {
|
||||||
// Skip known pages
|
|
||||||
if (in_array($slug, ['home', 'error', 'thanks'])) {
|
if (in_array($slug, ['home', 'error', 'thanks'])) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -57,20 +56,21 @@ return [
|
||||||
|
|
||||||
foreach ($products as $product) {
|
foreach ($products as $product) {
|
||||||
if ($product['handle'] === $slug) {
|
if ($product['handle'] === $slug) {
|
||||||
return Page::factory([
|
$page = Page::factory([
|
||||||
'slug' => $product['handle'],
|
'slug' => $product['handle'],
|
||||||
'template' => 'product',
|
'template' => 'product',
|
||||||
'parent' => site()->homePage(),
|
|
||||||
'content' => [
|
'content' => [
|
||||||
'title' => $product['title'],
|
'title' => $product['title'],
|
||||||
'shopifyHandle' => $product['handle'],
|
'shopifyHandle' => $product['handle'],
|
||||||
'uuid' => $product['id']
|
'uuid' => $product['id']
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
site()->visit($page, 'en');
|
||||||
|
return $page;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not a product, let Kirby handle normally
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
11
site/controllers/product.php
Normal file
11
site/controllers/product.php
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return function ($page, $kirby) {
|
||||||
|
$shopifyHandle = $page->shopifyHandle()->or($page->slug())->value();
|
||||||
|
$language = $kirby->language()->code();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'shopifyHandle' => $shopifyHandle,
|
||||||
|
'language' => $language
|
||||||
|
];
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue