Produkt-Sortierung
<script>
  document.querySelectorAll('.toggle-favorite').forEach(function(button) {
    function onClick(e) {  
      // check if href is already changed
      if (this.href !== window.location.origin + "/anfrage") {
        e.preventDefault();

        var placeHolderText = "Ich möchte gerne ein Angebot für die folgende Auswahl: ";
        var postTitle = this.getAttribute('data-post-title');
        var favorites = document.cookie.replace(/(?:(?:^|.*;\s*)produktauswahl\s*\=\s*([^;]*).*$)|^.*$/, "$1");
          
        favorites = favorites ? JSON.parse(favorites) : [];

        // If the post title is in the array, remove it
        if (favorites.includes(postTitle)) {
          var index = favorites.indexOf(postTitle);
          if (index > -1) {
            favorites.splice(index, 1);
          }
        // Otherwise add it to the array
        } else {
          // Only add a space before the postTitle if favorites is not empty
          postTitle = favorites.length === 0 ? placeHolderText + postTitle : " " + postTitle;
          favorites.push(postTitle);
        }

        // Save the updated array to the cookie, expires in 12 hours
        document.cookie = "produktauswahl=" + JSON.stringify(favorites) + ";path=/;max-age=" + 60 * 60 * 12;
        
        // Replace old link text and href with new one
        this.innerHTML = "Hinzugefügt. Klicken, um die Anfrage zu starten!";
        this.href = "/anfrage";

        console.log('Das Produkt wurde zur Anfrage hinzugefügt.');
        
        // remove the event listener
        this.removeEventListener('click', onClick);
      }
    }

    button.addEventListener('click', onClick);
  });
</script>