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:
isUnknown 2026-01-16 16:40:10 +01:00
parent 4489e705b8
commit f4ecdcf947
7 changed files with 41 additions and 20 deletions

View file

@ -14,11 +14,10 @@ return [
],
'routes' => [
// French product pages (default)
// French products (default)
[
'pattern' => '(:any)',
'action' => function($slug) {
// Skip known pages
if (in_array($slug, ['home', 'error', 'thanks'])) {
return null;
}
@ -27,28 +26,28 @@ return [
foreach ($products as $product) {
if ($product['handle'] === $slug) {
return Page::factory([
$page = Page::factory([
'slug' => $product['handle'],
'template' => 'product',
'parent' => site()->homePage(),
'content' => [
'title' => $product['title'],
'shopifyHandle' => $product['handle'],
'uuid' => $product['id']
]
]);
site()->visit($page, 'fr');
return $page;
}
}
// Not a product, let Kirby handle normally
return null;
}
],
// English product pages
// English products
[
'pattern' => 'en/(:any)',
'action' => function($slug) {
// Skip known pages
if (in_array($slug, ['home', 'error', 'thanks'])) {
return null;
}
@ -57,20 +56,21 @@ return [
foreach ($products as $product) {
if ($product['handle'] === $slug) {
return Page::factory([
$page = Page::factory([
'slug' => $product['handle'],
'template' => 'product',
'parent' => site()->homePage(),
'content' => [
'title' => $product['title'],
'shopifyHandle' => $product['handle'],
'uuid' => $product['id']
]
]);
site()->visit($page, 'en');
return $page;
}
}
// Not a product, let Kirby handle normally
return null;
}
]

View 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
];
};