Fuck Javascript, added the js part in the twig file, Button will now only be shown if the basket button is not displayed on the page

This commit is contained in:
Nils 2025-03-13 15:57:19 +01:00
parent a8534a9e67
commit 51f52983a3
Signed by: slinicraftet204
GPG Key ID: 78E12696BAFC2A4B
4 changed files with 56 additions and 81 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "slinicraftet204/notifyifavail", "name": "slinicraftet204/notifyifavail",
"description": "benachrichtigt Kunden, sobald ein Produkt wieder verfügbar ist", "description": "benachrichtigt Kunden, sobald ein Produkt wieder verfügbar ist",
"version": "0.1.507", "version": "0.1.519",
"type": "shopware-platform-plugin", "type": "shopware-platform-plugin",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [

View File

@ -1,4 +0,0 @@
import PluginManager from 'src/plugin-system/plugin.manager';
import NotifyMe from './plugin/notify-me.plugin';
PluginManager.register('NotifyMe', NotifyMe, '[data-notify-me]');

View File

@ -1,54 +0,0 @@
import Plugin from 'src/plugin-system/plugin.class';
export default class NotifyMe extends Plugin {
init() {
this.notifyButton = this.el;
this.emailInput = document.getElementById('notify-me-email');
this.submitButton = document.getElementById('submit-notify');
this.registerEvents();
console.log("Hello this is NotifyMe Plugin JS init");
}
registerEvents() {
console.log("Hello this is NotifyMe Plugin JS registerevents");
if (this.notifyButton) {
this.notifyButton.addEventListener('click', () => this.submitForm(true));
}
if (this.submitButton) {
this.submitButton.addEventListener('click', () => this.submitForm(false));
}
}
validateEmail(email) {
console.log("Hello this is NotifyMe Plugin JS validateEmail");
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
submitForm(isLoggedIn) {
console.log("Hello this is NotifyMe Plugin JS submitForm");
let email;
const productId = this.notifyButton.dataset.productId;
if (isLoggedIn) {
email = this.notifyButton.dataset.customerEmail;
} else {
email = this.emailInput.value;
if (!this.validateEmail(email)) {
alert("Bitte geben Sie eine gültige E-Mail-Adresse ein.");
return;
}
}
fetch('/notification/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `email=${encodeURIComponent(email)}&productId=${encodeURIComponent(productId)}`
})
.then(response => response.json())
.then(data => alert(data.message))
.catch(error => console.error('Error:', error));
}
}

View File

@ -3,16 +3,11 @@
{% block buy_widget_price %} {% block buy_widget_price %}
{{ parent() }} {{ parent() }}
<!-- Debugging: Zeigt an, ob das Produkt als verfügbar gilt --> <div id="product-notify-container" class="product-notify-container mt-3" style="display: none;">
<p>Produkt verfügbar? {{ product.available ? 'Ja' : 'Nein' }}</p>
<p>Lagerbestand: {{ product.stock }}</p>
{% if not product.available or product.stock <= 0 %}
<div class="product-notify-container mt-3">
<h3>{{ "NotifyIfAvail.notify_me"|trans }}</h3> <h3>{{ "NotifyIfAvail.notify_me"|trans }}</h3>
{% if app.customer %} {% if app.customer %}
<button class="btn btn-primary w-100" id="notify-me-button" <button class="btn btn-primary w-100 notify-me-button"
data-product-id="{{ product.id }}" data-product-id="{{ product.id }}"
data-customer-email="{{ app.customer.email }}"> data-customer-email="{{ app.customer.email }}">
{{ "NotifyIfAvail.notify_me"|trans }} {{ "NotifyIfAvail.notify_me"|trans }}
@ -20,11 +15,49 @@
{% else %} {% else %}
<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" id="submit-notify" <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>
{% endif %} {% endif %}
</div> </div>
{% endif %}
<script>
document.addEventListener('DOMContentLoaded', function () {
const notifyContainer = document.getElementById('product-notify-container');
const cartButton = document.querySelector('.btn-buy'); // Prüft, ob der Warenkorb-Button existiert
// Button nur anzeigen, wenn KEIN Warenkorb-Button existiert
if (!cartButton) {
notifyContainer.style.display = 'block';
}
document.querySelectorAll('.notify-me-button').forEach(button => {
button.addEventListener('click', function () {
let email;
const productId = this.dataset.productId;
if (this.dataset.customerEmail) {
email = this.dataset.customerEmail;
} else {
email = document.getElementById('notify-me-email').value;
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
alert("Bitte geben Sie eine gültige E-Mail-Adresse ein.");
return;
}
}
fetch('/notification/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `email=${encodeURIComponent(email)}&productId=${encodeURIComponent(productId)}`
})
.then(response => response.json())
.then(data => alert(data.message))
.catch(error => console.error('Error:', error));
});
});
});
</script>
{% endblock %} {% endblock %}