Brauche Hilfe bei meinen Warenkorb

Soulhunterla

Grünschnabel
Hallo Leute!

Ich habe einen Warenkorb der bis auf 2 Sachen nicht ganz so funktioniert wie ich es mir vorstelle.

1) Der Löschen-Button löscht leider nur den letzten Artikel. -> Ich hätte aber gerne, dass er den Artikel löscht, neben dem der Button ist.

2) Beim berechnen der Mehrwertsteuer will ich 10% und 20% getrennt haben. Das Problem ist, dass wenn ich die Menge ändere, dann wird sowohl der 10%ige und 20%ige neu berechnet.

1) Löscht leider nur den letzten hinzugefügten Artikel
PHP:
      //Produkt Löschen
        if(isset($_POST['delete'])){
          $product_id = $_POST['product_id'];
          unset($_SESSION['shopping_cart'][$product_id]);
          $message = "<p2>Gelöscht!</p2><br />";
        }

1) Hier die Buttons die das ausführen sollen:
PHP:
echo "
<td style='border-bottom:1px solid #000000;'><input type='hidden' name='product_id' value='$product_id' /></td>
<td style='border-bottom:1px solid #000000;'><input type='submit' name='delete' value='Löschen'></td>";

2) Hier mein bester Ansatz, aber leider besteht immer noch das Problem wie oben beschrieben:
PHP:
switch($products[$product_id]['mwst']) {
  case 10:
        $price_mwst10 = (($total_price*100) / (100 + 10)) * (10 / 100);
        echo"<p>MwsT 10%: € " . $price_mwst10 . "</p>";
  case 20:
        $price_mwst20 = (($total_price*100) / (100 + 20)) * (20 /100);
        echo"<p>MwsT 20%: € " . $price_mwst20 . "</p>";
        break;
default:
    echo"<p>Keine MwsT!</p>";
    break;
}

