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 %}
|
||||
{{ parent() }}
|
||||
{{ dump(page.customer) }}
|
||||
|
||||
<div id="product-notify-container" class="product-notify-container mt-3" style="display: none;">
|
||||
<h3>{{ "NotifyIfAvail.notify_me"|trans }}</h3>
|
||||
|
||||
{% if app.customer %}
|
||||
<!-- Eingeloggter Benutzer: Email wird dynamisch eingefügt -->
|
||||
<p id="user-email-display" style="display: none;"></p>
|
||||
{% if context.customer is defined and context.customer %}
|
||||
<!-- Eingeloggter Benutzer: Zeige E-Mail-Adresse -->
|
||||
<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"
|
||||
data-product-id="{{ product.id }}">
|
||||
{{ "NotifyIfAvail.notify_me"|trans }}
|
||||
</button>
|
||||
{% else %}
|
||||
<!-- Gast: Eingabefeld + Button -->
|
||||
<!-- Gast: Zeige Eingabefeld -->
|
||||
<input type="email" id="notify-me-email" class="form-control mb-2"
|
||||
placeholder="{{ 'NotifyIfAvail.email_placeholder'|trans }}" required>
|
||||
<button class="btn btn-primary w-100 notify-me-button"
|
||||
@ -26,34 +27,30 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
console.log("NotifyMe Script geladen!");
|
||||
document.addEventListener('DOMContentLoaded', async function () {
|
||||
console.log("🔹 NotifyMe Script geladen!");
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
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 notifyButton = document.querySelector('.notify-me-button');
|
||||
|
||||
// Button nur anzeigen, wenn KEIN Warenkorb-Button existiert
|
||||
if (!cartButton) {
|
||||
if (!cartButton && notifyContainer) {
|
||||
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) {
|
||||
if (event.target.classList.contains('notify-me-button')) {
|
||||
let email;
|
||||
@ -62,7 +59,7 @@
|
||||
if (event.target.dataset.customerEmail) {
|
||||
email = event.target.dataset.customerEmail;
|
||||
} else {
|
||||
email = document.getElementById('notify-me-email').value;
|
||||
email = emailInput.value;
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(email)) {
|
||||
alert("Bitte geben Sie eine gültige E-Mail-Adresse ein.");
|
||||
@ -77,7 +74,7 @@
|
||||
})
|
||||
.then(response => response.json())
|
||||
.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