plugin is now in canary state, button is not working currently but mail will now be displayed if user is logged in
This commit is contained in:
parent
9b8e4e465a
commit
9863afe09a
@ -2,20 +2,21 @@
|
|||||||
|
|
||||||
{% block buy_widget_price %}
|
{% block buy_widget_price %}
|
||||||
{{ parent() }}
|
{{ parent() }}
|
||||||
{{ dump(page.customer) }}
|
|
||||||
|
|
||||||
<div id="product-notify-container" class="product-notify-container mt-3" style="display: none;">
|
<div id="product-notify-container" class="product-notify-container mt-3" style="display: none;">
|
||||||
<h3>{{ "NotifyIfAvail.notify_me"|trans }}</h3>
|
<h3>{{ "NotifyIfAvail.notify_me"|trans }}</h3>
|
||||||
|
|
||||||
{% if app.customer %}
|
{% if context.customer is defined and context.customer %}
|
||||||
<!-- Eingeloggter Benutzer: Email wird dynamisch eingefügt -->
|
<!-- Eingeloggter Benutzer: Zeige E-Mail-Adresse -->
|
||||||
<p id="user-email-display" style="display: none;"></p>
|
<p id="user-email-display" data-customer-email="{{ context.customer.email }}">
|
||||||
|
Eingeloggt als: {{ context.customer.email }}
|
||||||
|
</p>
|
||||||
<button class="btn btn-primary w-100 notify-me-button"
|
<button class="btn btn-primary w-100 notify-me-button"
|
||||||
data-product-id="{{ product.id }}">
|
data-product-id="{{ product.id }}">
|
||||||
{{ "NotifyIfAvail.notify_me"|trans }}
|
{{ "NotifyIfAvail.notify_me"|trans }}
|
||||||
</button>
|
</button>
|
||||||
{% else %}
|
{% else %}
|
||||||
<!-- Gast: Eingabefeld + Button -->
|
<!-- Gast: Zeige Eingabefeld -->
|
||||||
<input type="email" id="notify-me-email" class="form-control mb-2"
|
<input type="email" id="notify-me-email" class="form-control mb-2"
|
||||||
placeholder="{{ 'NotifyIfAvail.email_placeholder'|trans }}" required>
|
placeholder="{{ 'NotifyIfAvail.email_placeholder'|trans }}" required>
|
||||||
<button class="btn btn-primary w-100 notify-me-button"
|
<button class="btn btn-primary w-100 notify-me-button"
|
||||||
@ -26,34 +27,30 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
console.log("NotifyMe Script geladen!");
|
console.log("🔹 NotifyMe Script geladen!");
|
||||||
document.addEventListener('DOMContentLoaded', async function () {
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
const notifyContainer = document.getElementById('product-notify-container');
|
const notifyContainer = document.getElementById('product-notify-container');
|
||||||
const cartButton = document.querySelector('.btn-buy'); // Prüft, ob der Warenkorb-Button existiert
|
const cartButton = document.querySelector('.btn-buy');
|
||||||
|
const emailInput = document.getElementById('notify-me-email');
|
||||||
const emailDisplay = document.getElementById('user-email-display');
|
const emailDisplay = document.getElementById('user-email-display');
|
||||||
const notifyButton = document.querySelector('.notify-me-button');
|
const notifyButton = document.querySelector('.notify-me-button');
|
||||||
|
|
||||||
// Button nur anzeigen, wenn KEIN Warenkorb-Button existiert
|
// Button nur anzeigen, wenn KEIN Warenkorb-Button existiert
|
||||||
if (!cartButton) {
|
if (!cartButton && notifyContainer) {
|
||||||
notifyContainer.style.display = 'block';
|
notifyContainer.style.display = 'block';
|
||||||
|
|
||||||
// Falls Nutzer eingeloggt ist, versuche die E-Mail-Adresse zu bekommen
|
|
||||||
try {
|
|
||||||
let response = await fetch('/account/profile');
|
|
||||||
let html = await response.text();
|
|
||||||
let emailMatch = html.match(/type="email"[^>]*value="([^"]+)"/);
|
|
||||||
|
|
||||||
if (emailMatch) {
|
|
||||||
emailDisplay.innerText = `Eingeloggt als: ${emailMatch[1]}`;
|
|
||||||
emailDisplay.style.display = 'block';
|
|
||||||
notifyButton.setAttribute('data-customer-email', emailMatch[1]);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Fehler beim Abrufen der Kunden-E-Mail:", error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event-Listener für alle Benachrichtigungsbuttons
|
// Falls der Benutzer eingeloggt ist, E-Mail abrufen
|
||||||
|
if (emailDisplay && emailDisplay.dataset.customerEmail) {
|
||||||
|
console.log("✅ Benutzer eingeloggt als:", emailDisplay.dataset.customerEmail);
|
||||||
|
emailInput.style.display = 'none'; // Eingabefeld ausblenden
|
||||||
|
notifyButton.setAttribute('data-customer-email', emailDisplay.dataset.customerEmail);
|
||||||
|
} else {
|
||||||
|
console.log("❌ Kein Benutzer eingeloggt.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event-Listener für den Button
|
||||||
document.body.addEventListener('click', function (event) {
|
document.body.addEventListener('click', function (event) {
|
||||||
if (event.target.classList.contains('notify-me-button')) {
|
if (event.target.classList.contains('notify-me-button')) {
|
||||||
let email;
|
let email;
|
||||||
@ -62,7 +59,7 @@
|
|||||||
if (event.target.dataset.customerEmail) {
|
if (event.target.dataset.customerEmail) {
|
||||||
email = event.target.dataset.customerEmail;
|
email = event.target.dataset.customerEmail;
|
||||||
} else {
|
} else {
|
||||||
email = document.getElementById('notify-me-email').value;
|
email = emailInput.value;
|
||||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||||
if (!emailRegex.test(email)) {
|
if (!emailRegex.test(email)) {
|
||||||
alert("Bitte geben Sie eine gültige E-Mail-Adresse ein.");
|
alert("Bitte geben Sie eine gültige E-Mail-Adresse ein.");
|
||||||
@ -77,7 +74,7 @@
|
|||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => alert(data.message))
|
.then(data => alert(data.message))
|
||||||
.catch(error => console.error('Error:', error));
|
.catch(error => console.error('⚠ Fehler:", error'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user