v1.0.0.1
StocketBase
- <?php
-
- declare(strict_types=1);
-
- if (count(get_included_files()) === 1) {
- http_response_code(403);
- exit;
- }
-
- if (!Auth::hasRoleAtLeast(Auth::ROLE_STORE_MANAGER)) {
- echo '<p>You don\'t have access to discounts.</p>';
- return;
- }
-
- $db = Database::site();
- $discounts = $db->query('SELECT * FROM discounts ORDER BY created_at DESC')->fetchAll();
-
- ?>
- <div class="dash-header-row">
- <h1 class="dash-title"><?= Icons::icon("tag", "icon icon-lg") ?>Discounts</h1>
- <button type="button" class="dash-btn dash-btn-primary" id="discount-new-btn"><?= Icons::icon('plus', 'icon icon-sm') ?>New discount</button>
- </div>
-
- <table class="dash-table">
- <thead>
- <tr>
- <th><?= Icons::icon('tag', 'icon icon-sm') ?>Code</th>
- <th><?= Icons::icon('dollar', 'icon icon-sm') ?>Value</th>
- <th><?= Icons::icon('checklist', 'icon icon-sm') ?>Redemptions</th>
- <th><?= Icons::icon('flag', 'icon icon-sm') ?>Active</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($discounts as $discount): ?>
- <tr>
- <td><?= htmlspecialchars($discount['code']) ?></td>
- <td><?= $discount['discount_type'] === 'percentage' ? (int) $discount['value'] . '%' : number_format(((int) $discount['value']) / 100, 2) . ' ' . htmlspecialchars((string) $discount['currency']) ?></td>
- <td><?= (int) $discount['times_redeemed'] ?><?= $discount['max_redemptions'] ? ' / ' . (int) $discount['max_redemptions'] : '' ?></td>
- <td><?= $discount['is_active'] ? 'Yes' : 'No' ?></td>
- <td class="dash-table-actions">
- <button type="button" class="dash-btn-small js-edit-discount"
- data-id="<?= (int) $discount['id'] ?>"
- data-code="<?= htmlspecialchars($discount['code'], ENT_QUOTES) ?>"
- data-description="<?= htmlspecialchars((string) $discount['description'], ENT_QUOTES) ?>"
- data-type="<?= htmlspecialchars($discount['discount_type'], ENT_QUOTES) ?>"
- data-value="<?= $discount['discount_type'] === 'percentage' ? (int) $discount['value'] : number_format(((int) $discount['value']) / 100, 2, '.', '') ?>"
- data-currency="<?= htmlspecialchars((string) $discount['currency'], ENT_QUOTES) ?>"
- data-min-order="<?= $discount['min_order_cents'] !== null ? number_format(((int) $discount['min_order_cents']) / 100, 2, '.', '') : '' ?>"
- data-max-redemptions="<?= htmlspecialchars((string) ($discount['max_redemptions'] ?? ''), ENT_QUOTES) ?>"
- data-active="<?= (int) $discount['is_active'] ?>"><?= Icons::icon('edit', 'icon icon-sm') ?>Edit</button>
- <form method="post" style="display:inline;" onsubmit="return confirm('Delete this discount code? Customers won\'t be able to use it anymore.');">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="delete_discount">
- <input type="hidden" name="discount_id" value="<?= (int) $discount['id'] ?>">
- <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- <?php if (empty($discounts)): ?>
- <tr><td colspan="5">No discount codes yet. Create one for a sale or a thank-you gift.</td></tr>
- <?php endif; ?>
- </tbody>
- </table>
-
- <div id="discount-form-wrapper" class="sidebar-box" style="max-width:480px;margin-top:20px;display:none;">
- <h2 class="dash-subtitle" style="margin-top:0;"><span id="discount-form-title">New discount</span></h2>
- <form method="post" id="discount-form">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="save_discount">
- <input type="hidden" name="discount_id" id="discount_id_input" value="">
-
- <label>Code</label>
- <input type="text" name="code" id="discount_code_input" required>
-
- <label>Note for yourself (optional)</label>
- <input type="text" name="description" id="discount_description_input">
-
- <label>Discount type</label>
- <select name="discount_type" id="discount_type_input">
- <option value="percentage">Percent off (like 10%)</option>
- <option value="fixed_amount">Money off (like 20 PLN)</option>
- </select>
-
- <label>How much (a percent, or an amount like 10.00)</label>
- <input type="text" name="value" id="discount_value_input" required>
-
- <label>Currency (only for money off)</label>
- <input type="text" name="currency" id="discount_currency_input" maxlength="3" value="<?= htmlspecialchars(SiteFront::settings()['store_currency'] ?? 'USD') ?>">
-
- <label>Minimum order (optional, like 50.00)</label>
- <input type="text" name="min_order" id="discount_min_order_input">
-
- <label>How many times it can be used (optional)</label>
- <input type="number" name="max_redemptions" id="discount_max_redemptions_input">
-
- <label><input type="checkbox" name="is_active" id="discount_active_input" checked> Customers can use it now</label>
-
- <div class="publish-actions">
- <button type="submit" class="dash-btn dash-btn-primary"><?= Icons::icon('check', 'icon icon-sm') ?>Save discount</button>
- <button type="button" class="dash-btn" id="discount-cancel-btn"><?= Icons::icon('x', 'icon icon-sm') ?>Cancel</button>
- </div>
- </form>
- </div>
-
- <script>
- (function() {
- var wrapper = document.getElementById('discount-form-wrapper');
- var formTitle = document.getElementById('discount-form-title');
- var form = document.getElementById('discount-form');
-
- function resetForm() {
- form.reset();
- document.getElementById('discount_id_input').value = '';
- document.getElementById('discount_active_input').checked = true;
- formTitle.textContent = 'New discount';
- }
-
- function showForm() {
- wrapper.style.display = '';
- wrapper.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
- }
-
- document.getElementById('discount-new-btn').addEventListener('click', function() {
- resetForm();
- showForm();
- });
-
- document.getElementById('discount-cancel-btn').addEventListener('click', function() {
- wrapper.style.display = 'none';
- resetForm();
- });
-
- document.querySelectorAll('.js-edit-discount').forEach(function(button) {
- button.addEventListener('click', function() {
- document.getElementById('discount_id_input').value = this.dataset.id;
- document.getElementById('discount_code_input').value = this.dataset.code;
- document.getElementById('discount_description_input').value = this.dataset.description;
- document.getElementById('discount_type_input').value = this.dataset.type;
- document.getElementById('discount_value_input').value = this.dataset.value;
- document.getElementById('discount_currency_input').value = this.dataset.currency;
- document.getElementById('discount_min_order_input').value = this.dataset.minOrder;
- document.getElementById('discount_max_redemptions_input').value = this.dataset.maxRedemptions;
- document.getElementById('discount_active_input').checked = this.dataset.active === '1';
- formTitle.textContent = 'Edit discount';
- showForm();
- });
- });
- })();
- </script>
-