113 lines
5.4 KiB
PHP
113 lines
5.4 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
/** @var array<string, mixed> $product */
|
||
/** @var array<int, array<string, mixed>> $variants */
|
||
/** @var array<int, array<string, mixed>> $images */
|
||
|
||
$mainImage = $product['main_image_path'] ?? '';
|
||
$showUnitPriceForVariant = static function (array $variant) use ($product): bool {
|
||
if (function_exists('product_should_show_unit_price')) {
|
||
return product_should_show_unit_price($product, $variant);
|
||
}
|
||
|
||
$unit = (string) ($variant['unit'] ?? $product['base_unit'] ?? '');
|
||
if (!in_array($unit, ['kg', 'кг'], true)) {
|
||
return false;
|
||
}
|
||
|
||
if (($variant['price_per_unit'] ?? null) === null || (float) $variant['price_per_unit'] <= 0) {
|
||
return false;
|
||
}
|
||
|
||
$packageQuantity = (float) ($variant['package_quantity'] ?? 0);
|
||
if ($packageQuantity < 1) {
|
||
return false;
|
||
}
|
||
|
||
$lower = static function (string $value): string {
|
||
return function_exists('mb_strtolower') ? mb_strtolower($value, 'UTF-8') : strtolower($value);
|
||
};
|
||
|
||
return preg_match('/(^|[\s,.;])\d+(?:[.,]\d+)?\s*(мл|шт)\.?($|[\s,.;])/u', $lower((string) ($variant['name'] ?? ''))) !== 1;
|
||
};
|
||
?>
|
||
<section
|
||
class="product-detail price-aware"
|
||
data-product-id="<?= e((string) ($product['id'] ?? '')) ?>"
|
||
data-product-name="<?= e((string) ($product['name'] ?? '')) ?>"
|
||
data-product-slug="<?= e((string) ($product['slug'] ?? '')) ?>"
|
||
data-product-image="<?= e((string) ($product['main_image_path'] ?? '')) ?>"
|
||
>
|
||
<div class="product-gallery panel">
|
||
<?php if ($mainImage !== ''): ?>
|
||
<img src="<?= e((string) $mainImage) ?>" alt="<?= e((string) $product['name']) ?>">
|
||
<?php else: ?>
|
||
<div class="product-image-placeholder">Рыбсток</div>
|
||
<?php endif; ?>
|
||
|
||
<?php if ($images !== []): ?>
|
||
<div class="product-thumbs">
|
||
<?php foreach ($images as $image): ?>
|
||
<img src="<?= e((string) $image['path']) ?>" alt="<?= e((string) ($image['alt'] ?: $product['name'])) ?>">
|
||
<?php endforeach; ?>
|
||
</div>
|
||
<?php endif; ?>
|
||
</div>
|
||
|
||
<article class="product-detail-main panel">
|
||
<p class="eyebrow"><?= e((string) ($product['category_name'] ?? 'Каталог')) ?></p>
|
||
<h1><?= e((string) ($product['h1'] ?: $product['name'])) ?></h1>
|
||
|
||
<?php if (!empty($product['short_description'])): ?>
|
||
<p class="product-lead"><?= e((string) $product['short_description']) ?></p>
|
||
<?php endif; ?>
|
||
|
||
<?php if ($variants !== []): ?>
|
||
<section class="variant-list" aria-label="Фасовки товара">
|
||
<h2>Фасовки</h2>
|
||
<?php foreach ($variants as $variant): ?>
|
||
<?php $showUnitPrice = $showUnitPriceForVariant($variant); ?>
|
||
<div
|
||
class="variant-row"
|
||
data-variant-id="<?= e((string) ($variant['id'] ?? '')) ?>"
|
||
data-variant-name="<?= e((string) ($variant['name'] ?? '')) ?>"
|
||
data-base-unit-price="<?= e((string) ($variant['price_per_unit'] ?? '')) ?>"
|
||
data-base-package-price="<?= e((string) ($variant['package_price'] ?? '')) ?>"
|
||
>
|
||
<strong><?= e((string) $variant['name']) ?></strong>
|
||
<span>
|
||
<?php if ($showUnitPrice): ?>
|
||
<span data-dynamic-unit-price data-base-unit-price="<?= e((string) $variant['price_per_unit']) ?>"><?= e(money_label($variant['price_per_unit'])) ?></span> / <?= e(unit_label((string) $variant['unit'])) ?>
|
||
<?php endif; ?>
|
||
</span>
|
||
<b>
|
||
<small>За фасовку <?= e((string) $variant['name']) ?></small>
|
||
<?php if (!empty($variant['old_package_price'])): ?>
|
||
<span class="old-price"><?= e(money_label($variant['old_package_price'])) ?></span>
|
||
<?php endif; ?>
|
||
<span data-dynamic-package-price data-base-package-price="<?= e((string) $variant['package_price']) ?>"><?= e(money_label($variant['package_price'])) ?></span>
|
||
</b>
|
||
<div class="product-card-actions">
|
||
<div class="quantity-stepper" data-quantity-control aria-label="Количество фасовок">
|
||
<button type="button" data-quantity-minus aria-label="Уменьшить количество">-</button>
|
||
<input data-quantity-input type="text" inputmode="numeric" value="1" aria-label="Количество">
|
||
<button type="button" data-quantity-plus aria-label="Увеличить количество">+</button>
|
||
</div>
|
||
<button class="add-to-cart-button" type="button">В корзину</button>
|
||
</div>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
</section>
|
||
<?php endif; ?>
|
||
</article>
|
||
|
||
<?php if (!empty($product['description'])): ?>
|
||
<section class="product-description panel">
|
||
<h2>Описание</h2>
|
||
<?= (string) $product['description'] ?>
|
||
</section>
|
||
<?php endif; ?>
|
||
</section>
|