Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds min/max sale price querying/filtering to the bike page #150

Merged
merged 9 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Setup PHP
id: setup-php
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: ${{ env.PHP_EXTENSIONS }}
ini-values: post_max_size=256M, max_execution_time=180, memory_limit=512M
ini-file: development
tools: composer:v2
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
Expand Down
9 changes: 5 additions & 4 deletions modules/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use craft\commerce\elements\Variant;
use craft\elements\Address;
use craft\elements\Entry;
use craft\events\ModelEvent;
use craft\events\RegisterElementSourcesEvent;
use craft\web\twig\variables\CraftVariable;
use modules\services\Reviews;
Expand Down Expand Up @@ -90,8 +91,8 @@ function($event) {
Event::on(
Entry::class,
Entry::EVENT_AFTER_SAVE,
function($event) {
/** @var Entry $entry */
function(ModelEvent $event) {
/** @var Entry|null $entry */
$entry = $event->sender;
if ($entry && $entry->getSection()?->handle === 'reviews') {
Craft::$app->getCache()->delete(Reviews::CACHE_KEY);
Expand All @@ -103,8 +104,8 @@ function($event) {
Event::on(
Entry::class,
Entry::EVENT_AFTER_DELETE,
function($event) {
/** @var Entry $entry */
function(Event $event) {
timkelty marked this conversation as resolved.
Show resolved Hide resolved
/** @var Entry|null $entry */
$entry = $event->sender;
if ($entry && $entry->getSection()?->handle === 'reviews') {
Craft::$app->getCache()->delete(Reviews::CACHE_KEY);
Expand Down
91 changes: 0 additions & 91 deletions modules/demos/src/console/controllers/SeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@
use Illuminate\Support\Collection;
use Solspace\Freeform\Elements\Db\SubmissionQuery;
use Solspace\Freeform\Elements\Submission;
use Solspace\Freeform\Freeform;
use Solspace\Freeform\Library\Composer\Components\Form;
use yii\base\Event;
use yii\console\ExitCode;

class SeedController extends Controller
{
public const FREEFORM_SUBMISSION_MIN = 1;
public const FREEFORM_SUBMISSION_MAX = 10;
public const FREEFORM_MESSAGE_CHARS_MIN = 5;
public const FREEFORM_MESSAGE_CHARS_MAX = 10;

/**
* Maximum number of carts to generate per day.
*/
Expand Down Expand Up @@ -184,7 +177,6 @@ function(MailEvent $event) {
public function actionIndex(): int
{
$this->stdout('Beginning seed ... ' . PHP_EOL . PHP_EOL);
// $this->runAction('freeform-data', ['contact']);
$this->runAction('refresh-articles');
$this->runAction('commerce-data');
$this->_cleanup();
Expand Down Expand Up @@ -223,42 +215,6 @@ private function _cleanup()
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
}

/**
* Seeds Freeform with submission data
*
* @param string $formHandle Freeform form handle
* @return int
*/
public function actionFreeformData(string $formHandle): int
{
$this->stdout('Seeding Freeform data ... ' . PHP_EOL);

$freeform = Freeform::getInstance();
$form = $freeform->forms->getFormByHandle($formHandle)->getForm();
$submissionCount = $this->_faker->numberBetween(self::FREEFORM_SUBMISSION_MIN, self::FREEFORM_SUBMISSION_MAX);
$errorCount = 0;

for ($i = 1; $i <= $submissionCount; $i++) {
try {
$submission = $this->_createFormSubmission($form);
$this->stdout(" - [{$i}/{$submissionCount}] Creating submission {$submission->title} ... ");

if ($this->_saveFormSubmission($submission)) {
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
} else {
$this->stderr('failed: ' . implode(', ', $submission->getErrorSummary(true)) . PHP_EOL, Console::FG_RED);
$errorCount++;
}
} catch (\Throwable $e) {
$this->stderr('error: ' . $e->getMessage() . PHP_EOL, Console::FG_RED);
$errorCount++;
}
}

$this->stdout('Done seeding Freeform data.' . PHP_EOL . PHP_EOL, Console::FG_GREEN);
return $errorCount ? ExitCode::UNSPECIFIED_ERROR : ExitCode::OK;
}

public function actionRefreshArticles(): int
{
$this->stdout('Refreshing articles ... ');
Expand Down Expand Up @@ -327,53 +283,6 @@ private function deleteElements(ElementQuery $query, string $label = 'elements')
}
$this->stdout($message . PHP_EOL . PHP_EOL, Console::FG_GREEN);
}

private function _createFormSubmission(Form $form): Submission
{
/** @var Submission $submission */
$submission = Freeform::getInstance()->submissions->createSubmissionFromForm($form);
$submission->dateCreated = $submission->dateUpdated = $this->_faker->dateTimeThisMonth();

// Reparse the title with the fake date
$submission->title = Craft::$app->view->renderString(
$form->getSubmissionTitleFormat(),
$form->getLayout()->getFieldsByHandle() + [
'dateCreated' => $submission->dateCreated,
'form' => $form,
]
);

$submission->setFormFieldValues([
'email' => $this->_faker->email,
'firstName' => $this->_faker->firstName,
'lastName' => $this->_faker->lastName,
'message' => $this->_faker->realTextBetween(self::FREEFORM_MESSAGE_CHARS_MIN, self::FREEFORM_MESSAGE_CHARS_MAX),
]);

return $submission;
}

private function _saveFormSubmission(Submission $submission): bool
{
if (!Craft::$app->getElements()->saveElement($submission)) {
return false;
}

// Update submissions table to match date, so element index will sort properly
$dateCreatedDb = Db::prepareDateForDb($submission->dateCreated);

Craft::$app->db->createCommand()
->update($submission::TABLE, [
'dateCreated' => $dateCreatedDb,
'dateUpdated' => $dateCreatedDb,
], [
'id' => $submission->id,
])
->execute();

return true;
}

/**
* Create demo users.
*
Expand Down
36 changes: 36 additions & 0 deletions sprig/components/ProductFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class ProductFilter extends Component
*/
public string|array $materials = [];

/**
* @var string|null
*/
public ?string $maxPrice = null;

/**
* @var string|null
*/
public ?string $minPrice = null;

/**
* @var string
*/
Expand Down Expand Up @@ -139,6 +149,8 @@ protected function defineRules(): array
'type',
'colors',
'materials',
'maxPrice',
'minPrice',
'sort',
'currentPushUrl',
'saveState',
Expand All @@ -161,6 +173,8 @@ public function attributes(): array
$attributes[] = 'filterUrlsByType';
$attributes[] = 'isAllBikes';
$attributes[] = 'materialFilters';
$attributes[] = 'maxPrice';
$attributes[] = 'minPrice';
$attributes[] = 'productCount';
$attributes[] = 'products';
$attributes[] = 'pushUrl';
Expand Down Expand Up @@ -281,6 +295,20 @@ public function getProducts(): array
$query->relatedTo($type);
}

if (is_numeric($this->minPrice) || is_numeric($this->maxPrice)) {
if (is_numeric($this->minPrice) && is_numeric($this->maxPrice)) {
$priceQuery = ['and', '>= ' . $this->minPrice, '<= ' . $this->maxPrice];
} elseif (is_numeric($this->minPrice)) {
$priceQuery = '>= ' . $this->minPrice;
} elseif (is_numeric($this->maxPrice)) {
$priceQuery = '<= ' . $this->maxPrice;
}

if (isset($priceQuery)) {
$query->hasVariant(['salePrice' => $priceQuery]);
}
}

// Sort
[$sort, $direction] = $this->sort ? explode('|', $this->sort) : ['date', 'desc'];
if ($sort == 'price') {
Expand Down Expand Up @@ -405,6 +433,14 @@ private function _getUrlParams($key = null, $value = null): array
$urlParams['sort'] = $this->sort;
}

if ($this->minPrice !== null && $this->minPrice !== '') {
$urlParams['minPrice'] = $this->minPrice;
}

if ($this->maxPrice !== null && $this->maxPrice !== '') {
$urlParams['maxPrice'] = $this->maxPrice;
}

if (!empty($this->colors)) {
$urlParams['colors'] = $this->colors;
sort($urlParams['colors']);
Expand Down
24 changes: 24 additions & 0 deletions templates/_includes/components/filters/filter.twig
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@
} only %}
{% endif %}

<hr class="my-6">
<h3 class="uppercase text-xs text-gray-600">{{ 'Price'|t }}</h3>
<div class="md:flex items-center">
<div>
<label for="minPrice" class="sr-only">{{ 'Minimum Price'|t }}</label>
{{ input('text', 'minPrice', minPrice, {
'id': 'minPrice',
'x-on:keyup.debounce.400': "if ($event.key !== 'Tab' && $event.keyCode !== 9 && (isFinite($event.key) || ($event.keyCode == 8 || $event.keyCode == 46))) { htmx.trigger($event.target, 'refresh'); }",
'aria-controls': 'products-list',
}) }}
</div>
<div class="px-3">
{{ 'to'|t }}
</div>
<div>
<label for="maxPrice" class="sr-only">{{ 'Maximum Price'|t }}</label>
{{ input('text', 'maxPrice', maxPrice, {
'id': 'maxPrice',
'x-on:keyup.debounce.400': "if ($event.key !== 'Tab' && $event.keyCode !== 9 && (isFinite($event.key) || ($event.keyCode == 8 || $event.keyCode == 46))) { htmx.trigger($event.target, 'refresh'); }",
'aria-controls': 'products-list',
}) }}
</div>
</div>

<hr class="my-6">

<h3 class="uppercase text-xs text-gray-600">{{ 'Sort'|t }}</h3>
Expand Down
Loading