Well, first thing i did was creating a country/tax field inside the shipping.yml:
taxsettings:
type: structure
fields:
country:
type: text
vat:
type: number
Works fine, next step: Utilizing it inside the checkout.php:
<select data-action="country" id="country" name="country">
<option value=""selected disabled hidden>Auswählen...</option>
<?php
$items = $site->find('shipping')->taxsettings()->toStructure();
foreach ($items as $item):
?>
<option value="<?= $item->country() ?>"><?= $item->country() ?></option>
<?php endforeach ?>
</select>
Also works fine, next one: creating a cookie via JS inside checkout.js:
const selectElement = document.querySelector('select[data-action="country"]');
function updateCountry() {
var countryValue = selectElement.options[selectElement.selectedIndex].value;
document.cookie = "shipping-destination=" + countryValue + "; SameSite=Lax";
}
selectElement.addEventListener('change', updateCountry);
Also works fine, cookie updates as intended.
And this is where it gets messy.
Problem 1: I am quite new to routes in general, so i couldn’t find a good example to start with for my use case. As you already said, it has to go inside my config, right?
Problem 2: POST request with JS. Same here basically. Not enough experience yet. Especially where to place it.
Problem 3: The Model. The example you gave earlier basically covers everything, right? mine looks like this right now:
<?php
class ShippingPage extends Page
{
public function price(): Field
{
if (Cookie::get('shipping-destination')) {
$currenttax = kirby()->site()->taxsettings()->toStructure()->findBy('country', Cookie::get('shipping-destination')) ? kirby()->site()->taxsettings()->toStructure()->findBy('country', Cookie::get('shipping-destination'))->tax()->toFloat() : 0.0;
$destination = Cookie::get('shipping-destination');
} else {
$currenttax = 19;
$destination = 'DE';
}
if ($destination == 'DE') {
$value = 7.00;
return new Field($this, 'price', $value);
} else {
$value = 10.08 * (1 + ($currenttax / 100));
return new Field($this, 'price', $value);
}
}
public function tax(): float
{
if ($this->content()->tax()->isEmpty()) {
return 0;
}
return $this->kirby()->option('taxRates')[$this->content()->tax()->toString()];
}
}
Really sorry, I know it’s slightly over my head. I started the project without having this in mind, but I’ve come so far and this is the only thing that’s missing to make the project run perfectly.