Hier meine Ansicht des Warenkorb:
PHP:
       // View cart
       else if(isset($_GET['view_cart'])) {

         //Warenkorb löschen
         echo "<h3>Ihr Warenkorb</h3>
         <p>
           <a href='./index.html?empty_cart=1'></a>
         </p>";

         if(empty($_SESSION['shopping_cart'])) {
           echo "Ihr Warenkob ist leer!<br />";
         }
         else {

           echo "<form name='view_cart' action='./index.html?view_cart=1' method='post'>
           <table style='width:500px;' cellspacing='0'>
               <tr>
                 <th style='border-bottom:1px solid #000000;'>Name</th>
                 <th style='border-bottom:1px solid #000000;'>Preis</th>
                 <th style='border-bottom:1px solid #000000;'>Menge</th>
                 <th style='border-bottom:1px solid #000000;'></th>
                 <th style='border-bottom:1px solid #000000;'>Löschen</th>

               </tr>";

               foreach($_SESSION['shopping_cart'] as $id => $product) {
                 $product_id = $product['product_id'];

                 if($product['quantity'] == 0){
                   //$key = array_search($id, $_SESSION['shopping_cart']);
                   unset($_SESSION['shopping_cart'][$id]);
                 }
                 else{

                 echo "<tr>
                   <td style='border-bottom:1px solid #000000;'>" . $products[$product_id]['name'] . "</td>
                   <td style='border-bottom:1px solid #000000;'>€ " . $products[$product_id]['price'] . "</td>
                   <td style='border-bottom:1px solid #000000;'>";

                   echo "<input type='text' style='width:50px' name='quantity[$product_id]' value='" . $product['quantity'] . "'
                      onkeyup='validate_num(this);'
                     >";

                     echo "
                     <td style='border-bottom:1px solid #000000;'><input type='hidden' name='product_id' value='$product_id' /></td>
                     <td style='border-bottom:1px solid #000000;'><input type='submit' name='delete' value='Löschen'></td>";

                     echo "</td>
                 </tr>";
               }
             }
             }

             echo "</table>";

             $total_price = 0;
             foreach($_SESSION['shopping_cart'] as $id => $product) {

               $product_id = $product['product_id'];
               $total_price += $products[$product_id]['price'] * $product['quantity'];

               }
/*
               //MWST 10
               $price_mwst10 = 0;
               foreach($_SESSION['shopping_cart'] as $id => $product) {

                 $product_id = $product['product_id'];
                 $price_mwst10 = (($total_price*100) / (100 + 10)) * (10 / 100);

                 }

                 //MWST 20
                 $price_mwst20 = 0;
                 foreach($_SESSION['shopping_cart'] as $id => $product) {

                   $product_id = $product['product_id'];
                   $price_mwst20 = (($total_price*100) / (100 + 20)) * (20 / 100);

                   }
*/
/*
               function mwst10($total_price){
                 $price_mwst10 = (($total_price*100) / (100 + 10)) * (10 / 100);
               }

               function mwst20($total_price){
                 $price_mwst20 = (($total_price*100) / (100 + 20)) * (20 / 100);
               }
*/

switch($products[$product_id]['mwst']) {
  case 10:

        $price_mwst10 = (($total_price*100) / (100 + 10)) * (10 / 100);
        echo"<p>MwsT 10%: € " . $price_mwst10 . "</p>";


  case 20:

        $price_mwst20 = (($total_price*100) / (100 + 20)) * (20 /100);
        echo"<p>MwsT 20%: € " . $price_mwst20 . "</p>";
        break;

default:
    echo"<p>Keine MwsT!</p>";
    break;

}

/*
             if($products[$product_id]['mwst'] == 10){
               //mwst10($total_price);
               //$mwst='10';
               $price_mwst10 .= (($total_price*100) / (100 + 10)) * (10 / 100);
              //echo"<p>MwsT 10%: € " . (($total_price*100) / (100 + mwst)) * (mwst / 100) . "</p>";
            }

            if($products[$product_id]['mwst'] == 20){
              //mwst20($total_price);
              //$mwst='20';
              $price_mwst20 .= (($total_price*100) / (100 + 20)) * (20 /100);
              //echo"<p>MwsT 20%: € " . (($total_price*100) / (100 + mwst)) * (mwst / 100) . "</p>";
            }
*/


            echo"<p>Nettopreis: € " . ($total_price * 100) / (100 + 20) . "</p>";
//          echo"<p>MwsT 10%: € " . $price_mwst10 . "</p>";
//          echo"<p>MwsT 20%: € " . $price_mwst20 . "</p>";
            echo "<p>Gesamtkosten: € " . $total_price . "</p>";

             echo "<input type='submit' name='update_cart' value='Aktualisieren' />
             </form>";

Und falls ihr jemand alles braucht um mir weiter zu helfen ist hier der komplette code:
PHP:
     <?php
       /*
        File:    index.html
        Author:  Markus Krakora
        Purpose: View products
        */

        session_start();

        require("products.php");

       // Initialize cart
       if(!isset($_SESSION['shopping_cart'])) {
         $_SESSION['shopping_cart'] = array();
       }
       // Empty cart
       if(isset($_GET['empty_cart'])) {
         $_SESSION['shopping_cart'] = array();
       }

       // **PROCESS FORM DATA**

       $message = '';

       // Add product to cart
       if(isset($_POST['add_to_cart'])) {
         $product_id = $_POST['product_id'];

         // Check for valid item
         if(!isset($products[$product_id])) {
           $message = "<p2>Ungültiges Produkt!</p2><br />";
         }
         // If item is already in cart, tell user
         else if(isset($_SESSION['shopping_cart'][$product_id])) {
           $message = "<p2>Das Produkt befindet sich bereits im Warenkorb!</p2><br />";
         }
         // Otherwise, add to cart
         else {
           $_SESSION['shopping_cart'][$product_id]['product_id'] = $_POST['product_id'];
           $_SESSION['shopping_cart'][$product_id]['quantity'] = $_POST['quantity'];
           $message = "<p2>Das Produkt wurde zum Warenkorb hinzugefügt!</p2><br />";
         }
       }


      //Produkt Löschen
        if(isset($_POST['delete'])){
          $product_id = $_POST['product_id'];
          unset($_SESSION['shopping_cart'][$product_id]);
          $message = "<p2>Gelöscht!</p2><br />";
        }

       // Update Cart
       if(isset($_POST['update_cart'])) {
         $quantities = $_POST['quantity'];

         if (is_array($quantities)) {
         foreach($quantities as $id => $quantity) {
           if(!isset($products[$id])) {
             $message = "<p2>Ungültiges Produkt!</p2><br />";
             break;
           }
           $_SESSION['shopping_cart'][$id]['quantity'] = $quantity;
         }
       }

         if(!$message) {
           $message = "<p3>Warenkorb aktualisiert!</p3><br />";
         }
       }


       // **DISPLAY PAGE**
       echo $header;

       echo $message;

       // View a product
       if(isset($_GET['view_product'])) {
         $product_id = $_GET['view_product'];

         if(isset($products[$product_id])) {

           // Display product
           echo "<table style='width:500px;' cellspacing='0'>";

           echo "<tr>";
             echo "<td valign=\"top\"> <img src=\"" . $products[$product_id]['images'] . "\" alt=\"\" height=\"100\" width=\"180\"></td>";
             echo "<td><span style='font-weight:bold;'>" . $products[$product_id]['name'] . "</span>
             <p> " . $products[$product_id]['description'] . " </p>
             <br>
             <p1>€ " . $products[$product_id]['price'] . "</p1>
             <br>
               <p>
                   <form action='./index.html?view_cart=1' method='post'>
                     <select name='quantity'>
                       <option value='1'>1</option>
                       <option value='2'>2</option>
                       <option value='3'>3</option>
                       <option value='4'>4</option>
                       <option value='5'>5</option>
                       <option value='6'>6</option>
                       <option value='7'>7</option>
                       <option value='8'>8</option>
                       <option value='9'>9</option>
                       <option value='10'>10</option>
                       <option value='11'>11</option>
                       <option value='12'>12</option>
                       <option value='13'>13</option>
                       <option value='14'>14</option>
                       <option value='15'>15</option>
                       <option value='16'>16</option>
                       <option value='17'>17</option>
                       <option value='18'>18</option>
                       <option value='19'>19</option>
                       <option value='20'>20</option>
                     </select>
                     <input type='hidden' name='product_id' value='$product_id' />
                     <input type='submit' name='add_to_cart' value='Zum Warenkorb hinzufügen' />
                   </form>
                 </p>

             </td>
           </tr>";

           echo "</table>";

         }
         else {
           echo "<p2>Ungültiges Produkt!</p2><br />";
         }
       }
       // View cart
       else if(isset($_GET['view_cart'])) {

         //Warenkorb löschen
         echo "<h3>Ihr Warenkorb</h3>
         <p>
           <a href='./index.html?empty_cart=1'></a>
         </p>";

         if(empty($_SESSION['shopping_cart'])) {
           echo "Ihr Warenkob ist leer!<br />";
         }
         else {

           echo "<form name='view_cart' action='./index.html?view_cart=1' method='post'>
           <table style='width:500px;' cellspacing='0'>
               <tr>
                 <th style='border-bottom:1px solid #000000;'>Name</th>
                 <th style='border-bottom:1px solid #000000;'>Preis</th>
                 <th style='border-bottom:1px solid #000000;'>Menge</th>
                 <th style='border-bottom:1px solid #000000;'></th>
                 <th style='border-bottom:1px solid #000000;'>Löschen</th>

               </tr>";

               foreach($_SESSION['shopping_cart'] as $id => $product) {
                 $product_id = $product['product_id'];

                 if($product['quantity'] == 0){
                   //$key = array_search($id, $_SESSION['shopping_cart']);
                   unset($_SESSION['shopping_cart'][$id]);
                 }
                 else{

                 echo "<tr>
                   <td style='border-bottom:1px solid #000000;'>" . $products[$product_id]['name'] . "</td>
                   <td style='border-bottom:1px solid #000000;'>€ " . $products[$product_id]['price'] . "</td>
                   <td style='border-bottom:1px solid #000000;'>";

                   echo "<input type='text' style='width:50px' name='quantity[$product_id]' value='" . $product['quantity'] . "'
                      onkeyup='validate_num(this);'
                     >";

                     echo "
                     <td style='border-bottom:1px solid #000000;'><input type='hidden' name='product_id' value='$product_id' /></td>
                     <td style='border-bottom:1px solid #000000;'><input type='submit' name='delete' value='Löschen'></td>";

                     echo "</td>
                 </tr>";
               }
             }
             }

             echo "</table>";

             $total_price = 0;
             foreach($_SESSION['shopping_cart'] as $id => $product) {

               $product_id = $product['product_id'];
               $total_price += $products[$product_id]['price'] * $product['quantity'];

               }
/*
               //MWST 10
               $price_mwst10 = 0;
               foreach($_SESSION['shopping_cart'] as $id => $product) {

                 $product_id = $product['product_id'];
                 $price_mwst10 = (($total_price*100) / (100 + 10)) * (10 / 100);

                 }

                 //MWST 20
                 $price_mwst20 = 0;
                 foreach($_SESSION['shopping_cart'] as $id => $product) {

                   $product_id = $product['product_id'];
                   $price_mwst20 = (($total_price*100) / (100 + 20)) * (20 / 100);

                   }
*/
/*
               function mwst10($total_price){
                 $price_mwst10 = (($total_price*100) / (100 + 10)) * (10 / 100);
               }

               function mwst20($total_price){
                 $price_mwst20 = (($total_price*100) / (100 + 20)) * (20 / 100);
               }
*/

switch($products[$product_id]['mwst']) {
  case 10:

        $price_mwst10 = (($total_price*100) / (100 + 10)) * (10 / 100);
        echo"<p>MwsT 10%: € " . $price_mwst10 . "</p>";


  case 20:

        $price_mwst20 = (($total_price*100) / (100 + 20)) * (20 /100);
        echo"<p>MwsT 20%: € " . $price_mwst20 . "</p>";
        break;

default:
    echo"<p>Keine MwsT!</p>";
    break;

}

/*
             if($products[$product_id]['mwst'] == 10){
               //mwst10($total_price);
               //$mwst='10';
               $price_mwst10 .= (($total_price*100) / (100 + 10)) * (10 / 100);
              //echo"<p>MwsT 10%: € " . (($total_price*100) / (100 + mwst)) * (mwst / 100) . "</p>";
            }

            if($products[$product_id]['mwst'] == 20){
              //mwst20($total_price);
              //$mwst='20';
              $price_mwst20 .= (($total_price*100) / (100 + 20)) * (20 /100);
              //echo"<p>MwsT 20%: € " . (($total_price*100) / (100 + mwst)) * (mwst / 100) . "</p>";
            }
*/


            echo"<p>Nettopreis: € " . ($total_price * 100) / (100 + 20) . "</p>";
//          echo"<p>MwsT 10%: € " . $price_mwst10 . "</p>";
//          echo"<p>MwsT 20%: € " . $price_mwst20 . "</p>";
            echo "<p>Gesamtkosten: € " . $total_price . "</p>";

             echo "<input type='submit' name='update_cart' value='Aktualisieren' />
             </form>";

             echo "<form action='/html/shop/se/contact.html' method='post'>
               <table style='width:200px;' cellspacing='0'>
                 <tr>
                   <td><input type='submit' name='zur_kassa' value='Zur Kassa'></td>
                 </form>";
                 echo "<form action = './index.html?view_product=$product_id' method = 'post'>";
                 echo "<td><input type='submit' name = 'weiter_einkaufen' value = 'Weiter Einkaufen'></td> ";
             echo "</form>";


             echo "</tr>
           </table>";
         }

       // Checkount
       else if(isset($_GET['checkout'])) {
         // Display site links

         echo "<h3>Zur Kassa</h3>";

         if(empty($_SESSION['shopping_cart'])) {
           echo "Ihr Warenkorb ist leer!<br />";
         }
         else {
           echo "<form action='./index.html?checkout=1' method='post'>
           <table style='width:500px;' cellspacing='0' border='0'>
               <tr>
                 <th style='border-bottom:1px solid #000000;'>Name</th>
                 <th style='border-bottom:1px solid #000000;'>Einzelpreis</th>
                 <th style='border-bottom:1px solid #000000;'>Menge</th>
                 <th style='border-bottom:1px solid #000000;'>Gesamtkosten</th>
               </tr>";

               $total_price = 0;
               foreach($_SESSION['shopping_cart'] as $id => $product) {
                 $product_id = $product['product_id'];

                 $total_price += $products[$product_id]['price'] * $product['quantity'];
                 echo "<tr>
                   <td style='border-bottom:1px solid #000000;'>" . $products[$product_id]['name'] . "</td>
                   <td style='border-bottom:1px solid #000000;'>€ " . $products[$product_id]['price'] . "</td>
                   <td style='border-bottom:1px solid #000000;'>" . $product['quantity'] . "</td>
                   <td style='border-bottom:1px solid #000000;'>€ " . ($products[$product_id]['price'] * $product['quantity']) . "</td>
                 </tr>";
                 }

             echo "</table>";

             echo"<p>Gesamtkosten: € " . $total_price . "</p>";

             echo "<input type='submit' name='update_cart' value='Aktualisieren' />
             </form>";

             echo "<form action='/html/shop/se/contact.html' method='post'>
               <table style='width:200px;' cellspacing='0'>
                 <tr>
                   <td><input type='submit' name='bestellen' value='Bestellen'></td>
                 </form>";
                 echo "<form action = './index.html?view_product=$product_id' method = 'post'>";
                 echo "<td><input type='submit' name = 'weiter_einkaufen' value = 'Weiter Einkaufen'></td> ";
             echo "</form>";
             echo "</tr>
           </table>";
         }
       }

       // View all products
       else {

         echo "<embed wmode='transparent' width='800' height='80' data='bannerse.swf' type='application/x-shockwave-flash' src='/html/shop//Flash/Banner/bannerse.swf'>";

         echo "<table style='width:600px;' cellspacing='0'>";

           echo "<tr>
             <td class='se1k'></td>
             <td><a href='./index.html?view_product=1'> sirius energies - 1. ray - Die Magie des Lebens </a>
             <p>physische Ebene - 1. Chakra - Basis-Chakra <br> Farbfrequenzen: Rot beginnend,über Koralle, Orange und Gold, bis hin zu Gelb,
                sowie deren Farbmischungen</p>
              <br>
              <p1>€ 21.00</p1> <p></p>
              <br>
             </td>
         </tr>";

           echo "<tr>
             <td>
           <p>
             <form action='./index.html?view_cart=1' method='post'>
               <select name='quantity'>
                 <option value='1'>1</option>
                 <option value='2'>2</option>
                 <option value='3'>3</option>
                 <option value='4'>4</option>
                 <option value='5'>5</option>
                 <option value='6'>6</option>
                 <option value='7'>7</option>
                 <option value='8'>8</option>
                 <option value='9'>9</option>
                 <option value='10'>10</option>
                 <option value='11'>11</option>
                 <option value='12'>12</option>
                 <option value='13'>13</option>
                 <option value='14'>14</option>
                 <option value='15'>15</option>
                 <option value='16'>16</option>
                 <option value='17'>17</option>
                 <option value='18'>18</option>
                 <option value='19'>19</option>
                 <option value='20'>20</option>
               </select>
               <input type='hidden' name='product_id' value='1' />
               <input type='submit' name='add_to_cart' value='Zum Warenkorb hinzufügen' />
             </form>
           </p>
           <br><br>
         </td>
         </tr>";

       echo "<tr>
       <td class='se2k'></td>
       <td><a href='./index.html?view_product=2'> sirius energies - 2. ray - Das “JA” zum Leben </a>
       <p>physische Ebene - 2. Chakra - Sakral-Chakra <br> Farbfrequenzen: Koralle und Orange, sowie deren Farbmischungen</p>
       <br>
       <p1>€ 21.00</p1> <p></p>
       <br>
       </td>
   </tr>";

       echo "<tr>
         <td>
       <p>
         <form action='./index.html?view_cart=1' method='post'>
           <select name='quantity'>
             <option value='1'>1</option>
             <option value='2'>2</option>
             <option value='3'>3</option>
             <option value='4'>4</option>
             <option value='5'>5</option>
             <option value='6'>6</option>
             <option value='7'>7</option>
             <option value='8'>8</option>
             <option value='9'>9</option>
             <option value='10'>10</option>
             <option value='11'>11</option>
             <option value='12'>12</option>
             <option value='13'>13</option>
             <option value='14'>14</option>
             <option value='15'>15</option>
             <option value='16'>16</option>
             <option value='17'>17</option>
             <option value='18'>18</option>
             <option value='19'>19</option>
             <option value='20'>20</option>
           </select>
           <input type='hidden' name='product_id' value='2' />
           <input type='submit' name='add_to_cart' value='Zum Warenkorb hinzufügen' />
         </form>
       </p>
      <br><br>
     </td>
     </tr>";
}
echo "</table>";

             echo $footer;
             ?>

Vielen Dank im Voraus für eure Hilfe!

lg Soul
 
Meine erste Idee wäre, mal im generierten HTML-Code zu gucken, ob Du tatsächlich bei jedem Button eine andere $product_id hast. Wenn ja, prüfe nach dem Absenden die $_POST, ob tatsächlich der gewünschte Wert übertragen wird.
 
Hi,

Also mein Code für die Buttons ist:
PHP:
echo "
<td style='border-bottom:1px solid #000000;'><input type='hidden' name='product_id' value='$product_id' /></td>
<td style='border-bottom:1px solid #000000;'><input type='submit' name='delete' value='Löschen'></td>";

Diese Buttons befinden sich in dieser foreach schleife:
PHP:
foreach($_SESSION['shopping_cart'] as $id => $product) {
$product_id = $product['product_id'];

echo "
<td style='border-bottom:1px solid #000000;'><input type='hidden' name='product_id' value='$product_id' /></td>
<td style='border-bottom:1px solid #000000;'><input type='submit' name='delete' value='Löschen'></td>";

}

Wie überprüfe ich ob in dem hidden Button immer eine andere $product_id drinnen ist? einfach mit echo"$product_id"; ausgeben?

lg
Soul
 
Das wäre eine Möglichkeit. Du kannst aber auch einfach die Seite aufrufen und dann im Browser den Quelltext anzeigen lassen. Entweder mit der rechten Maudtaste oder F12
 
Zurück