Client config values not working within snippet

Hi all.

I have a simple snippet which shows a chunk based on a date range. (for example, for free shipping in January).

Currently in the snippet I hard code the dates like so:

<?php
$today = date('Y-m-d');

// Adjust the date ranges as needed
$timedChunk = 'freeShippingJan2025';
$standardBannerChunk = 'normalBanner';

if ($today >= '2024-12-22' && $today <= '2025-01-31') {
    return $modx->getChunk($timedChunk);
} else {
    return $modx->getChunk($standardBannerChunk);
}

But I felt it would be easier to manage by setting the dates in client config. So I set up the settings, and replaced them in my snippet like so:

<?php
$today = date('Y-m-d');

// Adjust the date ranges as needed
$timedChunk = 'freeShippingJan2025';
$standardBannerChunk = 'normalBanner';

if ($today >= '[[++dateBannerStarts]]' && $today <= '[[++dateBannerEnds]]') {
    return $modx->getChunk($timedChunk);
} else {
    return $modx->getChunk($standardBannerChunk);
}

But this doesn’t work.
For sanity, I also included my client config settings in the template itself to check they are working like so:

[[++dateBannerStarts]]

and the output is exactly as expected:

2024-12-22

So I don’t understand why the date value doesn’t trigger the chunk within the snippet?

I’m sure there’s a simple explanation!

I would love to understand it!

Thanks
Andy

The field type is set to ‘date’ by the way.

You can’t use placeholders like that in a snippet. It should look more like this:

$today = date('Y-m-d');

$startDate = $modx->getOption('dateBannerStarts');
$endDate = $modx->getOption('dateBannerEnds');

$timedChunk = 'freeShippingJan2025';
$standardBannerChunk = 'normalBanner';

if ($startDate && $endDate && $today >= $startDate && $today <= $endDate) {
    return $modx->getChunk($timedChunk);
} else {
    return $modx->getChunk($standardBannerChunk);
}

Aha ok!

I will give that a go. Thank you!!

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.